Servlet的服务和init方法正在被调用,而不是doGet [英] Servlet's service and init method are being called, but not doGet

查看:176
本文介绍了Servlet的服务和init方法正在被调用,而不是doGet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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);
}
}

web.xml 看起来像这样:

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

< web-app>
< display-name>原型创建的Web应用< / 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>

当我访问url http:// localhost:8080 / instrurental / bla ,以下内容在控制台中打印出来:

  init 
service $ b根据我的预期,$ b

但不是 doGet 此外,浏览器中没有打印出来! (我期待它说它的工作)。



自昨天以来,我一直在努力解决这个问题。有谁有任何建议,可能是什么问题?

解决方案

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

  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.

这篇关于Servlet的服务和init方法正在被调用,而不是doGet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