Solved

sharing data between two jsp pages using session

Posted on 2006-06-29
16
5,295 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
  • 7
  • 4
  • 2
16 Comments
 
LVL 4

Accepted Solution

by:
astorer earned 250 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
Technology Partners: 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!

 

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 250 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

Technology Partners: 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!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

733 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