?
Solved

sharing data between two jsp pages using session

Posted on 2006-06-29
16
Medium Priority
?
5,469 Views
Last Modified: 2008-03-17
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
Comment
Question by:YASAR_SAID
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
16 Comments
 
LVL 4

Accepted Solution

by:
astorer earned 1000 total points
ID: 17013538

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
 

Author Comment

by:YASAR_SAID
ID: 17014958
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
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 17018022
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:YASAR_SAID
ID: 17021778
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
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 17022003
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
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 17022008
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
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 17022009
Refer this if u still need to convert -

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

0
 

Author Comment

by:YASAR_SAID
ID: 17025196
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
 

Author Comment

by:YASAR_SAID
ID: 17035950
please anyone

thanks
0
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 17037159
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
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 17037173
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
 
LVL 10

Assisted Solution

by:jagadeesh_motamarri
jagadeesh_motamarri earned 1000 total points
ID: 17037215
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
 
LVL 4

Expert Comment

by:astorer
ID: 17227990
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

800 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