Solved

Passing data from a servlet to a jsp

Posted on 2002-05-23
7
268 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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
Apache server configuration 7 87
object methods vs static methods 2 100
some methods private some are not 8 218
by zero exception 10 52
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

861 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