javaWeb 简单注册登录(含数据库连接) -- (四) 用户登录请求与响应

这篇讲登录处理

传进来什么

user, pass


判什么

  1. 用户名是否存在
  2. 基于1中已存在的情况下判断密码是否匹配

响应页面

login.jsp:
1. 当登录成功, 写一个新页面, 并显示该用户的Sno
2. 失败则在登录页面基础上 + 错误提示

实现

新建一个名为LoginServlet的servlet
代码;

package adrui;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        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.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("Login Test");
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");
        Connection conn = (Connection)getServletContext().getAttribute("conn");
        LoginModel obj = new LoginModel();
        //第三方类用来连接数据库并判断登录结果
        try{
            String res = obj.check(conn, user, pass);
            request.setAttribute("res", res);
            RequestDispatcher view = request.getRequestDispatcher("login.jsp");//分派请求
            view.forward(request, response);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

}

web.xml中配置:

<servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>adrui.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login.do</url-pattern>
  </servlet-mapping>

LoginModel.java

package adrui;
import java.sql.*;
public class LoginModel {
    private Statement state = null;
    private String sql = "select * from user";
    private ResultSet rs = null; 
    public String check(Connection conn, String user, String pass) throws SQLException{
        state = conn.createStatement();
        rs = state.executeQuery(sql);//查询
        while(rs.next()){
            if(rs.getString(2).equals(user)){
                if(rs.getString(3).equals(pass)){
                    String sno = rs.getString(1);
                    return "1" + sno;//成功登录,后半段是Sno
                }
                return "-1";//密码不匹配
            }
        }
        return "0";//用户名不存在
    }
}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Login Page</title>
    <link type = "text/css" rel="stylesheet" href="css/login.css"/>
    <script src="js/login.js"></script>
</head>
<body>

    <% String f = (String)request.getAttribute("res"); 
        if(f.substring(0, 1).equals("1")){

    %>
    <!-- 登陆成功, 写新页面-->
    <p>Hello, <%= ((String)request.getAttribute("res")).substring(1) %></p>
    <% }else{
        %>
        <!-- 登陆失败, 写登录页面 + 错误信息 -->
        <script type="text/javascript">
            window.onload=show();
        </script>
        <form action="login.do" method="POST">
            <center>用户登录</center><br/><br/>
            USER: <br/> <input type="text" name="user"></input><br/><br/>
            PASS: <br/><input type="text" name="pass"></input>
            <br/>
            <br/>
        <input style="color: #fff;" type="submit" value="login"></input>
        <br/><br/>
        还没有账号? <a href="register.html">立即注册</a>
        <br/>
        <br/>
        The time: <p id="time"></p>
        </form>
        <!-- 找错误信息 -->
    <%  if(f.equals("0")){
        %><p>用户名错误</p>
        <%}else{ %>
        <p>密码错误</p>
        <%}} %>


</body>
</html>

测试

数据:
这里写图片描述



  1. 这里写图片描述

这里写图片描述
2. 这里写图片描述

这里写图片描述
3. 这里写图片描述
这里写图片描述


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LoginProject</display-name>
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>register</servlet-name>
    <servlet-class>adrui.registerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>register</servlet-name>
    <url-pattern>/register.do</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>adrui.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login.do</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>driver</param-name>
    <param-value>com.mysql.jdbc.Driver</param-value>
  </context-param>
  <context-param>
    <param-name>url</param-name>
    <param-value>jdbc:mysql://localhost:3306/adrui?useSSL=true</param-value>
  </context-param>
  <context-param>
    <param-name>user</param-name>
    <param-value>root</param-value><!--DBMS账户名 -->
  </context-param>
  <context-param>
    <param-name>pass</param-name>
    <param-value>password</param-value><!-- 密码 -->
  </context-param>
  <listener>
    <listener-class>
        adrui.MyContextListener
    </listener-class>
  </listener>
</web-app>

最后说点

这个项目是在eclipse中写的, 如果想要放在本地tomcat服务器的webapps目录下, 浏览器键入localhost:8080/LoginProject运行, 要找对项目文件
项目文件在项目所在工作空间的
.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps目录下

总结

  1. jsp的基础还较薄弱, 这块用了不少时间
  2. 细节错误太多, 特别是前端的大小写问题
  3. 对于tomcat配置项目的了解还不够, 单eclipse项目导出就走了不少弯路
  4. 姑且算是一个项目吧, 好歹用个几天时间呢

以上.

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值