1. 程式人生 > >javaWeb 簡單註冊登入(含資料庫連線) -- (四) 使用者登入請求與響應

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. 姑且算是一個專案吧, 好歹用個幾天時間呢

以上.