Results 1 to 8 of 8
  1. #1
    rakesh_mca is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default how to pass data

    hi
    how to pass data from one servlet to another servlet?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can store attributes in the request (request.setAttribute) or store values in the session.

  3. #3
    suvojit168 is offline Member
    Join Date
    Jul 2009
    Posts
    10
    Rep Power
    0

    Default

    @rakesh_mca
    Hi, I am myself new to Servlet. Recently I read that you can perform inter servlet communication in two ways:
    1. By passing object between web components using request scope.

    Explanation:
    Servlet API provides mechanism to store objects inside request object using setAttribute() method which another servlet can extract using request object's getAttribute method().

    Ex:
    Servlet1
    [code]
    //assuming you have a code for testing the login info of user(basically task performed by controller servlet). Its srtucture will be something like this:

    Name=request.getParameter("txtName");
    Age=request.getParameter("txtAge");
    List errMsg= new LinkedList();
    if(Name.length()==0)
    errMsg.add("pls enter name, it cant be blank");
    if(Age<18)
    errMsg.add("Sorry you need to be 18 years or above to have an account");
    request.setAttribute("ErrorMessage",errMsg);
    request.setAttribute("UsrName",Name);
    request.setAttribute("UsrAge",Age);
    [\code]

    In above code,
    setAttribute(): is the request object's method which takes the alias name/String and the object to be passed to other servlet. This alias name is used by the target servlet to extract the object passed inside request object.You can also think this string as the name which identifies the attribute.



    Servlet2
    Java Code:
    //this servlet will extract the values passed by servlet1 inside thru request object using request object's getAttribute()
    
    System.out.println("Welcome " + request.getAttribute("UsrName") + " ,Your account created ");
    
    //similarly you can extract errMsg object also
    
    [\code]
    
    In above code 
    
    request.getAttribute(): is the request object's method which is used to extract values wrapped inside it using setAttribute() method( errMsg,UsrName,UsrAge-we did in servlet1)
    It basically take the string which is the attributes name.
    
    [\code]
    
    In above code 
    
    request.getAttribute(): is the request object's method which is used to extract values wrapped inside it using setAttribute() method( errMsg,UsrName,UsrAge-we did in servlet1)
    It basically take the string which is the attributes name.
    
    
    2.Using requestDispathcher
    
    Request Dispathcher enabled 1 servlet to send user request(data) to another. It does it using one of the 2 method: forward(), include().
    forward(): method simply forward the clients request to another servlet.
    include():This perform same task as forward() but in addition it forward its own data as well.
    
    You cant directly create RequestDispatchers object. You have to use getRequestDispatcher() method of request object to get its refernce.
    
    Ex.
    //lets say asfter receiving the usrName and age from client Servlet1 performs few task and then it forwards it to Servlet 2 to generate the final rsult and send back response to client
    
    Name=request.getParameter("txtName");
    Age=request.getParameter("txtAge");
    
    //Servlet1 does its processing 
    
    RequestDispatcher ob = request.getRequestDispatcher('Servlet2");
    ob.forward(request,response);
    or
    ob.include(request,response);
    Usage of forward or include will depend on users requirement.
    getRequestDispatcher('Servlet2"):this method returns a refernce to RequestDispatcher.It takes the name of servlet to which the request is dispatched.

    Servlet2

    //You can simply access the data in a way as if taht it is sent direcly to servlet2

    Name=request.getParameter("txtName");
    Age=request.getParameter("txtAge");



    for more info refer : for more info refer : java.sun.com and search the terms. I cant give u the link cos the forum rules require atleast 20 posts before one can supply links


    I have tried my best to explain the concepts. I know its not the best way to explain but I am sailing on the same bout as you are. So I did my best at my level. Hope it will be of atleast some use to you. revert back if u have doubts and I will try to clarify.... :)

  4. #4
    suvojit168 is offline Member
    Join Date
    Jul 2009
    Posts
    10
    Rep Power
    0

    Default

    sorry for the look and feel.....

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by suvojit168 View Post
    sorry for the look and feel.....
    You can use different type of tags in the forum, like CODE tags, QUOTE tags and so on next time. :P

  6. #6
    rakesh_mca is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    thank u ,i understood clearly

  7. #7
    suvojit168 is offline Member
    Join Date
    Jul 2009
    Posts
    10
    Rep Power
    0

    Default

    @Eranga : I wil keep it in mind.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Struts: pass data to other form
    By sgoyal in forum Advanced Java
    Replies: 1
    Last Post: 10-01-2009, 06:12 AM
  2. How can I pass a boolean to notifyObservers()
    By dumb_ass in forum New To Java
    Replies: 6
    Last Post: 03-05-2009, 09:15 PM
  3. I can't seem to pass a file through a parameter
    By daletron3030 in forum New To Java
    Replies: 5
    Last Post: 01-15-2009, 07:07 AM
  4. unable to pass value from one class to another
    By ddatta8 in forum New To Java
    Replies: 14
    Last Post: 12-28-2008, 03:24 PM
  5. Pass by ref. A work around?
    By diRisig in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •