Solved

Servlet argument passing

Posted on 2003-11-12
10
615 Views
Last Modified: 2013-11-24
Hi all. How can I pass arguments between servlets? I read arguments from HTML forms, but I can't find any resources on passing between servlets. E.g. via the url string with the '?' part...

0
Comment
Question by:piou
[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
  • 4
  • 4
  • 2
10 Comments
 
LVL 1

Accepted Solution

by:
arunec earned 75 total points
ID: 9730081
Hi,
You can use :

In your servlet1 :

session.setAttribute("Test1", Test1 );
session.setAttribute("Test2", Test2 );

In your servlet2 :

String l1 = (String)session.getAttribute("Test1");
String l2= (String)session.getAttribute("Test2");

or u can use RequestDispatcher

request.setAttribute("test1", test1);    
   
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher();
dispatcher.forward(request, response);

Regards,
arunec
0
 

Author Comment

by:piou
ID: 9730364
Thanks arunec. I was looking for something more simple, related to the getQueryString() method. Is there any automation implemented? Or should I just build *manually* the URLs so that the contain a tail after the '?', and then process that with a StringTokenizer...?

0
 

Author Comment

by:piou
ID: 9730479
Ok, I think I'm closer. There are these methods:

getQueryString()
getParameterNames()
getParameterValues()

But the API docs are not very clear on their usage. I think the answer lies around these methods, but I cannot find proper resources.

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 7

Expert Comment

by:grim_toaster
ID: 9730531
If you can avoid it, it is best to leave sessions out of the picture (unless there is one there already).

I would suggest looking into the ServletRequest interface methods setAttribute, getAttribute and getParameter, depending on your needs.  It would also be best to avoid writing your own code to deal with the query string, as there is already code availabel.

After you've set the value required to the request object, you can dispatch to the other servlet as stated by arunec.
0
 

Author Comment

by:piou
ID: 9730708
It doesn't seem to work. Shouldn't I set the ServletResponse instead? But only the request has a setAttribute() method. Doesn't make sense...
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9731071
You should be setting the attribute to the ServletRequest.  Ah, one slight problem with Arumec's code, when acquiring the request dispatcher you will need to specify the path of the resource you are trying to access:

request.setAttribute("name", "value");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/relative/path/servlet");
dispatcher.forward(request, response);

Then in the servlet you dispatch the call to, do:
String value = request.getAttribute("name");
0
 
LVL 1

Expert Comment

by:arunec
ID: 9737128
yaa, i missed the path in RequestDispatcher,thanx grim
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9737781
No problem!  Oops, I also missed something in my code!  The request.getAttribute("name"); call returns an Object, this will need to be cast to a String.
0
 

Author Comment

by:piou
ID: 9737895
Thank you guys. I hope grim has no problem for arunec to take the points, being first...
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9746392
Nope, none whatsoever!  Sorry it's taken a while to reply!  I lost the link!
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to execute a Python program and gather return output in Java 2 63
Java: The Public Class Main 4 66
Java class and jar 3 61
Java ArrayList and if statement 2 55
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

739 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