Solved

Servlet question

Posted on 2006-06-25
4
250 Views
Last Modified: 2012-05-05
Hi,
I have a question about an example of servlet that illustrates the use of session.

This servlet forwards the user first to the url "urlPage0" where the user needs to introduce a name.
So urlPage0 is a JSP with a form, with textfield asking for a name.
The value of the action of the form is the servlet and it gives a hidden field "étape" with the value 1.
Then the servlet forwards the user to "urlPage1" where the name is printed and asks for an age.
A hidden parameter is given again "étape" with value 2.
Then finally the user gets forwarded to "urlPage2" where name and age are printed.

I don't understand the use of the line request.setAttribute("nom",""); in étape0 method
like request.setAttribute("age",""); in étape1.

and what is the use of
request.setAttribute("nom",nom);
followed by
session.setAttribute("nom",nom);



public class main extends HttpServlet{
String msgErreur=null;
String urlPage0=null;
String urlPage1=null;
String urlPage2=null;
String urlErreur=null;



//-------- INIT
public void init(){
      ServletConfig config=getServletConfig();
      urlPage0=config.getInitParameter("urlPage0");
      urlPage1=config.getInitParameter("urlPage1");
      urlPage2=config.getInitParameter("urlPage2");
      urlErreur=config.getInitParameter("urlErreur");
      if(urlPage0==null || urlPage1==null || urlPage2==null){
            msgErreur="Configuration incorrecte";
      }
}



//-------- GET
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
      if(msgErreur!=null){
            getServletContext().getRequestDispatcher(urlErreur).forward(request,response);
      }
      String étape=request.getParameter("etape");
      HttpSession session=request.getSession();
      if(étape==null) étape0(request,response,session);
      if(étape.equals("1")) étape1(request,response,session);
      if(étape.equals("2")) étape2(request,response,session);
      étape0(request,response,session);
}




//-------- POST
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
      doGet(request,response);
}




//-------- étape0
public void étape0(HttpServletRequest request, HttpServletResponse response, HttpSession session)
throws IOException, ServletException{
      request.setAttribute("nom","");
      request.getRequestDispatcher(urlPage0).forward(request,response);
}




//-------- étape1
public void étape1(HttpServletRequest request, HttpServletResponse response, HttpSession session)
throws IOException, ServletException{
      String nom=request.getParameter("nom");
      if(nom==null) étape0(request,response,session);
      nom=nom.trim();
      request.setAttribute("nom",nom);
      if(nom.equals("")){
            ArrayList erreurs=new ArrayList();
            erreurs.add("Nous n'avez pas indiqué de nom");
            request.setAttribute("erreurs",erreurs);
            étape0(request,response,session);
      }
      session.setAttribute("nom",nom);
      request.setAttribute("age","");
      request.getRequestDispatcher(urlPage1).forward(request,response);
}





//-------- étape2
public void étape2(HttpServletRequest request, HttpServletResponse response, HttpSession session)
throws IOException, ServletException{
      String nom=(String)session.getAttribute("nom");
      if(nom==null) étape0(request,response,session);
      request.setAttribute("nom",nom);
      String age=request.getParameter("age");
      if(age==null){
            request.setAttribute("age","");
            request.getRequestDispatcher(urlPage1).forward(request,response);
      }
      age=age.trim();
      request.setAttribute("age",age);
      if(! Pattern.matches("^\\s*\\d+\\s*$",age)){
            ArrayList erreurs=new ArrayList();
            erreurs.add("Age invalide");
            request.setAttribute("erreurs",erreurs);
            request.getRequestDispatcher(urlPage1).forward(request,response);
      }
      request.getRequestDispatcher(urlPage2).forward(request,response);
}
}

0
Comment
Question by:matthew016
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
pablomorales earned 375 total points
ID: 16988182
In a Servlet/JSP you can store information in the request and in the session. The following code in etape0:

request.setAttribute("nom","");

Is storing the variable "nom" with a value of "" in the request. This will (I guess) be read by the JSP code in ulrPage0 (using request.getAttribute("nom")). This would probably allow you to specify a default name (blank in this case) to be shown in the form.

In etape1 the code is reading the value that the user entered in the form from the request (using nom=request.getParameter("nom")) and then storing the value in the request (using request.setAttribute("nom",nom)) so that it can be read the JSP code contained in urlPage1. It is also storing it in the session (session.setAttribute("nom",nom)) so that it can be read later.

In etape2 the "nom" is read from the session and stored in the request so that it can be used by the JSP code served by urlPage2.

I hope this helps you.

0
 
LVL 9

Author Comment

by:matthew016
ID: 16988794
Thank you, I just have one more question about what u said :
"
storing the value in the request (using request.setAttribute("nom",nom)) so that it can be read the JSP code contained in urlPage1. It is also storing it in the session (session.setAttribute("nom",nom)) so that it can be read later.
"
I don't understand why in this example "nom" is stored in the request + in the session.
Why not only in the session and then JSP retrieve it from the session ?
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 125 total points
ID: 16990821
Normally,

Attributes in the request are removed automatically and hence request is good if you only would like the parameter to be read only in the subsequent JSP. Session, unlike request, will retain the attributes until it is invalidated or you removed it manually. Session is useful if you want the parameter to be available in every JSP but you must bear in mind that your session may get bigger and bigger if you do not remove them accordingly.

David
0
 
LVL 4

Expert Comment

by:pablomorales
ID: 16993956
My guess is that the designer was trying to keep the interface between the JSPs and the servlet consistant and to separate concerns and responsabilities. Kind of like in the MVC programming pattern (http://en.wikipedia.org/wiki/Model-view-controller). The JSP is the view and the Servlet acts as the controller and also stores the data (more or less what the model normaly does). There seems to be no independant model in this example. Ideally the data should be stored by the model (Javabean) and the view (JSP) and controller (Servlet) should read the data from the model. I agree with you in that is not as efficient as just reading the data from the session for such a small example, but  for larger proyects efficiency may need to be sacrificied.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
grep code 4 206
using if condition without JSTL 2 115
How to Post an If Statement in JSP 3 65
maven j2ee examles 2 64
This article was originally published on Monitis Blog, you can check it  here .   Some years back, I worked as the CTO.  During my tenure, I had a head of IT support reporting to me.  He did his job quite well and had a commendable sense of duty…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question