Results 1 to 8 of 8
Thread: how to pass data
- 10-18-2009, 03:53 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
- 10-18-2009, 05:04 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You can store attributes in the request (request.setAttribute) or store values in the session.
- 10-21-2009, 08:10 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 10
- Rep Power
- 0
@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
Usage of forward or include will depend on users requirement.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);
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.... :)
- 10-21-2009, 08:12 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 10
- Rep Power
- 0
sorry for the look and feel.....
- 10-22-2009, 04:55 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-22-2009, 07:22 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
thank u ,i understood clearly
- 10-23-2009, 07:38 AM #7
Member
- Join Date
- Jul 2009
- Posts
- 10
- Rep Power
- 0
@Eranga : I wil keep it in mind.
- 10-25-2009, 01:13 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Struts: pass data to other form
By sgoyal in forum Advanced JavaReplies: 1Last Post: 10-01-2009, 05:12 AM -
How can I pass a boolean to notifyObservers()
By dumb_ass in forum New To JavaReplies: 6Last Post: 03-05-2009, 08:15 PM -
I can't seem to pass a file through a parameter
By daletron3030 in forum New To JavaReplies: 5Last Post: 01-15-2009, 06:07 AM -
unable to pass value from one class to another
By ddatta8 in forum New To JavaReplies: 14Last Post: 12-28-2008, 02:24 PM -
Pass by ref. A work around?
By diRisig in forum New To JavaReplies: 0Last Post: 02-05-2008, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks