Solved

Passing data from a servlet to a jsp

Posted on 2002-05-23
7
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

24 Experts available now in Live!

Get 1:1 Help Now