Servlet应该接受POST和GET

时间:2012-05-26 08:05:35

标签: java servlets

我尝试实现一个应该通过POST或GET调用的servlet。

所以我写了这样的东西

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

但是/或者因为最后的转发我得到一个IllegalStateException,这只是一个警告,但仍然。我该怎么办?

谢谢,
-lony

编辑:通缉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)

3 个答案:

答案 0 :(得分:3)

让GET和POST都做同样的事情与你的具体问题无关。您的问题是由写入servlet中的响应引起的,因此在执行转发之前隐式提交它。

不要触摸servlet中的response.getWriter()resposne.getOutputStream(),只需让JSP完成这项工作即可。如果您需要准备JSP需要显示的数据,只需将其设置为请求,会话或应用程序范围中的属性,具体取决于数据需要保留的范围。

另见:

在GET和POST上做同样的工作是臭的。您确定要了解这些方法的确切用途吗?

答案 1 :(得分:0)

您可以直接覆盖service()方法,为所有请求方法调用它。

答案 2 :(得分:-1)

req

替换 this.getServletContext()