Solved

JSP ATTRIBUTES

Posted on 2001-08-13
18
386 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:irodov
Comment Utility
0
 

Author Comment

by:irodov
Comment Utility
0
 

Author Comment

by:irodov
Comment Utility
0
 

Author Comment

by:irodov
Comment Utility
0
 

Author Comment

by:irodov
Comment Utility
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:irodov
Comment Utility
0
 
LVL 3

Expert Comment

by:Sendoh
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
getAttribute is Servlet 2.3 spec.

Its getValue in earlier Servlet specs.

CJ
0
 

Author Comment

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

Expert Comment

by:Sendoh
Comment Utility
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
Comment Utility
Yes it depends on what Servlet spec your servlet engine is adhering to.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
Comment Utility
Thanx for the "A".
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 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

19 Experts available now in Live!

Get 1:1 Help Now