用servlet校验密码2

百度云链接: https://pan.baidu.com/s/1hBiWGxmzSoCFPu4bHhbApg 提取码: x222 

一:用户名登录

相关代码:

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

HTML
package com.swpu;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
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;

/**
 *  implementation class check
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        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
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String userName = request.getParameter("input_user");
        String passWord = request.getParameter("input_password");
        /*response.getWriter().write("用户名:" + userName);
        response.getWriter().write("</br>");
        response.getWriter().write("密码:" + userPass);*/
        try {
            // 加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            // URL指向要访问的数据库名test
            String url = "jdbc:mysql://127.0.0.1:3306/login";
            // MySQL配置时的用户名
            String user = "root";
            // MySQL配置时的密码
            String password = "as471147112";
            // 连续数据库
            Connection connection = DriverManager.getConnection(url, user, password);
            if(!connection.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            // statement用来执行SQL语句
            Statement statement = connection.createStatement();
            // 要执行的SQL语句
            String sql = "SELECT * FROM USER WHERE userName = '" + userName + "' AND passWord = " + passWord;
            // 执行SQL语句并返回结果集
            ResultSet rs = statement.executeQuery(sql);
             if(!rs.next()){
                    response.getWriter().write("用户名或密码错误");
             }
             else{
                    
                    response.getWriter().write("用户名:" + rs.getString(1));
                    response.getWriter().write("</br>");
                    response.getWriter().write("密码:" + rs.getInt(2));
                }
            // 关闭结果集
            rs.close();
            // 关闭连接
            connection.close();
        } catch(ClassNotFoundException e) {
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch(SQLException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

}

Servlet

 

posted @ 2019-03-30 11:30  楼南  阅读(140)  评论(0编辑  收藏  举报