This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Disable Auto Servlet PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Disable Auto Servlet PrintWriter" Watch "Disable Auto Servlet PrintWriter" New topic
Author

Disable Auto Servlet PrintWriter

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
How do you disable the PrintWriter that is automatically obtained in a servlet with an empty service method.
public class Example extends HttpServlet
{
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
/* Calculate some things, Do not send any output */
}
}
I run this servlet and get output. How do I disable all output?

Warren Bell
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
What output do you get? This is a very strange thing to want to do - can you elaborate on why you are using servlets for this, and what you expect a browser to do if it gets no response from the server?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
There are two servlets and one jsp. The first servlet includes the second. The second servlet does some processing. The first servlet then forwards to the jsp. The forward is not working because the second servlet has automatically obtained a PrintWriter. I am getting an IllegalStateException. I have narrowed it down to the included second servlet. I have tested the second servlet by itself with an empty service method. It still produces output e.g. headers and some basic html. This output is what is getting in the way of the forward call. I am including and forwarding using the following code:
RequestDispatcher d = getServletContext().getRequestDispatcher("/filename");
d.include(request, response);
or
d.forward(request, response);
Any help will be greatly appreciated.
Warren Bell
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If your servlet doesn't need to generate any output, why is it a servlet? Surely you could implement that functionality in a regular Java class which you call from your first servlet.
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
That is exactly what I ended up doing.
Thanks for your help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Disable Auto Servlet PrintWriter
 
Similar Threads
doGet&session
sending an applet in a html document using servlet
Exception handling in Servlet
Trying to test my first servlet.
Getting the Servlet output in a JSP file