-1

I try to implement a servlet which should be called either through POST or GET.

So I wrote something like this

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  this.doGet(req, resp);
 }

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  // .. do stuff

  // forward to welcome page
  this.getServletContext().getRequestDispatcher("/guestbook.jsp").forward(req, resp);
  return;
 }

But/or because of the forward at the end I get an IllegalStateException, which is only a warning but still. What should I do differently?

Thanks,
-lony

Edit: Wanted Stacktrace

2012-05-26 18:02:16.422:WARN::/wsc/guestbook
java.lang.IllegalStateException: Committed
    at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1056)
    at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:216)
    at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:115)
    at de.tum.in.dss.GuestbookController.doGet(GuestbookController.java:135)
    at de.tum.in.dss.GuestbookController.doPost(GuestbookController.java:37)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:538)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1352)
    at de.tum.in.dss.XSSFilter.doFilter(XSSFilter.java:76)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1323)
    at de.tum.in.dss.AccessFilter.doFilter(AccessFilter.java:55)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1323)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:476)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:517)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:937)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:871)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:247)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
    at org.eclipse.jetty.server.Server.handle(Server.java:346)
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:589)
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:1065)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:823)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:220)
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:411)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:535)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
    at java.lang.Thread.run(Thread.java:722)
lony
  • 6,733
  • 11
  • 60
  • 92
  • 3
    An IllegalStateException is far from being only a warning. What does the code do, and what's the complete stack trace of the exception? Exceptions contain meaningful messages that are intended to be read, and help diagnose the problem. – JB Nizet May 26 '12 at 08:08
  • 2
    Have a read of this : http://www.jguru.com/faq/view.jsp?EID=501393 – Kazekage Gaara May 26 '12 at 08:09
  • Why you are using return at end? Please read these 1) [How to forward requests from Servlet to JSP](http://www.java-tips.org/java-ee-tips/javaserver-pages/how-to-forward-requests-from-servlet-t.html) 2) [Forward versus redirect](http://www.javapractices.com/topic/TopicAction.do?Id=181) Hopes that helps – Imran May 26 '12 at 08:10
  • @JB Nizet: I added the stack trace. To all others. Thanks for your links. Read your stuff but couldn't figure a way this helps me out? As I said I don't have a clue where the exception is coming from. Maybe I'm to inexperienced? – lony May 26 '12 at 16:10
  • Read http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#forward%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29. The response has already been committed. Why is impossible to tell since you still haven't shown your code. – JB Nizet May 26 '12 at 16:14
  • @JB Nizet You only asked for the stack trace so far ;) Uploaded the code too https://dl.dropbox.com/u/2990562/e3_1.zip – lony Jun 01 '12 at 07:48

3 Answers3

3

Your concrete problem is not related by letting GET and POST both do the same thing. Your problem is caused by writing to the response in the servlet and thus implicitly committing it before performing the forward.

Do not touch response.getWriter() or resposne.getOutputStream() in the servlet and just let JSP do that job. If you need to prepare data which JSP needs to display, just set it as an attribute in the request, session or application scope, depending on the scope the data needs to hold in.

See also:

Doing the same job on GET and POST is by the way smelly. Are you sure you understand what exactly each of those methods are to be used for?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Looked around in my code and only find one snipped which could case this. Inside XSSFilter I do `chain.doFilter(new FilteredRequest(request), response);` and just afterwards `chain.doFilter(request, response);` maybe there is a `return` statement needed? I think I know what POST and GET are good for, but this code is part of an exercise that's why it is convenient this way. Want to have one controller which handles form POST- and url GET-request which do the same thing. – lony Jun 01 '12 at 07:59
  • 1
    If they are *both* invoked during a single call, then yes that may be wrong. Remove that XSS filter anyway. XSS prevention should be done in response (in JSP), not on the request. See also http://stackoverflow.com/questions/2658922/xss-prevention-in-java – BalusC Jun 01 '12 at 10:58
  • True. But it is an exercise and the XSSFilter is requested ;) – lony Jun 02 '12 at 11:40
0

You can override directly the service() method, it's called for all request methods.

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
-1

replace this.getServletContext() with req

Dyapa Srikanth
  • 1,231
  • 2
  • 22
  • 60
  • The javadoc says: *The difference between this method and ServletContext#getRequestDispatcher is that this method can take a relative path.* – JB Nizet May 26 '12 at 08:17
  • @JBNizet the ans may be suitable for the requirement. But its not wrong right? – Dyapa Srikanth May 26 '12 at 08:20
  • Yes, it's wrong. Both methods do the same thing, except one accepts relative paths whether the other doesn't. And the OP is using an absolute path. – JB Nizet May 26 '12 at 08:22
  • 3
    It could use more explanation, more details, a reason why it's correct, a link to back up why you think it's correct. In general, there just isn't a lot of depth to your answer, and there is no way for me to know if it's right or wrong without doing my own research, but then at that point, I'll be able to write my own answer. In short, it's not clear *why* this fixes the problem, and SO answers should be a repository of knowledge to help others not just solve their problem but understand *why* the solution works. Hope this helps :) – jamesmortensen May 26 '12 at 08:23