简体   繁体   中英

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

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 ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM