Bird
Bird

Reputation: 143

Other forms does not work after redirect from Servlet

So, I'm currently trying to learn some JSP, and I can't figure out how to get around this problem I'm having.

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

Original index.jsp address: http://localhost:8080/TestJSPConversion/

After the redirect, the address is http://localhost:8080/TestJSPConversion/test

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, http://localhost:8080/TestJSPConversion/test?author=Peter+Johnson

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.

Servlet 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);
}

Index.jsp Code:

<h2>Choose authors:</h2>
<form method="get">
    <input type="checkbox" name="author" value="Tan Ah Teck">Tan
    <input type="checkbox" name="author" value="Mohd Ali">Ali 
    <input type="checkbox" name="author" value="Kumar">Kumar 
    <input type="checkbox" name="author" value="Peter Johnson">Peter
    <input type="submit" value="Query">
</form>

<c:set var="authorName" value="${param.author}" />
</br>
<c:if test="${not empty authorName}">
    <c:out value="${authorName}" />
</c:if>
</br>
<form  action="test" method="POST">  
    First Name: <input type="text" name="name" size="20"><br />  
    Surname: <input type="text" name="email" size="20">  
    <br /><br />  
    <input type="submit" value="Submit">  
</form>
<c:out value="${message}" /> 

Upvotes: 0

Views: 1483

Answers (3)

Visruth
Visruth

Reputation: 3450

try response.sendRedirect("index.jsp?message=hello"); and display it using ${param.message} (EL). If you are using method as post in <form action="test" method="POST">, then you have to write the code in the doPost method.

Upvotes: 1

Cygnusx1
Cygnusx1

Reputation: 5409

First : you only implement the doPost so your first Form with method=Get will prob. fail.

Second : you should use sendRedirect instead of forward. this way if yo hit refresh on the browser, you will not get the warning about reposting data! Always better to sendRedirect when the server process is done and you want to display the result in a JSP.

regards

Upvotes: 0

D.S
D.S

Reputation: 1150

set action="test" for other <form> also.

if this leads you to some url like domain/TestJspConnection/test/test... then try action="/TestJSPConnection/test" as your <form> attribute

Oh yes, didn't notice this at all!...You didn't implement doGet() in your sevlet...so <form method="get" > is landing you on blank page

so..two solutions:

1) implement doGet() in servlet
or
2) set method="post" for first form

Upvotes: 0

Related Questions