IDEA implements fake login using HttpServlet

1. JSP comments and scripts

1. Comments

Annotation type
HTML comments
JAVA comments // , /* */
JSP comments: <%–comment content–%>

The java annotation content in JSP annotations and java scripts will not be displayed in the web page source code, while html annotations will be displayed in the web page source code.

            Comment shortcut key: Ctrl+Shift+/

2, Script

1) <% java code%>: The java script will be translated into the service method

2) <%=variable%>: The output expression is used to output content to the page and will be translated into the service method.

3) <%!java code%>: The declaration is used to declare global variables or methods that will be translated into the servlet class

2. Fake login steps

1. Create a web project and deploy it to the tomcat server

2. Add an a tag to the index.jsp homepage to jump to the login page.

<%--
  Created by IntelliJ IDEA.
  User: 86177
  Date: 2023/2/13
  Time: 11:12
  To change this template use File | Settings | File Templates.
--%>
<html>
<head>
    <title>Web页面</title>
</head>
<body>
    <h1>欢迎登录</h1>
    <a herf="login">登录 </a> <br>
    <a herf="zhuce">注册</a>
</body>
</html>

3. Create a login.jsp as the login page, and write the form to specify the submission address and submission method.

1) Log in

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
</head>
<body>
    <h2>登录</h2>
    <form action="Login.jsp" method="post">
        账号:<input type="text" name="urse" value=""> <br>
        密码:<input type="text" name="psw" value=""> <br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

2) Register

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册页面</title>
</head>
<body>
    <h2>注册</h2>
    <form action="zhuce.jsp" method="post">
        账号:<input type="text" name="urse" value=""> <br>
        密码:<input type="text" name="psw" value=""> <br>
        <input type="submit" value="注册">
    </form>
</body>
</html>

4. Configure the servlet mapping relationship in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>com.cheng.servlet.Login</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
 
<--================================-->
 
    <servlet>
        <servlet-name>zhuce</servlet-name>
        <servlet-class>com.cheng.servlet.Zhuce</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>zhuce</servlet-name>
        <url-pattern>/zhuce</url-pattern>
    </servlet-mapping>
 
</web-app>
5. Create a Java class that inherits HttpServlet and obtains the request parameters in doPost.
package com.cheng.servlet;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Login-doGet...");
        doPost(request,response);
    }
 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Login-doPost...");
 
        // 1.从请求中获取用户提交的参数(数据)
        request.setCharacterEncoding("utf-8"); // 设置获取数据的格式
        String urse = request.getParameter("urse"); //根据表单的name属性获取用户输入的账号密码
        String psw = request.getParameter("psw");
        System.out.println(urse);
        System.out.println(psw);
 
        /*
         2.根据用户提交的用户名和密码---去数据库执行查询
         select * from users where urse=? and psw=?
         假登录*/
        // 3.判断成功要干什么? 判断失败要干什么?---做出响应
        response.setCharacterEncoding("utf-8"); //设置响应的编码格式
        response.setContentType("text/html:charset=UTF-8"); //以什么样的格式(文本/网页)响应textml:charset=UTF-8
        if (urse.equals("222")&&psw.equals("111")){
            // 登陆成功
            response.getWriter().write("登陆成功");
        }else {
            // 登陆失败
            response.getWriter().write("登陆失败");
        }
    }
}

 3. JSP execution process
1. The client initiates the first request for xxx.jsp, and the server will find the corresponding jsp file.

2. The server will translate the jsp file into xxx_jsp.java

3. Compile xxx_jsp.java into xxx_jsp.class

4. The server executes the xxx_jsp.class file and outputs the results to the client browser

4. Requset and Response
1. Overview
It can be seen from the process of writing Servlet that there are two parameters in the doGet) or doPost() method, namely HttpServletRequest and HttpServletResponse. When the Servlet is executed, the request information will be read from HttpServletRequest. The response information is encapsulated into the HttpServletResponse object.

2. The process of Servlet processing HTTP requests

 

Guess you like

Origin blog.csdn.net/WJY898989/article/details/129074154