Issue
I am trying to return a JSP request back to the calling Page with the help of a Java servlet class. This means that after adding a comment on a JSP page, the comment reappears on the JSP page as well after sending the request object to the servlet. This is the calling JSP code:
<form action="/WebAppOpe/UploadComments" method="post">
<textarea name="comment" placeholder="Comment" column="10"></textarea>
<input type="submit" value="Post Comment" class="btn-login" name="btn_post" />
<input type ="hidden" name="carIdComment" value="${car.carId}" />
</form>
And this is the servlet code of the UploadComments URI for sending the request back to the source.
RequestDispatcher rd = request.getRequestDispatcher("model.jsp?" + request.getParameter("carIdComment"));
rd.forward(request, response);
response.sendRedirect("model.jsp?"+ request.getParameter("carIdComment"));
Solution
Since you just want to refresh the current page,ajax
is a better choice for you.
For you current design,both forward
and redirect
are okay,but something need to pay attention:
if you use
redirect
,you can set parameters via the urlafter
forward
orredirect
,you need to query the exits comments,which means that you had can eitherredirect
to a new url and query data, or query the data beforeforward
.
Answered By - lucumt
Answer Checked By - Katrina (JavaFixing Volunteer)