How can a servlet always perform the same task?

Posted by membersound on Stack Overflow See other posts from Stack Overflow or by membersound
Published on 2012-09-28T09:17:55Z Indexed on 2012/09/28 9:37 UTC
Read the original article Hit count: 225

Filed under:
|
|
|

I want a Servlet to perform always the same tasks. Regardless of if it is a GET or POST. At the moment I just call the doGet() from doPost(), which works fine.

Then I tried overriding the service() method, and I thought it would just work the same way. But it does not!

The code somehow gets executed, but the response does not generate the webpage:

response.getWriter();
response.println(string); 

This code works for the doGet/doPost methods, but not for the service. Why?

Servlet:

class MyWebServlet extends HttpServlet {
    @Override
    public void service(ServletRequest request, ServletResponse response) {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String string = "teststring";
        out.println(string);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about jsf