0

In my project I have the following files:

  • pole.jsp containing a form and a submit
  • results.jsp where I display the result (it only has a title)
  • A PollServlet where I set both the title in pole.jsp and results.jsp

here are the files: poll.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Poll Page</title>
    </head>
    <body>
        <form action="/poll?action=pole" method="POST">
            <div>
                <a><h2><% out.print(request.getAttribute("oldTitle").toString());%>
                </h2></a><br>
            </div>  
           <br><br>
           <input type="submit" name = "submit"value="submit">
        </form>  
    </body

results.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Results</title>
    </head>
    <body >
        <form action="/poll?action=results" method="POST">
            <a><%  out.print(request.getAttribute("title"));%></a>
        </form>
    </body>
</html>

PollServlet.java

@WebServlet(name = "PollServlet", urlPatterns = {"/poll"})
public class PollServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

        }
    }

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String action = request.getParameter("action");
    if (action.equals("pole")) {
        request.setAttribute("oldTitle","new tile for poll.jsp ");
        getServletConfig().getServletContext().getRequestDispatcher(
            "/poll.jsp").forward(request, response);
    } else if (action.equals("results")) {

    /* set the title for results.jsp */
        request.setAttribute("title","title for results.jsp");
        getServletConfig().getServletContext().getRequestDispatcher(
            "/results.jsp").forward(request, response);  

        }
}
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, 
}

The problem seem to appear in the servlet's doGet() method. I'm only able to set the value for the first item's (poll.jsp) title and not the second (results.jsp) what am I doing wrong and how can this be implemented correctly? thanks!

  • I don't see any condition in your doGet method. The problem I think is you are forwarding using dispatcher, thus I don'e think the second attribute is getting set. To verify this can you try a logger between the first and second setAttribute methods. – Saurabh Jhunjhunwala May 08 '17 at 05:44
  • Can I see your updated code – Saurabh Jhunjhunwala May 08 '17 at 06:21
  • where are initializing the value for action, secondly in the else if where is the condition. – Saurabh Jhunjhunwala May 08 '17 at 06:40
  • just like you have action = PollServlet?action=results, why dont you change the action in pole.jsp to PollServlet?action=pole. I think with the current code, you will only enter the else block. if the request comes from result.jsp – Saurabh Jhunjhunwala May 08 '17 at 07:09
  • hang on, you are making the changes in doGet, when you are using http method of post. can you change that to GET and check. another alternate is change the doPost method and use request.setAttribute("action",????); also remember to remove ?action=?? – Saurabh Jhunjhunwala May 08 '17 at 07:27
  • Can you put a logger to find what is the value of action in the doGet method, assuming you are using http's Get method instead of POST – Saurabh Jhunjhunwala May 08 '17 at 09:15
  • Can you try System.out.println("action :: " + action); – Saurabh Jhunjhunwala May 08 '17 at 09:30
  • @SaurabhJhunjhunwala I put the line in doGet() and same error showd –  May 08 '17 at 10:04
  • did you update method="POST" to method="GET", once that is done, in the console you will see something like action ::: ______ – Saurabh Jhunjhunwala May 08 '17 at 10:12
  • @SaurabhJhunjhunwala with the code as is i get a HTTP Status 500 - Internal Server Error in poll.jsp and I get the default data as is in results.jsp –  May 08 '17 at 10:23
  • @SaurabhJhunjhunwala I think I might have explained my self incorrectly. I want to see the poll.jsp when I go to http://localhost:8080/poll/poll and after pressing submit go to http://localhost:8080/poll/results.jsp. –  May 08 '17 at 10:32
  • getServletConfig().getServletContext().getRequestDispatcher( "/results.jsp") will take to target jsp, try to change method=POST to method=GET – Saurabh Jhunjhunwala May 08 '17 at 11:24
  • @SaurabhJhunjhunwala I did that to no effect. when i put it localhost:8080/poll/poll I get http status 500 and when i put in results.jsp it does not show the changed title. –  May 08 '17 at 11:38
  • what is your application name – Saurabh Jhunjhunwala May 08 '17 at 11:50
  • @SaurabhJhunjhunwala the project is called poll, –  May 08 '17 at 11:55

1 Answers1

2

You cannot forward the same request multiple times because when once you have forwarded your request, then your response is already served. See this question.

However the code that's written after forward() will still get executed but it's for other purposes (like logging...) but you cannot forward that request again.


EDIT

You can send one parameter to your url /poll?action=pole and /poll?action=results which will help you to find out whether this page is poll.jsp or results.jsp, something like this

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        String action = request.getParameter("action");

        if(action.equals("pole")) {

            /* set the title for pole.jsp */

            request.setAttribute("oldTitle","new tile for poll.jsp ");
            getServletConfig().getServletContext().getRequestDispatcher(
                    "/poll.jsp").forward(request, response);

        } else if(action.equals("results")){

            /* set the title for results.jsp */
            request.setAttribute("title","title for results.jsp");
            getServletConfig().getServletContext().getRequestDispatcher(
                "/results.jsp").forward(request, response);  

        }
}
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • @hozan you can forward one request only one time. In your `doGet` method, you're forwarding it 2 times which won't work. And so you need to change this logic. You can use 2 different servlets to handle both requests separately. – Raman Sahasi May 08 '17 at 05:44
  • @hozan you can send one parameter in your URL which will help you to differentiate between 2 pages. See my edited answer. – Raman Sahasi May 08 '17 at 06:00
  • @hozan go to your IDE and see console... what error are you getting there? – Raman Sahasi May 08 '17 at 06:16
  • @hozan then you're not calling URL properly. You have to call either `/poll?action=pole` and `/poll?action=results`. Note that I've added an additional `action` parameter. – Raman Sahasi May 08 '17 at 07:31
  • This will not work, because the http method used is POST and he is making a change in the doGet method. – Saurabh Jhunjhunwala May 08 '17 at 07:39
  • @SaurabhJhunjhunwala thanks for pointing it out. I didn't saw that. @hozan if your form method is of type `POST` then doPost() will get called, whereas if it's `GET` then doGet() will get called. – Raman Sahasi May 08 '17 at 07:46
  • @hozan put debug point on `String action` and see what value are you getting. – Raman Sahasi May 08 '17 at 07:58
  • How do I do that in netbeans? I set the breakpoint next to the line it shows nothing –  May 08 '17 at 08:01