Servlet's service and init method are being called, but not doGet

大憨熊 提交于 2020-01-03 03:13:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25608493/servlets-service-and-init-method-are-being-called-but-not-doget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!