Note that there are some explanatory texts on larger screens.

plurals
  1. POOther forms does not work after redirect from Servlet
    text
    copied!<p>So, I'm currently trying to learn some JSP, and I can't figure out how to get around this problem I'm having.</p> <p>Currently, I have an index.jsp page with several forms. For one form, it has two text fields that it sends to a servlet, test.java, in order to build a string. After building the string, the servlet then redirects back to index.jsp</p> <p>Original index.jsp address: <code>http://localhost:8080/TestJSPConversion/</code></p> <p>After the redirect, the address is <code>http://localhost:8080/TestJSPConversion/test</code></p> <p>The problem comes up when I try to use another form on index.jsp, it then takes me to a blank page at the address, <code>http://localhost:8080/TestJSPConversion/test?author=Peter+Johnson</code></p> <p>I believe it's due to the method I'm using to redirect from the servlet (request.getRequestDispatcher("/index.jsp").forward(request, response); but, I'm not too sure how to fix this issue. I would like to get the form to work even after the servlet redirects back to index.jsp.</p> <p>Servlet Code:</p> <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // Get parameters from the request. String name = request.getParameter("name"); String email = request.getParameter("email"); String message = null; GregorianCalendar calendar = new GregorianCalendar(); if (calendar.get(Calendar.AM_PM) == Calendar.AM) { message = "Good Morning, "; } else { message = "Good Afternoon, "; } message += name + " with the email, " + email; request.setAttribute("message", message); request.getRequestDispatcher("/index.jsp").forward(request, response); } </code></pre> <p>Index.jsp Code:</p> <pre><code>&lt;h2&gt;Choose authors:&lt;/h2&gt; &lt;form method="get"&gt; &lt;input type="checkbox" name="author" value="Tan Ah Teck"&gt;Tan &lt;input type="checkbox" name="author" value="Mohd Ali"&gt;Ali &lt;input type="checkbox" name="author" value="Kumar"&gt;Kumar &lt;input type="checkbox" name="author" value="Peter Johnson"&gt;Peter &lt;input type="submit" value="Query"&gt; &lt;/form&gt; &lt;c:set var="authorName" value="${param.author}" /&gt; &lt;/br&gt; &lt;c:if test="${not empty authorName}"&gt; &lt;c:out value="${authorName}" /&gt; &lt;/c:if&gt; &lt;/br&gt; &lt;form action="test" method="POST"&gt; First Name: &lt;input type="text" name="name" size="20"&gt;&lt;br /&gt; Surname: &lt;input type="text" name="email" size="20"&gt; &lt;br /&gt;&lt;br /&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; &lt;c:out value="${message}" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload