登录和注册(jsp+servlet+JavaBean)

来源:互联网 发布:学中医的软件 编辑:程序博客网 时间:2024/05/17 08:44

一:示例

   //model层(Student.java)  

Java代码  收藏代码
  1. package com.model;  
  2.   
  3. public class Student {  
  4.     private int id;  
  5.     private String userName;  
  6.     private String userPwd;  
  7.   
  8.     public Student(int id, String userName, String userPwd) {  
  9.         super();  
  10.         this.id = id;  
  11.         this.userName = userName;  
  12.         this.userPwd = userPwd;  
  13.     }  
  14.       
  15.     public Student(String userName, String userPwd) {  
  16.         super();  
  17.         this.userName = userName;  
  18.         this.userPwd = userPwd;  
  19.     }  
  20.   
  21.     public int getId() {  
  22.         return id;  
  23.     }  
  24.   
  25.     public void setId(int id) {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     public String getUserName() {  
  30.         return userName;  
  31.     }  
  32.   
  33.     public void setUserName(String userName) {  
  34.         this.userName = userName;  
  35.     }  
  36.   
  37.     public String getUserPwd() {  
  38.         return userPwd;  
  39.     }  
  40.   
  41.     public void setUserPwd(String userPwd) {  
  42.         this.userPwd = userPwd;  
  43.     }  
  44. }  

  //dao层(DbManager.java)

  用数据源连接数据库,注意要将数据库驱动包添加到Tomcat ,lib目录下。  

Java代码  收藏代码
  1. package com.dao;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import javax.naming.Context;  
  8. import javax.naming.InitialContext;  
  9. import javax.naming.NamingException;  
  10. import javax.sql.DataSource;  
  11.   
  12. public class DbManager {  
  13.     public static Connection getConnection(){  
  14.         Connection conn = null;       
  15.         try {  
  16.             Context context = new InitialContext();  
  17.             DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/test");  
  18.             conn = ds.getConnection();  
  19.         } catch (NamingException e) {  
  20.             e.printStackTrace();  
  21.         } catch (SQLException e) {  
  22.             e.printStackTrace();  
  23.         }         
  24.         return conn;          
  25.     }  
  26.       
  27.     public static void closeConnection(Connection conn, PreparedStatement pst,ResultSet rs){  
  28.         try{  
  29.             if(rs !=null){  
  30.                 rs.close();  
  31.             }  
  32.             if(pst !=null){  
  33.                 pst.close();  
  34.             }  
  35.             if(conn !=null){  
  36.                 conn.close();  
  37.             }  
  38.         }catch(SQLException e){  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  
  42. }  

 

  //StudentDao.java

Java代码  收藏代码
  1. package com.dao;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9. import com.model.Student;  
  10.   
  11. public class StudentDao {  
  12.     public List<Student> findName(Student student){  
  13.         Connection conn = null;  
  14.         PreparedStatement pst = null;  
  15.         ResultSet rs = null;  
  16.           
  17.         List<Student> list = new ArrayList<Student>();        
  18.         try {  
  19.             conn = DbManager.getConnection();  
  20.             conn.setAutoCommit(false);  
  21.             pst = conn.prepareStatement("select * from student where userName=? and userPwd=?");  
  22.             pst.setString(1, student.getUserName());  
  23.             pst.setString(2, student.getUserPwd());  
  24.             rs = pst.executeQuery();  
  25.             while (rs.next()){  
  26.                 list.add(new Student(rs.getString(1),rs.getString(2)));  
  27.             }  
  28.             conn.commit();  
  29.         } catch (SQLException e) {  
  30.             try {  
  31.                 conn.rollback();  
  32.             } catch (SQLException e1) {  
  33.                 e1.printStackTrace();  
  34.             }  
  35.             e.printStackTrace();  
  36.         }finally{  
  37.             DbManager.closeConnection(conn, pst, rs);  
  38.         }  
  39.         return list;          
  40.     }  
  41.       
  42.     public int create(Student student){  
  43.         Connection conn = null;  
  44.         PreparedStatement pst = null;  
  45.         int state = 0;  
  46.           
  47.         try {  
  48.             conn = DbManager.getConnection();  
  49.             conn.setAutoCommit(false);  
  50.             pst = conn.prepareStatement("insert into student values(?,?)");  
  51.             pst.setString(1, student.getUserName());  
  52.             pst.setString(2, student.getUserPwd());  
  53.             state = pst.executeUpdate();  
  54.             conn.commit();  
  55.         }catch (SQLException e) {  
  56.             try {  
  57.                 conn.rollback();  
  58.             } catch (SQLException e1) {  
  59.                 e1.printStackTrace();  
  60.             }  
  61.             e.printStackTrace();  
  62.         } finally {  
  63.             DbManager.closeConnection(conn, pst, null);  
  64.         }  
  65.         return state;  
  66.     }  
  67. }  

  //服务层service(StudentService.java) 

Java代码  收藏代码
  1. package com.service;  
  2.   
  3. import java.util.List;  
  4. import com.model.Student;  
  5.   
  6. public interface StudentService {  
  7.     public List<Student> findName(Student student);  
  8.     public int create(Student student);  
  9. }  

  //实现层(StudentServiceImpl.java)  

Java代码  收藏代码
  1. package com.service.impl;  
  2.   
  3. import java.util.List;  
  4. import com.dao.StudentDao;  
  5. import com.model.Student;  
  6. import com.service.StudentService;  
  7.   
  8. public class StudentServiceImpl implements StudentService {  
  9.     private StudentDao studentDao;  
  10.   
  11.     public StudentServiceImpl() {  
  12.         studentDao = new StudentDao();  
  13.     }  
  14.   
  15.     public List<Student> findName(Student student) {  
  16.         return studentDao.findName(student);  
  17.     }  
  18.   
  19.     public int create(Student student) {  
  20.         return studentDao.create(student);  
  21.     }  
  22. }  

   //action层

  //StringUtil.java 

Java代码  收藏代码
  1. package com.action;  
  2.   
  3. public class StringUtil {  
  4.     public static boolean isBlank(String str) {  
  5.         return str == null || str.trim().length() == 0;  
  6.     }  
  7. }  

  //StudentServlet.java

Java代码  收藏代码
  1. package com.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.List;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import com.model.Student;  
  10. import com.service.StudentService;  
  11. import com.service.impl.StudentServiceImpl;  
  12.   
  13. public class StudentServlet extends HttpServlet {  
  14.     private StudentService studentService;  
  15.   
  16.     public StudentServlet() {  
  17.         studentService = new StudentServiceImpl();  
  18.     }  
  19.   
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  21.             throws ServletException, IOException {  
  22.         doPost(request, response);  
  23.     }  
  24.   
  25.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  26.             throws ServletException, IOException {  
  27.         response.setContentType("text/html");  
  28.         request.setCharacterEncoding("utf-8");  
  29.         response.setCharacterEncoding("utf-8");  
  30.         String userName = request.getParameter("userName");  
  31.         String userPwd = request.getParameter("userPwd");  
  32.         System.out.println(userName);  
  33.         System.out.println(userPwd);  
  34.           
  35.         if(StringUtil.isBlank(userName)&&(StringUtil.isBlank(userPwd))){  
  36.             request.setAttribute("error","姓名和密碼錯誤!");  
  37.             request.getSession().setAttribute("login""/login");  
  38.             request.getRequestDispatcher("/login").forward(request, response);  
  39.             return;  
  40.         }  
  41.           
  42.         List<Student> list = studentService.findName(new Student(userName,userPwd));  
  43.         if(list.size()>0){  
  44.             request.getSession().setAttribute("login", list.get(0));  
  45.             request.getRequestDispatcher("/chapter1.jsp").forward(request, response);  
  46.         }else{  
  47.             request.setAttribute("error""账号和密码错误!");  
  48.             request.getSession().setAttribute("login2""login2");  
  49.             request.getRequestDispatcher("/login").forward(request, response);  
  50.             return;  
  51.         }  
  52.     }  
  53. }  

  //RigesterServlet.java 

Java代码  收藏代码
  1. package com.action;  
  2.   
  3. import java.io.IOException;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import com.model.Student;  
  9. import com.service.StudentService;  
  10. import com.service.impl.StudentServiceImpl;  
  11.   
  12. public class RigesterServlet extends HttpServlet {  
  13.     private StudentService studentService;  
  14.   
  15.     public RigesterServlet() {  
  16.         studentService = new StudentServiceImpl();  
  17.     }  
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19.             throws ServletException, IOException {  
  20.         doPost(request, response);  
  21.     }  
  22.   
  23.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  24.             throws ServletException, IOException {        
  25.         response.setContentType("text/html");  
  26.         request.setCharacterEncoding("utf-8");  
  27.         response.setCharacterEncoding("utf-8");  
  28.           
  29.         String userName = request.getParameter("userName");  
  30.         String userPwd = request.getParameter("userPwd");  
  31.         String pwd2 = request.getParameter("pwd2");  
  32.           
  33.         request.setAttribute("userName", userName);  
  34.         request.setAttribute("userPwd", userPwd);  
  35.         request.setAttribute("pwd2", pwd2);  
  36.           
  37.         if(StringUtil.isBlank(userName)){  
  38.             request.setAttribute("userName", userName);  
  39.             request.getRequestDispatcher("/register").forward(request, response);  
  40.             return;  
  41.         }  
  42.         if(StringUtil.isBlank(userPwd)){  
  43.             request.setAttribute("userPwd", userPwd);  
  44.             request.getRequestDispatcher("/register").forward(request, response);  
  45.             return;  
  46.         }  
  47.         if(StringUtil.isBlank(pwd2)){  
  48.             request.setAttribute("pwd2", pwd2);  
  49.             request.getRequestDispatcher("/register").forward(request, response);  
  50.             return;  
  51.         }  
  52.           
  53.         Student student = new Student(userName,userPwd);  
  54.         if(studentService.create(student)>0){  
  55.             request.getRequestDispatcher("/chapter2.jsp").forward(request, response);  
  56.         }  
  57.     }  
  58. }  

  //context.xml 数据源配置文件 

Java代码  收藏代码
  1. <Context>  
  2. <Resource name="jdbc/test" auth="Container"  
  3.     type="javax.sql.DataSource" maxActive="100" maxIdle="30"  
  4.     maxWait="10000" username="sa" password="admin"  
  5.     driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"  
  6.     url="jdbc:sqlserver://localhost:1433;DatabaseName=test" />  
  7. </Context>  

 
   //login.jsp   

Java代码  收藏代码
  1. <%@ page language="java" pageEncoding="utf-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  3. <c:set var="bathPath" value="${pageContext.request.contextPath}"/>  
  4.   
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  6. <html>  
  7.   <head>  
  8.   </head>  
  9.   <body>  
  10.     <div>  
  11.     <form action="${bathPath}/StudentServlet" method="post">  
  12.         <div>  
  13.             <label>用户名:</label>  
  14.             <label><input type="text" name="userName" /></label><br />  
  15.             <label>密&nbsp;&nbsp;碼:</label>  
  16.             <label><input type="password" name="userPwd" /></label>             
  17.         </div>  
  18.         <div>  
  19.             <label><input type="submit" value="登錄" /></label>  
  20.             <label><input type="button" value="註冊" onclick="window.location='${bathPath}/register'"/></label>  
  21.         </div>  
  22.     </form>  
  23.     </div>  
  24.   </body>  
  25. </html>  

  //register.jsp 

Java代码  收藏代码
  1. <%@ page language="java" pageEncoding="utf-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  3. <c:set var="bathPath" value="${pageContext.request.contextPath}"/>  
  4.   
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  6. <html>  
  7.   <head>  
  8.   </head>  
  9.   <body>  
  10.     <div>  
  11.     <form action="${bathPath}/RigesterServlet" method="post">  
  12.         <div>  
  13.             <label for="username" class="label">用&nbsp;户&nbsp;名:</label>  
  14.                 <input type="text" name="userName"/>  
  15.                 <label id="chage1"></label><br/>  
  16.                 <label for="username">*正确填写用户名,6-12位之间请用英文小写、下划线、数字。</label><br/>  
  17.                 <label for="pwd1">用户密码:</label>  
  18.                 <input type="password" name="userPwd" /><br/>  
  19.                 <label for="pwd1">*正确填写密码,6-12位之间请用英文小写、下划线、数字。</label><br/>  
  20.                 <label for="pwd2">确认密码:</label>  
  21.                 <input type="password" name="pwd2"/><br/>  
  22.                 <label for="pwd2">*两次密码要一致,请用英文小写、下划线、数字。</label><br/>  
  23.         </div>  
  24.         <div>  
  25.             <label><input type="submit" value="註冊" /></label>  
  26.             <label><input type="reset" value="取消" /></label>  
  27.         </div>  
  28.     </form>  
  29.     </div>  
  30.   </body>  
  31. </html>  

 

0 0