Two ways to create a servlet


Two ways to create a servlet


We say servlet is an extension that runs on the server side and inherits from javax.servlet.http.HttpServlet. HttpServlet inherits GenericServlet, GenericServlet implements

Servlet, ServletConfig, Serializable these three interfaces. Its role is to handle requests sent by clients.

Access method: http://hostname:port number/servlet name The servlet name here is inaccurate, it should be the mapping name of the servlet name or urlPatterns

For example: http://localhost:8080/hehe to access the servlet


1. Conventional method

(1) Create a class, inherit javax.servlet.http.HttpServlet, and override doPost, doGet and other methods


package com.test.hehe;

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 Haha extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("hahaha");
    }
}



(2) In the /web/WEB-INF/ directory of the project, open web.xml, and add the following lines in the <web-app> here is the internal </web-app>:

    <servlet>
        <display-name>Haha</display-name>
        <servlet-name>Haha</servlet-name>
        <servlet-class>com.test.hehe.Haha</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>Haha</servlet-name>
        <url-pattern>/hahaha</url-pattern>
    </servlet-mapping>

Enter http://localhost:8080/hahaha in the browser to see the result (provided that you have started the server, and the project has been running in the server, the port number is 8080, and the host name is localhost).


2. Annotation

This creation of sevrlet is very simple, but it is not supported in lower versions. Without further ado, look at the code:


package com.test.hehe;

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="hehe",urlPatterns="/hehe")
public class SayHehe extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("hehe");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    
        resp.getWriter().println("hehe");
}}
 
 

 
 
Be sure to  import javax.servlet.annotation.WebServlet ;

and at the top write: @WebServlet (name= "hehe" ,urlPatterns= "/hehe" )

http://localhost:8080/hehe You can see the effect


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325645385&siteId=291194637