在发布到Servlet之后重新显示JSP

时间:2014-07-22 13:33:29

标签: java jsp servlets

我有一个名为CampaignMaint.java的Servlet,它带有一个doGet和一个doPost以及一个名为JCampaignMaint.jsp的JSP。 在输入URL / CampaignMaint?campaign = Treasury时,doGet将数据构建到一个数组中并执行:

Request.setAttribute("OrgContacts", contacts);
RequestDispatcher rd = request.getRequestDispatcher("JCampaignMaint.jsp");
rd.forward(request,response);  
然后,

JCampaignMaint.jsp构建一个包含一些可修改字段的表,如下所示:

<form action="CampaignMaint" method="post">
<c:forEach var="OrgContact" items="${OrgContacts}">
<tr>
<td>${OrgContact.orgName}</td>
<td>${OrgContact.orgTitle}</td>
<td><input type="number" name=${OrgContact.orgJoinName} value=${OrgContact.orgInfluence} min="1" max="5"</td>
<td><input type="number" name=${OrgContact.orgJoinName}  value=${OrgContact.orgSupport} min="1" max="5"</td>
<td><input type="number" name=${OrgContact.orgJoinName}  value=${OrgContact.orgEngagement} min="1" max="5"</td>
</tr>
</c:forEach>

<input type="submit" value="Submit">
</form>

单击“提交”按钮,HTTP Post将发送到服务器并由CampaignMaints goPost方法处理,其中读取表单以更新数据库。屏幕现在为空白,带有URL / SalesPoliticalMapping / CampaignMaint。

我想要的是提交doPost时更新数据库,但我的屏幕与显示的表格(或重建/重新显示)以及原始网址/ CampaignMaint?campaign = Treasury保持一致。然后,我希望能够进行进一步的更新和提交。

添加

时的GlassFish堆栈跟踪
response.sendRedirect("/CampaignMaint?campaign=Treasury"); 

警告:StandardWrapperValve [CampaignMaint]:servlet的Servlet.service()CampaignMaint抛出异常 java.lang.IllegalStateException     在org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:518)     在com.mycompany.salespoliticalmapping.controller.CampaignMaint.doPost(CampaignMaint.java:196)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:707)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)     在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)     在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)     在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)     在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)     在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)     在com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)     在org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)     在org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)     在org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)     在org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)     在org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)     在org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)     在org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)     在org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:55)     在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)     在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:564)     在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:544)     在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:3)

您的doPost方法可以重定向到生成视图的方法,而不是转发到jsp:

response.sendRedirect("/yourwebappname/CampaignMaint?campaign=Treasury");

这样您就不必在post方法中重复显示逻辑,您将看到使用提交结果更新的视图。 (您将看到屏幕刷新,因为这是一个新的GET请求。)

此技术还避免了重复的表单提交,称为Post/redirect/get pattern