【javaweb】注冊登錄頁面與mysql連接后的驗證與傳值


要求:

  1)regist.jsp      form  action   method    input  name

  2) 定義RegisterServlet     doGet()中實現獲取用戶名和密碼獲取到,連數據庫---執行insert   into   s_user(uname,upwd)  values(name,pwd);

    S_user  表中主鍵是自增長的,進行插入操作時不需要給主鍵復制

  3) 響應結果—注冊成功跳轉到login.jsp

  4) login.jsp獲取剛注冊的用戶名和密碼

eclipse界面

      

mysql頁面:

    

 

注意要點:

傳值方法:

   RegisterServlet.java頁面:

   request.setAttribute("Rname", RegisterName);

    request.setAttribute("Rpwd", RegisterPwd);

    RequestDispatcher rd=request.getRequestDispatcher("login.jsp");

    rd.forward(request, response);

           .jsp頁面: value="${Rname}"

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>登錄</title>
 8 </head>
 9 <body>
10 <!-- 
11 name屬性表示請求參數名  inputName
12 value屬性表示請求參數值 inputPwd
13 inputName=張三
14 inputPwd=123
15  -->
16  <p>登錄頁面</p>
17     <form action="UserServlet" method="post">
18         用戶名:<input type="text" name="inputName" id="username" placeholder="請輸入用戶名"  value="${Rname}" />
19         密碼:<input type="password" name="inputPwd" id="pwd" placeholder="請輸入密碼" value="${Rpwd}" />
20         <!-- 注意input的value值中來自RegisterServlet.java的傳遞方法-->
21             <input type="submit" value="登錄" id="register"/>
22     </form>
23 
24 </body>
25 </html>
login.jsp
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10  <p>注冊頁面</p>
11     <form action="RegisterServlet" method="post">
12         用戶名:<input type="text" name="inputName" id="username" placeholder="請輸入用戶名" />
13         密碼:<input type="password" name="inputPwd" id="pwd" placeholder="請輸入密碼" />
14             <input type="submit" value="注冊" id="register"/>
15     </form>
16 </body>
17 </html>
register.jsp

【UserServlet.java頁面】
 1 package com.shoppingmall.servlet;
 2 import java.io.IOException;
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 @WebServlet("/UserServlet")
14 public class UserServlet extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16     public UserServlet() {
17         super();
18     }
19     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20         response.getWriter().append("Served at: ").append(request.getContextPath());
21         request.setCharacterEncoding("UTF-8");
22         String name=request.getParameter("inputName");
23         String pwd=request.getParameter("inputPwd");
24         boolean flag= false;
25         try{
26             Class.forName("com.mysql.jdbc.Driver");
27             String url="jdbc:mysql://localhost:3306/shopping";
28             String username="root";
29             String password="111111";
30             Connection conn=DriverManager.getConnection(url,username,password);
31             System.out.println(conn);
32             String sql = "select  *   from employee  where username = '"+ name +"'  and pwd = '"+ pwd +"' ";
33             PreparedStatement ps=conn.prepareStatement(sql);
34             ResultSet rs=ps.executeQuery();
35             while(rs.next()){
36                 System.out.println("驗證成功!");
37                 flag=true;
38             }
39             if(flag){
40                 response.sendRedirect("success.jsp");
41             }else{
42                 response.sendRedirect("index.jsp");
43             }
44         }catch(ClassNotFoundException e){
45             e.printStackTrace();
46         }catch(SQLException e){
47             e.printStackTrace();
48         }
49     }
50     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51         doGet(request, response);
52     }
53 }

 

【RegisterServlet.java頁面】

 1 package com.shoppingmall.servlet;
 2 import java.io.IOException;
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import javax.servlet.RequestDispatcher;
 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 @WebServlet("/RegisterServlet")
15 public class RegisterServlet extends HttpServlet {
16     private static final long serialVersionUID = 1L;
17     public RegisterServlet() {
18         super();
19     }
20     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21         response.getWriter().append("Served at: ").append(request.getContextPath());
22         request.setCharacterEncoding("UTF-8");
23         response.setContentType("text/html;charset=utf-8");
24         String RegisterName=request.getParameter("inputName");
25         String RegisterPwd=request.getParameter("inputPwd");
26         boolean flag= false;
27         try{
28             Class.forName("com.mysql.jdbc.Driver");
29             String url="jdbc:mysql://localhost:3306/shopping";
30             String username="root";
31             String password="111111";
32             Connection conn=DriverManager.getConnection(url,username,password);
33             System.out.println(conn);
34             String sql = "select  *   from employee  where username = '"+ RegisterName +"'  and pwd = '"+ RegisterPwd +"' ";  //數據庫中查詢是否有 35             PreparedStatement ps=conn.prepareStatement(sql);
36             ResultSet rs=ps.executeQuery(sql);
37             while(rs.next()){
38                 flag=true;
39             }
40             if(flag){
41                 System.out.println("您的賬號已存在請直接登錄!");
42                 request.setAttribute("Rname", RegisterName);
43                 request.setAttribute("Rpwd", RegisterPwd);
44                 RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
45                 rd.forward(request, response);  //以上四行是賬號存在時向login.jsp傳值 46             }else{
47                 String sqlInsert = "insert into employee(username,pwd)"+"values('"+RegisterName+"','"+RegisterPwd+"')";  //向數據庫中插入數據 48                 PreparedStatement r=conn.prepareStatement(sqlInsert);
49                 r.executeUpdate();
50                 System.out.println("注冊成功!");
51                 request.setAttribute("Rname", RegisterName);
52                 request.setAttribute("Rpwd", RegisterPwd);
53                 RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
54                 rd.forward(request, response);  //以上四行是賬號不存在時向login.jsp傳值 55             }
56         }catch(ClassNotFoundException e){
57             e.printStackTrace();
58         }catch(SQLException e){
59             e.printStackTrace();
60         }
61     }
62     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
63         // TODO Auto-generated method stub
64         response.setCharacterEncoding("UTF-8");  
65         doGet(request, response);
66     }
67 }

 


注意!

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



 
粤ICP备14056181号  © 2014-2021 ITdaan.com