Welcome to Dream.In.Code
Become a Java Expert!

Join 150,116 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,961 people online right now. Registration is fast and FREE... Join Now!




How to receive Attribute values from servlet on JSP Page

 
Reply to this topicStart new topic

How to receive Attribute values from servlet on JSP Page

shadowBean
17 Jun, 2008 - 07:05 PM
Post #1

New D.I.C Head
*

Joined: 13 Jun, 2008
Posts: 27


My Contributions
I have a servlet that sends values to a JSP page. On my JSP I use EL ${} to display values. All works ok. But I need to assign some values to variables
heer is my simplified code

servlet
CODE

req.setAttribute("Message" , Message);
req.setAttribute("arrayApp" , arrayApp);
req.setAttribute("insertId" , insertId );

RequestDispatcher dispatcher = req.getRequestDispatcher("Process.jsp");
dispatcher.forward(req,res);


JSP
CODE

<table>
<tr><td>${arrayApp[0]}</td></tr>
<tr><td>
<%=  request.getAttribute(insertId)  %>
</td></tr>
</table>


I am just trying to use insertId but after trying so many different ways not successful. Not only insertId (object from database) but I can't seem to receive any values other than by using EL.

The question is what is the object I need to use to receive values?

Regarding the insertId I used toString() to convert it to String before sending it. But result is the same.

Many Thanks
User is offlineProfile CardPM
+Quote Post

1lacca
RE: How To Receive Attribute Values From Servlet On JSP Page
18 Jun, 2008 - 12:13 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
<%= request.getAttribute(insertId) %>
should be
<%= request.getAttribute("insertId") %>
User is offlineProfile CardPM
+Quote Post

shadowBean
RE: How To Receive Attribute Values From Servlet On JSP Page
18 Jun, 2008 - 12:36 AM
Post #3

New D.I.C Head
*

Joined: 13 Jun, 2008
Posts: 27


My Contributions
Thanks I tried that first thing. Nothing. I can only get the value from ${insertId} but not <%= request.getAttribute("insertId") %>
Don't know why.

Thanks again
User is offlineProfile CardPM
+Quote Post

1lacca
RE: How To Receive Attribute Values From Servlet On JSP Page
18 Jun, 2008 - 01:02 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Could you post the whole JSP and the part of the servlet from declaring insertId to setting it into the request?
User is offlineProfile CardPM
+Quote Post

shadowBean
RE: How To Receive Attribute Values From Servlet On JSP Page
22 Jun, 2008 - 02:15 AM
Post #5

New D.I.C Head
*

Joined: 13 Jun, 2008
Posts: 27


My Contributions
I got it thanks,
silly mistake

However, How do I assign this Attribute Value to another variable

ie. String s = session.getAttribute("s") ;

when session.getAttribute() is an object how do I convert it to assign it to String or interger

Thanks again

This post has been edited by shadowBean: 22 Jun, 2008 - 02:27 AM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: How To Receive Attribute Values From Servlet On JSP Page
23 Jun, 2008 - 01:57 AM
Post #6

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
You need to cast it:
String s = (String)session.getAttribute("s") ;
It'll work for null values as well, so don't forget to check them before calling methods on them (to avoid null pointer exceptions)
User is offlineProfile CardPM
+Quote Post

shadowBean
RE: How To Receive Attribute Values From Servlet On JSP Page
24 Jun, 2008 - 01:00 AM
Post #7

New D.I.C Head
*

Joined: 13 Jun, 2008
Posts: 27


My Contributions
thanks,

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:12AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month