很全面的登錄注冊頁面(jsp+servlet+jstl)


1.login.jsp


[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.   </head>  
  14.   <body>  
  15. <%   
  16. String msg="";  
  17. msg=(String)request.getAttribute("msg");  
  18. %>  
  19.   <center>  
  20.     <form action="dologin" method="post" onsubmit="return check();">  
  21.                             用戶名:<input type="text" id="username" name="username"/><span id="userPmg" style="color:red;"></span></br></br>  
  22.                            密 碼 :<input type="password" id="password" name="password"/><span id="pwd" style="color:red;"></span></br>  
  23.                   <input type="button" value="注冊" onclick="window.location='regis.jsp;'"/>      
  24.                   <input type="submit" value="登陸"/></br>  
  25.            <c:if test="${msg!=''}">  
  26.            <span style="color:red;">${msg}</span>  
  27.            </c:if>  
  28.     </form>  
  29.     </center>  
  30.   </body>  
  31.     <script type="text/javascript">  
  32.       function check(){  
  33.          document.getElementById("userPmg").innerHTML="";  
  34.          document.getElementById("pwd").innerHTML="";  
  35.          var flag=true;  
  36.          var username=document.getElementById("username").value;  
  37.          if(username.length==0){  
  38.              document.getElementById("userPmg").innerHTML="用戶名不能為空";  
  39.              flag=false;  
  40.          }  
  41.          var password=document.getElementById("password").value;  
  42.          if(password.length==0){  
  43.             document.getElementById("pwd").innerHTML="密碼不能為空";  
  44.             flag=false;  
  45.          }  
  46.          return flag;  
  47.       }  
  48.     
  49.   </script>  
  50.     
  51. </html>  

2.dologin


[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. package com.cdsxt.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.List;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.annotation.WebServlet;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. import javax.servlet.http.HttpSession;  
  12.   
  13. import com.cdsxt.dao.UserDao;  
  14. import com.cdsxt.dao.impl.UserDaoImpl;  
  15. import com.cdsxt.po.User;  
  16.   
  17. /** 
  18.  * Servlet implementation class dologin 
  19.  */  
  20. @WebServlet("/dologin")  
  21. public class dologin extends HttpServlet {  
  22.     private static final long serialVersionUID = 1L;  
  23.          
  24.     /** 
  25.      * @see HttpServlet#HttpServlet() 
  26.      */  
  27.     public dologin() {  
  28.         super();  
  29.         // TODO Auto-generated constructor stub  
  30.     }  
  31.   
  32.     /** 
  33.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  34.      */  
  35.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  36.   
  37.         this.doPost(request, response);  
  38.     }  
  39.   
  40.     /** 
  41.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  42.      */  
  43.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  44.         response.setContentType("text/html");  
  45.         response.setCharacterEncoding("UTF-8");  
  46.         request.setCharacterEncoding("UTF-8");  
  47.           
  48.         HttpSession session=request.getSession();  
  49.           
  50.         String username=request.getParameter("username");  
  51.         String password=request.getParameter("password");  
  52.         UserDao us=new UserDaoImpl();  
  53.         List<User> list=us.getUserList();  
  54.         int a=0;  
  55.         for(User user:list){  
  56.             if(user.getName().equals(username)&&(user.getPassword().equals(password))){  
  57.                 a=1;  
  58.                 session.setAttribute("username", username);  
  59.                 session.setAttribute("password",password);  
  60.                 response.sendRedirect("success.jsp");  
  61.                 return;  
  62.             }  
  63.         }  
  64.         if(a==0){  
  65.             String msg="用戶名或密碼錯誤,請重新輸入";  
  66.             request.setAttribute("msg", msg);  
  67.             request.getRequestDispatcher("login.jsp").forward(request,response);  
  68.             return;  
  69.         }  
  70.     }  
  71.   
  72. }  

3.UserDao


