Solved

sharing data between two jsp pages using session

Posted on 2006-06-29
16
5,078 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:YASAR_SAID
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Expert Comment

by:jagadeesh_motamarri
Comment Utility
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
Comment Utility
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
Comment Utility
please anyone

thanks
0
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

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).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
scoreUp challenge 14 47
Device same like our heart 12 42
Path to Python 9 35
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

10 Experts available now in Live!

Get 1:1 Help Now