Solved

JSP ATTRIBUTES

Posted on 2001-08-13
18
388 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
 

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
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.

 

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

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Delphi: Connect to running MS Outlook 4 48
How to create virtual printer while installing my application 5 22
ForLoop Example 3 40
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

26 Experts available now in Live!

Get 1:1 Help Now