Reputation: 29
I have created two web pages using servlets and jsp. In the first page the user enters his first name , last name and mobile number and submits. Then i have redirected to the next page showing the details the user has entered. I have to provide a "edit" button such that it allows user to edit the values in the same page.(second page). how do i do that?
Upvotes: 1
Views: 1177
Reputation: 2764
You can do that in two ways. The first part of the two alternatives is the same:
The first page lets you enter user data using a form;
The servlet corrsponding to the form action does something like this:
public void doPost(HttpServletRequest request,HttpServletResponse response) {
// Get parameters
String name = request.getParameters("name"); String surname = request.getParameters("surname"); ...
// Make some validation on received parameters ...
// Creates user object User user = new User(name, username);
// Store it in HttpSession
request.getSession(true).setAttribute("user", user);
// Forward to view RequestDispatcher dispatcher = request.getRequestDispatcher("the target resource depends on the scenaro. See below");
dispatcher.forward(request,response);
}
The difference between 2 ways to do the complete flow resides in the second jsp. Basically
1) There's no a second jsp, the servlet forwards again to the jsp containing the form: in this case the difference is that instead of a blank form you will see in textfields values previously submitted. Note that you must change a little bit the first jsp in order to display those values in case the "user" object is found in session. Also, as you can see, in this case you don't strictly need an "Edit" button because user could directly change the textfield. Anyway, if you want that, in case the "user" instance is in session (i.e. the user already inserted data) you can display those textields disabled and the edit button could enable them allowing modification (with a simple javascript).
2) The second jsp doesn't contain a form, just labels, so a "read-only" view of what user inserted. In this case, the servlet forwards to the second jsp. The "Edit" button could simply redirects to the first jsp. Also in this case you should change this page (the first jsp) with the same changes previously described. So basically
As last note, valid for both approaches, using session lets you redisplay the first jsp with the data entered in case of validation errors, avoiding user to retype all information (which is very very tedious)
Upvotes: 1