【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>
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>
【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 }
莫听穿林打叶声,何妨吟啸且徐行,竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。