[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. package com.cdsxt.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.cdsxt.po.User;  
  6.   
  7. public interface UserDao {  
  8.     List<User> getUserList();  
  9.     boolean addUser(User user);  
  10. }  

4.UserDaoImpl


[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. package com.cdsxt.dao.impl;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. import com.cdsxt.dao.UserDao;  
  12. import com.cdsxt.po.User;  
  13.   
  14.   
  15. public class UserDaoImpl implements UserDao{  
  16.   
  17.     @Override  
  18.     public List<User> getUserList() {  
  19.         List<User> list=new ArrayList<User>();  
  20.         Connection conn=null;  
  21.         Statement ps=null;  
  22.         ResultSet rs=null;  
  23.         try{  
  24.             Class.forName("com.mysql.jdbc.Driver");  
  25.             conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");  
  26.             String sql="select * from User";  
  27.             ps=conn.createStatement();  
  28.             rs=ps.executeQuery(sql);  
  29.             while(rs.next()){  
  30.                 int id=rs.getInt("id");  
  31.                 String username=rs.getString("name");  
  32.                 String password=rs.getString("password");  
  33.                 int age=rs.getInt("age");  
  34.                   
  35.                 User user=new User();  
  36.                 user.setId(id);  
  37.                 user.setName(username);  
  38.                 user.setPassword(password);  
  39.                 user.setAge(age);  
  40.                   
  41.                 list.add(user);  
  42.                   
  43.             }  
  44.         }catch(Exception e){  
  45.             e.printStackTrace();  
  46.         }finally{  
  47.             if(rs!=null){  
  48.                 try {  
  49.                     rs.close();  
  50.                 } catch (SQLException e) {  
  51.                     // TODO Auto-generated catch block  
  52.                     e.printStackTrace();  
  53.                 }  
  54.             }  
  55.             if(ps!=null){  
  56.                 try {  
  57.                     ps.close();  
  58.                 } catch (SQLException e) {  
  59.                     // TODO Auto-generated catch block  
  60.                     e.printStackTrace();  
  61.                 }  
  62.             }  
  63.             if(conn!=null){  
  64.                 try {  
  65.                     conn.close();  
  66.                 } catch (SQLException e) {  
  67.                     // TODO Auto-generated catch block  
  68.                     e.printStackTrace();  
  69.                 }  
  70.             }  
  71.         }  
  72.         return list;  
  73.     }  
  74.   
  75.     @Override  
  76.     public boolean addUser(User user) {  
  77.         boolean flag=false;  
  78.         Connection conn=null;  
  79.         Statement st=null;  
  80.         try{  
  81.             Class.forName("com.mysql.jdbc.Driver");  
  82.             conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");  
  83.             st=conn.createStatement();  
  84.             String sql="insert into user(name,password) values('"+user.getName()+"','"+user.getPassword()+"');";  
  85.             int i=st.executeUpdate(sql);  
  86.             if(i>0){  
  87.                 flag=true;  
  88.                 System.out.println("添加成功");  
  89.             }  
  90.         }catch(Exception e){  
  91.             e.printStackTrace();  
  92.             System.out.println("添加失敗");  
  93.         }finally{  
  94.             if(st!=null){  
  95.                 try {  
  96.                     st.close();  
  97.                 } catch (SQLException e) {  
  98.                     // TODO Auto-generated catch block  
  99.                     e.printStackTrace();  
  100.                 }  
  101.             }  
  102.             if(conn!=null){  
  103.                 try {  
  104.                     conn.close();  
  105.                 } catch (SQLException e) {  
  106.                     // TODO Auto-generated catch block  
  107.                     e.printStackTrace();  
  108.                 }  
  109.             }  
  110.         }  
  111.         return false;  
  112.     }  
  113.   
  114. }  


5.regis.jsp


[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'regis.jsp' starting page</title>  
  13.       
  14.       
  15.   
  16.   </head>  
  17.   <script text="text/javascript">  
  18.       function check(){  
  19.          document.getElementById("umeg").innerHTML="";  
  20.          document.getElementById("pwd").innerHTML="";  
  21.          document.getElementById("pwdmeg").innerHTML="";  
  22.          var username=document.getElementById("username").value;  
  23.          var flag=true;  
  24.          if(username==""){  
  25.             document.getElementById("umeg").innerHTML="用戶名不能為空";  
  26.             flag=false;  
  27.          }  
  28.          var password=document.getElementById("password").value;  
  29.          if(password==""){  
  30.             document.getElementById("pwd").innerHTML="密碼不能為空";  
  31.             flag=false;  
  32.          }  
  33.          var repwd=document.getElementById("repwd").value;  
  34.          if(repwd!=password){  
  35.             document.getElementById("pwdmeg").innerHTML="兩次輸入的密碼不一致";  
  36.             flag=false;  
  37.          }  
  38.          return flag;  
  39.         
  40.       }  
  41.     
  42.     
  43.     
  44.     
  45.     
  46.   </script>  
  47. <%   
  48. String msg="";  
  49. msg=(String)request.getAttribute("msg");  
  50. %>  
  51.     
  52.     
  53.   <body>  
  54.       <center> <h2 style="color:blue;">注冊界面</h2></center>  
  55.       <center>  
  56.       <form action="doregis" method="post" onsubmit="return check();">  
  57.                             用戶名:<input type="text" id="username" name="username"/><span id="umeg" style="color:red;"></span>  
  58.              <c:if test="${msg!=''}">  
  59.            <span style="color:red;">${msg}</span>  
  60.            </c:if></br></br>  
  61.                               密碼:<input type="password" id="password" name="password"/><span id="pwd" style="color:red;"></span></br></br>  
  62.                             確認密碼:<input type="password" id="repwd" name="repwd"/><span id="pwdmeg" style="color:red;"></span></br></br>  
  63.                <input type="submit" value="提交"/>  
  64.         
  65.         
  66.         
  67.       </form>  
  68.         
  69.         
  70.         
  71.         
  72.       </center>  
  73.          
  74.   </body>  
  75. </html>  

6.doregis


[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. package com.cdsxt.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.annotation.WebServlet;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12. import javax.servlet.http.HttpSession;  
  13.   
  14. import com.cdsxt.dao.UserDao;  
  15. import com.cdsxt.dao.impl.UserDaoImpl;  
  16. import com.cdsxt.po.User;  
  17.   
  18. /** 
  19.  * Servlet implementation class doregis 
  20.  */  
  21. @WebServlet("/doregis")  
  22. public class doregis extends HttpServlet {  
  23.     private static final long serialVersionUID = 1L;  
  24.          
  25.     /** 
  26.      * @see HttpServlet#HttpServlet() 
  27.      */  
  28.     public doregis() {  
  29.         super();  
  30.         // TODO Auto-generated constructor stub  
  31.     }  
  32.   
  33.     /** 
  34.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  35.      */  
  36.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  37.         // TODO Auto-generated method stub  
  38.         this.doPost(request, response);  
  39.     }  
  40.   
  41.     /** 
  42.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  43.      */  
  44.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  45.         response.setContentType("text/html");  
  46.         response.setCharacterEncoding("UTF-8");  
  47.         request.setCharacterEncoding("UTF-8");  
  48.           
  49.         HttpSession session=request.getSession();  
  50.         String username=request.getParameter("username");  
  51.         String password=request.getParameter("password");  
  52.         User user1=new User();  
  53.         user1.setName(username);  
  54.         user1.setPassword(password);  
  55.           
  56.         UserDao us=new UserDaoImpl();  
  57.         List<User> list=us.getUserList();  
  58.         int a=0;  
  59.         for(User user:list){  
  60.             if(user.getName().equals(username)){  
  61.                 a=1;  
  62.                 String msg="該用戶名已經存在,請重新注冊";  
  63.                 request.setAttribute("msg", msg);  
  64.                 request.getRequestDispatcher("regis.jsp").forward(request,response);  
  65.                   
  66.             }  
  67.         }  
  68.         if(a==0){  
  69.             boolean b=us.addUser(user1);  
  70.             session.setAttribute("username",username);  
  71.             session.setAttribute("password",password);  
  72.             response.sendRedirect("login.jsp");  
  73.         }  
  74.     }  
  75.   
  76. }  


7.seccess.jsp

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.    
  13.   
  14.   </head>  
  15. <%   
  16. String username="";  
  17. username=(String)request.getAttribute("username");  
  18. %>  
  19.   <body>  
  20.                                             歡迎 <c:if test="${username!=''}">  
  21.            <span id="username" style="color:red;">${username}</span>  
  22.            </c:if>使用本系統  
  23.   </body>  
  24. </html>  

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: