Solved

JSP ATTRIBUTES

Posted on 2001-08-13
18
389 Views
Last Modified: 2013-11-23
Hi all,

Had a question. woudl appreciate help plz..


I have a JSP PAGE..

IT HAS A HTML Form with a update button (say a.jsp)

onclick of update .. it calls b.jsp..

now b.jsp validates form values in a.jsp ...and puts in database..and then

i do..

response.sendRedirect("a.jsp?update="+update);

in b.jsp so that a.jsp refreshes..

what I want to do is..

in b.jsp.. like I have query string.

update = ...

I want to pass some name-value pair to a.jsp

so that I can get a value of it in a.jsp

I know I can do this..


response.sendRedirect("a.jsp?update="+update + "&name1="+value1..etc);

but I don't want to do this because I have some for loop for getting this name, value pair..

I want to do something like:

pageContext.setAttribute("name_"+i,value,PageContext.REQUEST_SCOPE);

I tried to do this in b.jsp  

and then in a.jsp like ..


 out.println("num 0: " + pageContext.getAttribute("name_0",PageContext.REQUEST_SCOPE));

I am getting null value for name_0 ..

any help plz


0
Comment
Question by:irodov
  • 10
  • 6
  • 2
18 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 6381841
You can try using:
<jsp:forward page="relativeURL">
  <jsp:param name="name" value="value"/>
</jsp:forward>

But it seems in your case a Servlet doing the forward seems like the appropriate approach as you can attach attributes to the request object and forward to a JSP.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6381859
servlet code would be:
   RequestDispatcher rd = null;
   String targetURL = "b.jsp";
request.setAttribute("name", value);
request.setAttribute("name2", value2);
   ServletContext ctx = this.getServletContext();
   rd = ctx.getRequestDispatcher(targetURL);
   rd.forward(request, response);

b.jsp would retrieve the data using usebean and scope=request

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6381868
from orielly web site:
Passing Data Between Pages Processing the Same Request
Quite often, more than one JSP page is used to process a single request. One example is when you use the include action to include a common navigation bar in all pages. Another example is when you use one page for request processing (e.g., input validation and database access) and then use the forward action, <jsp:forward page="pagename.jsp" />, to let another page generate the response. There are two ways to pass data from one page to another: using request parameters or request attributes.

First of all, the page invoked using a <jsp:include> or <jsp:forward> action has access to all HTTP request parameters that were sent to the first page. In addition, you can specify new request parameters using nested <jsp:param> actions:


  <jsp:forward page="login.jsp">
      <jsp:param name="errMsg"
       value="The name or password is not valid" />
  </jsp:forward>

The target page has access to the all request parameters the same way, no matter if they are original or additional parameters.

Request parameters can only hold string values. If you need to pass an object, say a UserInfoBean with various user information properties, you need to pass it as a request attribute instead (or as a session or application scope bean, but let's ignore that for now). A request attribute is the same thing as a request scope object, so the first page can create the object and set all its properties with the <jsp:useBean> and <jsp:setProperty> actions:


  <jsp:useBean id="userInfo" scope="request"
     class="com.mycompany.UserInfoBean">
     <jsp:setProperty name="userInfo" property="*" />
  </jsp:useBean>
  ...
  <jsp:forward page="nextpage.jsp" />

In order to use the bean in a page invoked through <jsp:forward> or <jsp:include>, you must use the <jsp:useBean> action in the invoked page as well. This is so that the bean created by the first page can be located and associated with the specified id. It can then be used by scripting code or other actions, such as <jsp:getProperty>, in the new page:


  <jsp:useBean id="userInfo" scope="request"
     class="com.mycompany.UserInfoBean" />
  <jsp:getProperty name="userInfo" property="firstName" />


CJ
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:irodov
ID: 6381977
0
 

Author Comment

by:irodov
ID: 6381980
0
 

Author Comment

by:irodov
ID: 6381984
0
 

Author Comment

by:irodov
ID: 6381988
0
 

Author Comment

by:irodov
ID: 6382024
0
 

Author Comment

by:irodov
ID: 6382018
0
 

Author Comment

by:irodov
ID: 6382193
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 6382614
hi !
in b.jsp
    <%
    HttpSession ses = request.getSession(false);
    ses.setAttribute("name_0","a");
    response.sendRedirect(response.encodeURL("a.jsp"));
    %>
   
in a.jsp
    <%
    HttpSession ses = request.getSession(false);
    %>
    result = <%=ses.getAttribute("name_0")%>
0
 

Author Comment

by:irodov
ID: 6419907
ses.getAttribute(string).  is this a method. I am getting following error:

Method getAttribute(java.lang.String) not found in interface javax.servlet.http.HttpSession. String result = ses.getAttribute("name_0");

Thanks
0
 

Author Comment

by:irodov
ID: 6419915
Its probably only in JAVA2.2 .. I don't have that version on my server...

Any other suggestion plz?

regards
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6420034
getAttribute is Servlet 2.3 spec.

Its getValue in earlier Servlet specs.

CJ
0
 

Author Comment

by:irodov
ID: 6420175
so u mean i can use getValue to get the funtionality I require?
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 6420553
what servlet engine u r using ??
perhaps what cheekycj suggestion might work for u...
as long as u manage to stored the valua u want...:)

cheers...
Sendoh
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 248 total points
ID: 6421830
Yes it depends on what Servlet spec your servlet engine is adhering to.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6828485
Thanx for the "A".
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

815 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

11 Experts available now in Live!

Get 1:1 Help Now