Reputation: 195
I'm using Eclipse JUNO IDE and tomcat 7.0.
I have a web application. In the application I have:
Servlet
JSP pages
HTML - the main page.
In the main page I have a form which redirect to the servlet (because I need to get data from Server) when a submit-button is clicked and the servlet redirect to the match jsp page. Now when I'm refreshing the jsp page, the servlet get NULL in the request. so my question is how to resubmitting? because I want to display the new data from the server.
NOTE: The Servlet talks to the Server with sockets, so when user is pressing on some submit-button, the request redirect to the servlet and the servlet redirect the request to the server to get the required data. Now when the servlet recieved the data, it's transporting the data to the jsp page, and the jsp page display it as html
if (request.getParameter("submit").equals("Show Taxis at Driving"))
{
requests.add("driving");
to_server.writeObject(requests);
to_server.flush();
requests.removeAllElements();
try{
driving = (Vector)from_server.readObject();
request.setAttribute("driving", driving);
//request.getRequestDispatcher("/WEB-INF/driving_page.jsp").forward(request, response);
RequestDispatcher disp = getServletContext().getRequestDispatcher("/WEB-INF/driving_page.jsp");
disp.forward(request, response);
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
else if (request.getParameter("submit").equals("Add Passenger to Taxi"))
{
requests.add("taxis");
requests.add("passengers");
to_server.writeObject(requests);
to_server.flush();
requests.removeAllElements();
try {
taxis_waiting = (Vector)from_server.readObject();
passengers = (Vector)from_server.readObject();
request.setAttribute("taxis", taxis_waiting);
request.setAttribute("passengers", passengers);
request.getRequestDispatcher("/WEB-INF/add_passenger_to_taxi.jsp").forward(request, response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
request.getRequestDispatcher("error_page.jsp").forward(request, response);
}
}
Upvotes: 1
Views: 9444
Reputation: 2191
You can simply solve this problem using sessions. This will be a alternative solution.
if(session.getAttribute("REFRESH")!=null){
response.sendRedirect("../your_servlet");
}
else{
session.setAttribute("REFRESH","TRUE");
}
You can place the above code in the jsp page. When the page loads at first time it checks for the session value, but not such a session is saved so it creates a session as in the else part of the code. but after that when the user clicks the refresh button, then session has a value and redirection happens.
But to avoid circular redirection , you need to remove the session in the servelt. So bellow code should be placed in your servlet.
session.removeAttribute("REFRESH");
Upvotes: 1
Reputation: 340713
Here a simplest and most common approach:
Your index.html
page should have a form using GET
action. This way form parameters are passed via query parameters to your servlet:
/servlet?search=foo
Your servlet process the search and forwards to the results.jsp
so that the browser still points to /servlet?search=foo
:
RequestDispatcher dispatcher = getServletContext().
getRequestDispatcher("results.jsp);
dispatcher.forward(request,response);
If your user refreshes the page, she will hit /servlet?search=foo
again, calling the server-side logic and refreshing the search results.
Upvotes: 1