JSP

Mapping Servlet

Plan 1. Modify web.xml

<servlet-name> ; Can set any name
<servlet-class> ; servlet path
<url-pattern> ; url path

<?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>HelloServlet</servlet-name>
        <servlet-class>com.waterhouse.sample.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/testPath</url-pattern>
    </servlet-mapping>
</web-app>

 

Plan 2. Write in Servlet(java file)

@WebServlet(name = “HelloServlet”, urlPatterns = {“/testPath”, “path2”})
name ; Servlet name
urlPattenrs ; url path

package com.waterhouse.sample;
 
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;
import java.io.PrintWriter;
 
@WebServlet(name = "HelloServlet", urlPatterns = {"/testPath", "/path2"})
public class HelloServlet extends HttpServlet {
 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<h1>Hello jsp servlet!!!!!!!!!!!! this is servlet mapping");
        out.flush();
    }
}

 

Back To Top