javaEE note administrator login

Keywords: JSP xml Java JavaEE

index.jsp in the web Directory:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/12/25/025
  Time: 20:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form >
    <br> Please enter the administrator password
    <input type="password" name="password">
    <input type="submit">
  </form>
  </body>
</html>

Next, create a servlet with the class name Dologin, as shown in the figure

And that's what happened

(note that the bottom Create javaEE6 annotated class should not be checked)
Use JSP, servlet and JavaBean to realize MVC:
jsp is attempt V, servlet is controller C, javabean is model M
After creation, we found such a problem. Servlets are all red, no servlet s found

Other methods on the Internet are wrong.. As long as mine is right
file—>project structer
Then press the following figure

Then select servlet-api.jar under the lib folder of tomecat, ok

OK, now the servlet is also configured
The next thing we need to do is to implement administrator login on the index.jsp page and pass the password to the servlet named Dologin. If the password is correct, go to addSubject.jsp. If the password is wrong, return index.jsp

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/12/25/025
  Time: 20:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form method="post" action="/admin">
    <br> Please enter the administrator password
    <input type="password" name="password">
    <input type="submit">
  </form>
  </body>
</html>

Dologin.java:

package servlet;

import java.io.IOException;

public class Dologin extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String password=request.getParameter("password");
        if(password.equals("123456"))
            response.sendRedirect("/addSubject.jsp");
        else
            response.sendRedirect("/index.jsp");
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}

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_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>Dologin</servlet-name>
        <servlet-class>servlet.Dologin</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dologin</servlet-name>
        <url-pattern>/admin</url-pattern>
    </servlet-mapping>
</web-app>

As we can see, in index.jsp, through the

action="/admin"

Found the Dologin servlet
There are three ways to configure the URL pattern in a servlet
1. Directory matching, 2. Extension matching, 3. Full path matching

Posted by shadow_blade47 on Thu, 30 Apr 2020 16:17:20 -0700