Solved

Passing data from a servlet to a jsp

Posted on 2002-05-23
7
263 Views
Last Modified: 2010-04-01
Hello,
 I would like pass some data that a servlet gets to a jsp page. Could some body show me some sample code how to do this.
Ramani
0
Comment
Question by:ramani081399
  • 5
7 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 7029773
there are three approaches.

1. use cookies.
2. use session vars.
3. use request object.

I will show code examples of 2 and 3.

2:
in the servlet add this:
HttpSession session = request.getSession(true);
session.setAttribute("SOME_KEY", theVariable);

now redirect or forward to jsp.

in the JSP you can retrieve the value using:
varType varName = (varType) session.getAttribute("SOME_KEY");

3:
In the servlet add this:

request.setAttribute("SOME_KEY", theVariable);
getServletContext().getRequestDispatcher("/jsp/jsptoforwardto.jsp").forward(request, response);

now in your JSP you can retrieve the value using:

varType varName = (varType) request.getAttribute("SOME_KEY");

HTH,
CJ
0
 
LVL 1

Expert Comment

by:raid999
ID: 7031860
i really dont understand you fully but if you put this in a jsp page
<jsp:include page="../Servlet/nameofservlet"/>
and make that servlet to display in html format then the user opening that jsp page will also get the result from the servlet if that what you mean

but if you mean just passing a variable then in the servlet get the variable and then use
request.sendRedirect("http://www.yoursite.com/filename.jsp?variable="+variablename);

___________
cheers
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7032463
raid99: that will only work for scalor values what about arrays or date or any other object?

CJ
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 19

Expert Comment

by:cheekycj
ID: 7075694
any updates for us?

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7076055
Glad I could help and Thank you for the "A"

CJ
0
 
LVL 2

Expert Comment

by:climbingjaffa
ID: 13616211
Hi just confused where you put this in the jsp

" varType varName = (varType) request.getAttribute("SOME_KEY");2



0
 
LVL 19

Expert Comment

by:cheekycj
ID: 13618341
Hi climbingjaffa : you should open your own question for this but to answer simply:
You can put it anywhere in the JSP as long as it is before any code that references the jsp.

I usually like to put my var retrievals at the top like

<%
varType varName = (varType) request.getAttribute("SOME_KEY");
%>
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

SEO can be a real minefield to navigate, but there are three simple ways to up your SEO game just be re-assessing your content output.
An article on effective troubleshooting
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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