Solved

JSP ATTRIBUTES

Posted on 2001-08-13
18
390 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 54
jmss example java 2 23
Show tab when enter div and change underline color 3 62
Eclipse for Java EE development 2 27
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

839 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