Solved

Passing data from a servlet to a jsp

Posted on 2002-05-23
7
246 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 26
Run the project 5 82
Set up Wildfly 10 8 325
equalIsNot  challenge 43 117
Hyena v12.2 is now available for downloading and is available in English, French, German and Spanish versions.
Find out what the Office 365 disclaimer function is, why you would use it and its limited ability to create Office 365 signatures.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now