Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing data from a servlet to a jsp

Posted on 2002-05-23
7
Medium Priority
?
310 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
[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
  • 5
7 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Working from home is a dream for many people who aren’t happy about getting up early, going to the office, and spending long hours at work. There are lots of benefits of remote work for employees.
An overview of cyber security, cyber crime, and personal protection against hackers. Includes a brief summary of the Equifax breach and why everyone should be aware of it. Other subjects include: how cyber security has failed to advance with technol…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

610 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