首页 > 其他 > 详细

用servlet校验密码2

时间:2019-03-30 15:42:32      阅读:177      评论:0      收藏:0      [点我收藏+]

一、效果图
技术分享图片技术分享图片

技术分享图片

技术分享图片

 

 

 

二、链接

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

三、代码

<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8" />
    <title>登录界面</title>    
        <script type="text/javascript" src="script/login.js"></script>
        <link href="css/login.css" type="text/css" rel="stylesheet">
    </head>
    <body>
    <div id="page">
        <div id="page_head">
            <div id="logo">
                <img src="rsc\login_logo.png"/>
            </div>
            </div>
        <div id="page_body">
            <div id="login">
                <div id="loginTitle">
                    <b>账号登录</b>
                </div>
                <form action="TestServlet"  id="loginForm">
                    <div id="tip">请填写用户名</div>
                    <div class="textitem_count">
                        <input style="width:320px;height:30px" name="input_user" type="text"  placeholder="用户名">
                    </div>
                    <div class="textitem_pwd">
                        <input style="width:320px;height:30px" name="input_password"  type="password"  placeholder="密码">
                    </div>
                    <div style="position:absolute;left:30px;top:220px;width:320px">
                        <sqan style="color:red;float:left;">学生选择@stu.swpu.edu.cn</sqan>
                        <a href="#" style="float:right;">忘记密码</a>
                    </div>    
                    <div style="position: absolute; left:30px; top:260px;width: 320px"> 
                      <input onclick="fnLogin()" style="float:right;background:url(rsc/login_btn.jpg);" class="btn" type="submit" value="登 录" />   
                     </div> 
                 </form>
          </div>
         </div>
        <div id="page_foot">西南石油大学</div>
    </div>
</body>
</html>



package swpu.com;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    String sql = null;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    boolean isLoing=false;
    /**
     * @see HttpServlet#HttpServlet(g)
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String userName = request.getParameter("input_user");
        String userPsw  = request.getParameter("input_password");
        
         //连接数据库检测用户名和密码
        try {
            //连接数据库
            Class.forName("com.mysql.jdbc.Driver");
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn=DriverManager.getConnection("jdbc:mysql://localhost/workdb?user=root&password=root&serverTimezone=UTC");
            stmt=conn.createStatement();
            sql="select userPWD from login where userID=‘"+userName+"‘";
            rs=stmt.executeQuery(sql);
            
            if(rs.next()) {
                //获取输入用户名的密码进行检验,若与输入的一致则isLogin置为true,反之置false
                String pw=rs.getString("userPWD");
                if (pw.equals(userPsw)) {
                    isLoing=true;
                }else {
                    isLoing=false;
                }
            }else {
                //若未查询到用户的存在也置为false
                isLoing=false;
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null)
                    rs.close();
                if(stmt!=null)
                    stmt.close();
                if(conn!=null)
                    conn.close();
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
        
        if(isLoing){
        response.getWriter().write("登陆成功");
        response.getWriter().write("</br>");
        response.getWriter().write("用户名:" + userName);
        response.getWriter().write("</br>");
        response.getWriter().write("密码:" + userPsw);
        }
        else{

            response.getWriter().write("登录失败!");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
    }
}

 

用servlet校验密码2

原文:https://www.cnblogs.com/catya/p/10627310.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!