You need to forward to the jsp page on the server side, since redirection is an action on the client side (check location header 1 ) request attributes are lost.
replace
response.sendRedirect("MyFirstJSP.jsp");
with
request.getRequestDispatcher("MyFirstJSP.jsp").forward(request, response);
Edit: Sorry, I skipped this part
If I use the query manager in this case, then the values become available and the form is populated with values, but the URL in the address bar does not change and always shows the URL of the servlet.
however, you cannot pass request attributes to jsp when redirecting (as I mentioned above, this is a client action)
I suggest doing the following:
- Implement doGet only to display the page containing the form
- Implement doPost to process submitted form data
- use POST instead of GET in the HTML form to submit the form
In both cases, doGet and doPost use forward to display the * .jsp page.
GET / MyFirstServlet → go to MyFirstJSP.jsp
POST / MyFirstServlet → go to MyFirstJSP.jsp
This is the most commonly used and cleanest approach.
EDIT 2: Simple Example
SimpleFormServlet.java
public class SimpleFormServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String VIEW_NAME = "/WEB-INF/jsp/simpleForm.jsp"; private static final String MODEL_NAME = "form"; public SimpleFormServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute(MODEL_NAME, new SimpleForm()); request.getRequestDispatcher(VIEW_NAME).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final SimpleForm form = map(request); if(form.getfName().equalsIgnoreCase("abc")){ request.setAttribute(MODEL_NAME, form);
/WEB-INF/jsp/simpleForm.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> </head> <body> <form method="POST"> First Name<input type="text" name="fName" value="${form.fName}"><br> Last Name<input type="text" name="lName" value="${form.lName}"> <input type="submit" value="Send"> </form> </body> </html>
- GET / SimpleFormServlet
- doGet () prepares the form model (SimpleForm) and adds it as a request attribute called 'form'
- go to simpleForm.jsp
- access model values and fill out the form: $ {form.fName} and $ {form.lName}
- the browser still shows / SimpleFormServlet (and we like it ;-))
- POST form relative to / SimpleFormSerlvet (you do not need to explicitly set the action attribute of the form element)
- doPost () maps query parameters to SimpleForm.
- process the request and do whatever you want (validation)
- then you can either forward the file simpleForm.jsp (for example, when the check is completed), or redirect to another servlet (for example, SuccessServlet)