且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Servlet的服务和init方法正在被调用,而不是doGet

更新时间:2023-12-04 14:15:04

为什么你重写服务方法。没有必要。删除它,否则调用

  super.service(request,response); 

REASON

尝试看到源的HttpServlet类。你会看到,根据用于调用servlet的方法,即GET / POST,调用doGet()或doPost()方法。当容器实际接收到请求时,它启动一个新线程,并通过调用service()方法为客户端提供服务。所以如果你重写它,不要调用超类'service方法或定义你自己的策略,GET将被调用,doGet()方法永远不会被调用。您的请求不会调用doGet()方法,它的调用它的service()方法。


I have a simple Servlet that looks like this:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Bla extends HttpServlet {

    private static final long serialVersionUID = 16252534;

    @Override
    public void init() throws ServletException {
        System.out.println("init");
    }
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("doGet");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html><h1>It works!!</h1></html>");
    }

    @Override
    public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
        System.out.println("service");

    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost");
    }

    @Override
    public void destroy() {
        System.out.println("Destroy servlet");
    }
}   

and a web.xml that looks like this:

<?xml version="1.0" encoding="UTF-8" ?>

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Bla</servlet-name>
        <servlet-class>instrurental_proj.servlets.Bla</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Bla</servlet-name>
        <url-pattern>/bla</url-pattern>
    </servlet-mapping>  
</web-app>

When I visit the url http://localhost:8080/instrurental/bla, the following is printed out in the console:

init
service

but not doGet as I expect. Also, nothing is printed out in the browser! (I'm expecting it to say "It Works").

I've been struggling with this issue since yesterday. Does anyone have any suggestions, what could the problem be?

Why are u overriding the service method. There is no need of that. Remove it or else call

super.service(request,response);

REASON
Try to see the source of HttpServlet class. There you will see that depending on the method that is used to called the servlet i.e. GET/POST the necessary method doGet() or doPost() is called. And when the container actually receives a request it starts a new thread and and serves the client by calling service() method. So if you override it and don't call the super class' service method or define your own strategyhow GET will be called, doGet() method will never be called. Your request never calls doGet() method, its the service() method which calls it.