1. 程式人生 > >servlet之註冊登錄(簡寫)

servlet之註冊登錄(簡寫)

cor 響應 愛好 user 分享 語句 ace 設置 tps

1.註冊頁面

技術分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7
<title>Insert title here</title> 8 <script type="text/javascript" src="../js/jquery-1.6.4.min.js"></script> 9 <script type="text/javascript" src="../js/jquery.validate.js"></script> 10 <script type="text/javascript"> 11 $(function() 12 { 13 $("form").validate();
14 15 }) 16 </script> 17 </head> 18 <body> 19 <form action="../Servlet" method="post"> 20 用戶名:<input type="text" name="userName" required /><br/> 21 密碼:<input type="password" name="password" id="l1" maxlength="10" minlength="6" required /><br/> 22
確認密碼:<input type="password" name="password" equalTo="#l1" required /><br/> 23 性別:<input type="radio" name="sex" required />24 <input type="radio" name="sex"/>女<br/> 25 出生日期:<input type="date" name="birthday" required /><br/> 26 個人愛好:<input type="checkbox" name="hobby" required />遊泳 27 <input type="checkbox" name="hobby"/>籃球 28 <input type="checkbox" name="hobby"/>排球 29 <input type="checkbox" name="hobby"/>氣球<br/> 30 <button type="submit">註冊</button> 31 <button type="reset">重置</button> 32 </form> 33 </body> 34 </html>
View Code

2.servlet

技術分享
 1 package com.zdsofe.servlet1;
 2 
 3 import java.io.IOException;
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 
15 
16 
17 /**
18  * Servlet implementation class Servlet
19  */
20 @WebServlet("/Servlet")
21 public class Servlet extends HttpServlet {
22     private static final long serialVersionUID = 1L;
23     private static String DRIVER="com.mysql.jdbc.Driver";
24     private static String URL="jdbc:mysql://localhost:3306/mysql";
25     private static String user="root";
26     private static String key="775297";
27      Connection conn;
28      int result=0;
29     //加載驅動
30     static{
31         try {
32             Class.forName(DRIVER);
33         } catch (ClassNotFoundException e) {
34             e.printStackTrace();
35         }
36     }
37     /**
38      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
39      */
40     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41         /*request.getRequestDispatcher("https://www.baidu.com").forward(request, response);*/        
42         response.sendRedirect("/webProject1/Pages/Welcome.jsp");
43               
44     }
45 
46     /**
47      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
48      */
49     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50                 //設置請求字符編碼
51                 request.setCharacterEncoding("utf-8");
52                 //設置響應字符編碼
53                 response.setCharacterEncoding("utf-8");
54                 response.setContentType("text/html;charset=utf-8");
55                 //獲取用戶名
56                 String userName=request.getParameter("userName");
57                 //密碼
58                 String password=request.getParameter("password");
59                 try {
60                      //連接數據庫
61                      conn = DriverManager.getConnection(URL,user,key);
62                      //創建SQL語句對象
63                      Statement stmt=conn.createStatement();
64                      String sql="insert into denglu(userName,mima) values(‘"+userName+"‘,‘"+password+"‘)";
65                       result= stmt.executeUpdate(sql);    
66                 } catch (SQLException e) {
67                     e.printStackTrace();
68                 }
69                 if(result==1)
70                 {
71                     //response.sendRedirect(request.getContextPath()+"/jsp-login/welcome.jsp");
72                     //request.getRequestDispatcher("/Pages/DengLu.jsp?userN="+userName).forward(request, response);
73                     request.getRequestDispatcher("/Pages/DengLu.jsp").forward(request, response);
74                 }
75                 else
76                 {
77                     request.setAttribute("error", "註冊失敗!");
78                     request.getRequestDispatcher("/Pages/ZhuCe.jsp").forward(request, response);
79                 }
80     }
81 
82 }
View Code

3.登錄端

技術分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="Servlet" method="get">
11 用戶名:<input type="text" name="userName1"/><br/>
12 密碼:<input type="password" name="password1"/><br/>
13 <button type="submit">登錄</button>
14 <button type="reset">重置</button>
15 </form>
16 </body>
17 </html>
View Code

4.歡迎頁面

技術分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 welcome!
11 </body>
12 </html>
View Code

servlet之註冊登錄(簡寫)