-1

我尝试实现一个应该通过 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)
4

3 回答 3

3

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

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

也可以看看:

顺便说一句,在 GET 和 POST 上做同样的工作很臭。您确定您了解每种方法的具体用途吗?

于 2012-05-26T18:48:36.590 回答
0

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

于 2012-05-26T08:11:26.513 回答
-1

req替换this.getServletContext()

于 2012-05-26T08:12:11.250 回答