I am new to jsp and servlet. My scenario is as follows
I have a jsp page which have a form in it. with two fields. The code snipet of the jsp page is as follows.
You need to forward to the jsp page on server side, as a redirect is a client side action (check out the location header 1) the request attributes get lost.
replace
response.sendRedirect("MyFirstJSP.jsp");
with
request.getRequestDispatcher("MyFirstJSP.jsp").forward(request, response);
Edit: sorry, I skipped this part
If I use the request dispatcher in this case then the values are gets available and the form is gets filled with the values but the url in the address bar does not chage and always shows the url to the servlet.
nevertheless, you cannot pass request attributes to your jsp when redirecting (as i've already mentioned above it's a clientside action)
I'd suggest doing the following:
In both, doGet and doPost, use forward to render the *.jsp page.
GET /MyFirstServlet -> forward to MyFirstJSP.jsp
POST /MyFirstServlet -> forward to MyFirstJSP.jsp
this is the most commonly used and clean 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);
// put additional attributes on the request
// e.g. validation errors,...
request.getRequestDispatcher(VIEW_NAME).forward(request, response);
}else{
System.out.println("No problem");
response.sendRedirect("/SuccessServlet");
}
}
private SimpleForm map(final HttpServletRequest request) {
SimpleForm form = new SimpleForm();
form.setfName(request.getParameter("fName"));
form.setlName(request.getParameter("lName"));
return form;
}
public static class SimpleForm implements Serializable {
private static final long serialVersionUID = -2756917543012439177L;
private String fName;
private String lName;
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
}
}
/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>
I know it's too late to answer but this would be helpful to someone. redirect is a client side action so that we can't getAttribute values but we can solve it by using URL Rewriting concept.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub
String firstname=request.getParameter("fname");
if(firstname.equalsIgnoreCase("abc")){
//System.out.println("Setting attributes");
response.sendRedirect("MyFirstJSP.jsp?fname="+firstname+"&lname="+request.getParameter("lname")+"");
}
else{
System.out.Println("No problem");
}
}
then use request.getParameter() to extract values as string in MyFirstJSP.jsp.