user3718259
user3718259

Reputation: 15

Redisplaying JSP after post to Servlet

I have a Servlet called CampaignMaint.java with a doGet and a doPost and a JSP called JCampaignMaint.jsp. On entering the URL /CampaignMaint?campaign=Treasury the doGet builds the data into an array and executes:

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

JCampaignMaint.jsp then builds a table with some amendable fields as follows:

<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>

On clicking the Submit button an HTTP Post is sent to the server and processed by CampaignMaints goPost method where the form is read the database is updated. The screen is now blank with a URL /SalesPoliticalMapping/CampaignMaint.

What I want is that on submit the doPost updates the database but my screen remains the same with the table displayed (or rebuild / redisplayed) and with the original URL of /CampaignMaint?campaign=Treasury. I then want to be able to do further updates and submits.

The GlassFish stack trace when adding

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

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

Upvotes: 1

Views: 345

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96385

Instead of forwarding to a jsp, your doPost method can redirect to the method that generates the view:

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

That way you don't have to repeat the display logic in the post method and you will see the view updated with the submission results. (You'll see the screen refresh since this is a new GET request.)

This technique also avoids duplicate form submissions, it's called the Post/redirect/get pattern.

Upvotes: 3

Related Questions