• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

JSP ATTRIBUTES

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
irodov
Asked:
irodov
  • 10
  • 6
  • 2
1 Solution
 
cheekycjCommented:
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
 
cheekycjCommented:
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
 
cheekycjCommented:
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
irodovAuthor Commented:
0
 
irodovAuthor Commented:
0
 
irodovAuthor Commented:
0
 
irodovAuthor Commented:
0
 
irodovAuthor Commented:
0
 
irodovAuthor Commented:
0
 
irodovAuthor Commented:
0
 
SendohCommented:
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
 
irodovAuthor Commented:
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
 
irodovAuthor Commented:
Its probably only in JAVA2.2 .. I don't have that version on my server...

Any other suggestion plz?

regards
0
 
cheekycjCommented:
getAttribute is Servlet 2.3 spec.

Its getValue in earlier Servlet specs.

CJ
0
 
irodovAuthor Commented:
so u mean i can use getValue to get the funtionality I require?
0
 
SendohCommented:
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
 
cheekycjCommented:
Yes it depends on what Servlet spec your servlet engine is adhering to.

CJ
0
 
cheekycjCommented:
Thanx for the "A".
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 10
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now