Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5923
  • Last Modified:

sharing data between two jsp pages using session

Hi

what i want to do is to take a value from a text field in a jsp page and put the value in a vector then put the vector in a session object the retrieve the value inside the vector from the other jsp page all of this using the session thing

am almost there but i cant do it

thanks
0
YASAR_SAID
Asked:
YASAR_SAID
  • 7
  • 4
  • 2
2 Solutions
 
astorerCommented:

Embed in your JSP - to add to the vector:
Vector v = (Vector) (session.getAttribute("mystuff"));
if (v == null)
    v = new Vector();
v.add(myValue);
session.setAttribute("mystuff", v);

Embed in your JSP - to read from the vector:
Vector v = (Vector) (session.getAttribute("mystuff"));

remember to check for null before using v.

There are more elegant ways to do this if you are using Struts MVC framework but that's the basic idea.

Andrew
0
 
YASAR_SAIDAuthor Commented:
Hi again

i tried just like you said but am gitting this error :

Generated servlet error:
Vector cannot be resolved to a type


and this is the part where i submit the values :

<%

String name = null;
name = request.getParameter("description1");
    //int g=Integer.parseInt(x);
Vector v = new Vector();
v.add(name);    

HttpSession session1 = request.getSession(true);

session1.setAttribute("infos",v);
%>


and this is the part where i recieve the values :

<%

HttpSession session1 = request.getSession(true);

Vector v2 = (Vector) (session.getAttribute("infos"));


%>

beside the error how i ll be able to print the value inside the vector in a table for example is it gonna be like this :

<td width="33%" align="center" bgcolor="#0099FF"><font size="2"><% My value %></font></td>

and thanks
0
 
jagadeesh_motamarriCommented:
ok.....

First and foremost thing you got to do is include the java.util.Vector in your jsp page
i.e somthing like this -

<%@ page import="java.util.Vector.*" %>

And secondly inorder to retrieve the values that are there in a vector you got to iterate on the values of the vector and put them in a table.

IF

Vector v2 = (Vector) (session.getAttribute("infos"));

is where you collect the values.....then u can iterate using Iterator class

this way -

 *         Iterator it = v2.iterator();
 *         while (it.hasNext())
 *         {
 *             out.write(it.next());
 *         }

......This way you print the value of the Vector.

_____Jags.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
YASAR_SAIDAuthor Commented:
everything now is ok i think but am getting this error now

The method write(int) in the type Writer is not applicable for the arguments (Object)

0
 
jagadeesh_motamarriCommented:
Ok.....I assume you are following what i've suggested.

instead of out.write(it.next()) >> you can use <%=it.next()>

or else -

if u still want to use the out.write() >> you may need to change it to

out.write((String)it.next())

_____Jags.
0
 
jagadeesh_motamarriCommented:
Oops....what did i say.....

>>out.write((String)it.next())

This doesn't work....i thought i need to convert the object into string.

0
 
jagadeesh_motamarriCommented:
Refer this if u still need to convert -

http://forum.java.sun.com/thread.jspa?threadID=493613&tstart=0

0
 
YASAR_SAIDAuthor Commented:
i think the session thing is just working fine with me and am passing the parametre but i found out what exactly is causing the problem the problem is when i get the value form the control textfield in the first page in this line

String name = request.getParameter("textfield");

it is not actually taking taking the value because in the second page it is vewing NULL so i think
the problem in the first page when am using the request thing

so now my question
1.does it matter the place of the jsp code in the html page  
2. when exactly  the jsp code  ia executed ( in the page load in the page refresh ....)

please help me with this because i am getting sick of this problem

thanks
0
 
YASAR_SAIDAuthor Commented:
please anyone

thanks
0
 
jagadeesh_motamarriCommented:
ok.....!!

How are you passing the value of the 'name' to the other page ? Is it using the request.setAttribute(name)?

What do you mean by >>.does it matter the place of the jsp code in the html page ?


_____Jags.
0
 
jagadeesh_motamarriCommented:
And more importantly where is this line of code >> String name = request.getParameter("textfield"); is it the one in you servlet? or u are using only 2 jsp and no servlets?
0
 
jagadeesh_motamarriCommented:
Well let me give you a sample code snippet that should help you...

Hi

I have created a small example which illustrate how to pass values from one jsp page to another.  

Input.jsp creates a form where you can enter your firstname.  It will also put the original firstname in the session as a attribute so it can be retrieved in check.jsp

check.jsp checks if the two names (original name: Mike) are equal.  If not, check.jsp should do some extra error checking.  If yes, check.jsp use RequestDispatcher to forward the orginal request (incl. all parameters) to output.jsp

finally, output.jsp shows the name entered in the form (input.jsp)


1. input.jsp

<%
    String firstName = "Mike";
    session.setAttribute("firstName", firstName); // old value
%>

<form method="post" action="/check.jsp">
    <input type="text" name="firstName" value="<%=firstName%>">
    <input type="submit" value="send">
</form>


2. check.jsp
<%

    //You should use a formHolder
    String firstName =  (String) session.getAttribute("firstName"); // old value
    String formFirstName = (String) request.getParameter("firstName"); // value comes from form

    out.println (firstName + "  " + formFirstName);

    if (!firstName.equals(formFirstName)) {
        out.println ("names not equal, you should do something...");
        // do something
    }
    else {

      // forward to output.jsp and keep all request attributes/parameters (incl. form parameters)
      RequestDispatcher rd = request.getRequestDispatcher("/output.jsp");
      rd.forward(request, response);
    }

%>

Output:
<br>
<%
    out.println ("firstName: " + request.getParameter("firstName"));
%>

..........
Source URL:http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21105625.html

Hopes this helps
_____Jags.
0
 
astorerCommented:
I think I gave the answer to the basic question but jagadeesh_motamarri did see this through to completion.
I'd suggest that jagadeesh_motamarri have the accepted answer and I gave an assist.  jagadeesh_motamarri should get the majority of the points.

Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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