JSP page request Servlet

JSP page request Servlet

First of all you know, "/" meaning in the JSP and not the same as in web.xml

In web.xml, "/" represents the root server path, for example: http: // localhost: 8080 /

In the JSP page, "/" represents the root path of the project, for example: http: // localhost: 8080 / myfirstjsp_war_exploded /

Initiate a request form to form, for example:
First, write a obtain a user name and password Servlet:

package Controller;

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.io.IOException;

//@WebServlet(name = "CheckLogin")
public class CheckLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request.getParameter("username"));
        System.out.println(request.getParameter("userpassword"));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

Then start the configuration file web.xml

<servlet-mapping>
    <servlet-name>CheckLogin</servlet-name>
    <!--url-pattern配置的是浏览器访问时候的路径-->
    <url-pattern>/a/CheckLogin</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>CheckLogin</servlet-name>
    <!--具体所指向的Servlet类,需要带上包名-->
    <servlet-class>Controller.CheckLogin</servlet-class>
</servlet>

When a browser accesses / a / CheckLogin, does not require the presence of a directory of the project root directory, here is a path accessible, free to write, only need to first understand where "/" represents the root path project

Finally, write JSP page

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
       <form method="post" action="a/CheckLogin">
           用户名:<input type="text" name="username">
           密码:<input type="text" name="userpassword">
           <input type="submit">
       </form>
</body>
</html>

If action does not begin with "/", it represents in the current directory, if you are on the Web directory, is the root path of the project, and here web.xml configuration file corresponding to the content label

After configuring web.xml, remember to restart the server.

Test:
Here you can use the IE browser, the mouse on the inquiry button to see the address bar below the prompt about to jump, is the same as the configuration information in the label web.xml

Enter the user name and password:
Here Insert Picture Description
print station output:
Here Insert Picture Description

Released six original articles · won praise 0 · Views 374

Guess you like

Origin blog.csdn.net/weixin_45343343/article/details/101794431