要求:validate.jsp页面中获取请求参数(request.getparameter(“name属性的值”)),注意中文参数获取之前要设置请求编码(request.setCharaterEncoding(“UTF-8”)),通过连数据库、执行sql语句,完成登录验证,成功—success.jsp,失败-index.jsp
通过reponse.sendRedirect(“目标页面”);
注意事项:
- input中的name属性有用,它的属性用于在验证界面获取 eg:String name=request.getParameter("inputName");//inputName是input中name的值
-
设置请求参数的编码格式
request.setCharacterEncoding("UTF-8"); //此处解决了参数不能传递中文问题 - 跳转页面 response.sendRedirect(" ");
-
Class.forName("com.mysql.jdbc.Driver"); //mysql驱动名称
获取连接:提供3个链接参数 url数据库链接地址 username连数据库的用户名 password连数据库的密码
String url = "jdbc:mysql://localhost:3306/shopping"; // jdbc:mysql:// + MYSQL主机名 +: mysql端口号 + 数据库名
String username = "root";
String password = "123456";
自己做的版本:
mysql界面:
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 <a href="login.jsp">跳转到login.jsp页面!</a> 11 </body> 12 </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>登录</title> 8 </head> 9 <body> 10 <!-- 11 name属性表示请求参数名 inputName 12 value属性表示请求参数值 inputPwd 13 inputName=张三 14 inputPwd=123 15 --> 16 <form action="validate.jsp" method="post"> 17 用户名:<input type="text" name="inputName" id="username" placeholder="请输入用户名" /> 18 密码:<input type="password" name="inputPwd" id="pwd" placeholder="请输入密码" /> 19 <input type="submit" value="登录" id="register"/> 20 </form> 21 22 </body> 23 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" import="java.sql.*"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>验证</title> 8 </head> 9 <body> 10 <% 11 request.setCharacterEncoding("UTF-8"); 12 String name=request.getParameter("inputName"); 13 String pwd=request.getParameter("inputPwd"); 14 boolean flag= false; 15 try{ 16 Class.forName("com.mysql.jdbc.Driver"); 17 String url="jdbc:mysql://localhost:3306/shopping"; 18 String username="root"; 19 String password="111111"; 20 Connection conn=DriverManager.getConnection(url,username,password); 21 System.out.println(conn); 22 String sql = "select * from employee where username = '"+ name +"' and pwd = '"+ pwd +"' "; 23 PreparedStatement ps=conn.prepareStatement(sql); 24 ResultSet rs=ps.executeQuery(); 25 while(rs.next()){ 26 System.out.println("验证成功!"); 27 flag=true; 28 } 29 if(flag){ 30 response.sendRedirect("success.jsp"); 31 }else{ 32 response.sendRedirect("index.jsp"); 33 } 34 }catch(ClassNotFoundException e){ 35 e.printStackTrace(); 36 }catch(SQLException e){ 37 e.printStackTrace(); 38 } 39 %> 40 <a href="index.jsp">返回index</a> 41 </body> 42 </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 </body> 12 </html>
1 package com.shoppingmall.db; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 public class DatabaseConnection { 8 public static void main(String[] args) throws ClassNotFoundException, SQLException { 9 // TODO Auto-generated method stub 10 Class.forName("com.mysql.jdbc.Driver"); 11 String url="jdbc:mysql://localhost:3306/shopping"; 12 String username="root"; 13 String password="111111"; 14 Connection conn=DriverManager.getConnection(url,username,password); 15 System.out.println(conn); 16 String sql = "select * from employee where username ='张三' and pwd='123'"; 17 PreparedStatement ps=conn.prepareStatement(sql); 18 ResultSet rs=ps.executeQuery(); 19 while(rs.next()) { 20 System.out.println("验证成功!"); 21 } 22 } 23 24 }
带注释版:
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 表单提交验证用户信息合法性,validate.jsp页面验证 11 name属性表示请求参数名 inputName 12 value属性表示请求参数值 张三 13 inputName=张三 14 inputPwd=1234 15 <form action="validate.jsp" method="POST"> 16 用户名:<input placeholder="username" name="inputName"/><br> 17 密码:<input placeholder="pwd" name="inputPwd"/><br> 18 <input type="submit" value="登录"/><br> 19 </form> 20 </body> 21 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" import="java.sql.*"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 验证,返回到index.jsp 11 <% 12 //0-0)设置请求参数的编码格式 13 request.setCharacterEncoding("UTF-8"); 14 //0)获取表单中传递的用户名和密码 15 String name = request.getParameter("inputName"); 16 String pwd = request.getParameter("inputPwd"); 17 //标识验证结果: 18 boolean flag = false; 19 try{ 20 //1) 加载驱动 ,不同的数据库由不同的驱动(Mysql),mysql-connector.jar 21 //maven仓库下载jar包---存在工程根目录下的lib目录下-->修改构建目录(选中jar包,右键选择build path,选择add to build path) 22 Class.forName("com.mysql.jdbc.Driver");//mysql驱动名称 23 //2)获取连接:提供3个链接参数 url数据库链接地址 username连数据库的用户名 password连数据库的密码 24 String url = "jdbc:mysql://localhost:3306/shopping";// jdbc:mysql:// + MYSQL主机名 +: mysql端口号 + 数据库名 25 String username = "root"; 26 String password = "123456"; 27 Connection conn = DriverManager.getConnection(url,username,password); 28 //3-0)验证连接结果 29 System.out.println(conn); //控制台显示输出结果---console 30 //com.mysql.jdbc.JDBC4Connection@45283ce2 31 //3-1)执行sql语句 32 //根据用户名和密码,验证用户是否存在 33 String sql = "select * from s_user where uname = '"+ name +"' and upwd = '"+ pwd +"' "; 34 //3-2)通过连接获取预编译对象PreparedStatement 35 PreparedStatement ps = conn.prepareStatement(sql); 36 //3-3)执行查询 executeQuery---返回结果集ResultSet 37 ResultSet rs = ps.executeQuery(); 38 //4)处理结果ResultSet,遍历,while循环 39 while(rs.next()) { 40 System.out.println("验证成功"); 41 flag = true; 42 } 43 if(flag){ 44 //跳转页面 success.jsp 45 //1) 响应重定向 response 46 response.sendRedirect("success.jsp"); 47 }else{ 48 response.sendRedirect("index.jsp"); 49 } 50 51 }catch (ClassNotFoundException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } catch (SQLException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 %> 59 <a href="index.jsp">返回index</a> 60 </body> 61 </html>
1 package com.shoppingmall.db; 2 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 9 public class DatabaseConnection { 10 //连接数据库操作 11 //Java程序的入口,main方法 12 public static void main(String[] args){ 13 try { 14 //1) 加载驱动 ,不同的数据库由不同的驱动(Mysql),mysql-connector.jar 15 //maven仓库下载jar包---存在工程根目录下的lib目录下-->修改构建目录(选中jar包,右键选择build path,选择add to build path) 16 Class.forName("com.mysql.jdbc.Driver");//mysql驱动名称 17 //2)获取连接:提供3个链接参数 url数据库链接地址 username连数据库的用户名 password连数据库的密码 18 String url = "jdbc:mysql://localhost:3306/shopping";// jdbc:mysql:// + MYSQL主机名 +: mysql端口号 + 数据库名 19 String username = "root"; 20 String password = "123456"; 21 Connection conn = DriverManager.getConnection(url,username,password); 22 //3-0)验证连接结果 23 System.out.println(conn); //控制台显示输出结果---console 24 //com.mysql.jdbc.JDBC4Connection@45283ce2 25 //3-1)执行sql语句 26 //根据用户名和密码,验证用户是否存在 27 String sql = "select * from s_user where uname = '张三' and upwd = '123' "; 28 //3-2)通过连接获取预编译对象PreparedStatement 29 PreparedStatement ps = conn.prepareStatement(sql); 30 //3-3)执行查询 executeQuery---返回结果集ResultSet 31 ResultSet rs = ps.executeQuery(); 32 //4)处理结果ResultSet,遍历,while循环 33 while(rs.next()) { 34 System.out.println("验证成功"); 35 } 36 } catch (ClassNotFoundException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } catch (SQLException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 44 } 45 //Web工程页面入口是index.jsp 46 }