用servlet校验密码2

 1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 2         //设置请求和响应的字符集编码
 3                 response.setContentType("text/html;charset=UTF-8");
 4                 request.setCharacterEncoding("UTF-8");
 5                 PrintWriter out=response.getWriter();//获得输出流
 6                 //从请求对象中获得指定参数的值
 7                 String strName = request.getParameter("uname");
 8                 String strPassWord = request.getParameter("upassword");
 9                 
10                 DBConnection dbConnection=new DBConnection();//实例化数据库工具类
11                 dbConnection.Connection();
12                 dbConnection.uName=strName;
13                 dbConnection.select();//使用数据库查询
14                 String name = dbConnection.uName;
15                 String password = dbConnection.uPassword;
16                 
17                 
18                 if(name.equals(strName)&&password.equals(strPassWord))
19                 {
20                     out.println("你的账号是:"+strName+"<br>");
21                     out.println("你的密码是:"+strPassWord);
22                 }
23                 else{
24                     response.sendRedirect("LoginFail.html");
25                 }
26     }
Servlet中dopost方法
 1 package com.a;
 2 
 3 import java.sql.DriverManager;
 4 import java.sql.ResultSet;
 5 
 6 import com.mysql.jdbc.Connection;
 7 import com.mysql.jdbc.Statement;
 8 
 9 public class DBConnection {
10     String dirver = "com.mysql.jdbc.Driver";//驱动类名
11     String url = "jdbc:mysql://localhost:3306/userdb?useUnicode=true&characterEncoding=UTF8";
12     String user="root";
13     String password="zhoujunjie";
14     Connection con=null;
15     
16     String uName="NULL";
17     String uPassword="NUll";
18     
19     //连接数据库方法
20     public void Connection(){
21         try{
22             Class.forName(dirver);//加载驱动程序
23             con=(Connection)DriverManager.getConnection(url,user,password);
24             //if(!con.isClosed()){
25                 //System.out.print("连接成功");
26             //}
27         }catch(Exception e){
28             e.printStackTrace();
29         }
30     }
31     //关闭连接方法
32     public void close(){
33         try {
34             this.con.close();
35         } catch (Exception e) {
36             e.printStackTrace();
37         }
38     }
39     //查询方法
40     public void select(){
41         String sql="select * from userinfo";
42         try {
43             Statement stmt=(Statement)this.con.createStatement();
44             ResultSet rs=(ResultSet)stmt.executeQuery(sql);
45             uName="tom";
46             uPassword="123";
47             while(rs.next()){
48                 if(rs.getString("name")==uName){
49                     uName=rs.getString("name");
50                     uPassword=rs.getString("password");
51                     break;
52                 }
53             }
54             stmt.close();
55         } catch (Exception e) {
56             e.printStackTrace();
57         }
58     }
59 }
DBConnection.java工具类

结构图:

 

链接:https://pan.baidu.com/s/1mFm99jrt_TF4CV4sJROX8A
提取码:myec
复制这段内容后打开百度网盘手机App,操作更方便哦

 

posted @ 2019-03-30 20:24  Joker_zou  阅读(121)  评论(0编辑  收藏  举报