?
Solved

access same variable in multiple jsp pages

Posted on 2004-04-04
21
Medium Priority
?
371 Views
Last Modified: 2010-04-01
Hi,This is a basic form contained in form.jsp.
<form name="Form1" method="POST" action="new.jsp">

In new.jsp im using this line to access the variable name from form.jsp.
 String Name = request.getParameter("Name");

How can I access the variable name from other jsp pages?
Thanks.
0
Comment
Question by:Ronayne
[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
  • 12
  • 9
21 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10751871
I would suggest to make a simple Java Bean and then read in the values from there.

MyBean.java ...

package myPackage;

public class MyBean {
  private String name;
  public MyBean() {
    name = "";
  }
  public String getName() {
    return name;
  }
 
  public void setName(String x) {
    name = x;
  }
}

in your new.jsp you would do this..

<jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />
String UserName = request.getParameter("Name");
<jsp:setProperty name="firstBean" property="name" value="UserName" />


Then on anyother jsp page you would use this line..
<jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />

and then to get the value you do this..
<jsp:getProperty name="firstBean" property="name" />

when using beans there are 3 things to keep in mind...

1)   you need a package.
      add this statement to all your .java files.
      package myPackage;

2)   you must call your bean like this.....
      <jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />

3)   You must be using this directory structure

-jspfiles
-WEB-INF
   -classes
       -myPackage
            -MyBean.class
Cheers,
Ghost
0
 

Author Comment

by:Ronayne
ID: 10751937
>>and then to get the value you do this..
<jsp:getProperty name="firstBean" property="name" />

So the value will be stored in name?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10751972
well what's happening is your sending the variable to the java bean. the rules for javabeans are that if the method is
getName()...
the the variable must be name..
that's the way to do it.

you are then retriving the varible from the java bean.
is that clearer?
Ghost
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!

 

Author Comment

by:Ronayne
ID: 10751981
>><jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />
String UserName = request.getParameter("Name");
<jsp:setProperty name="firstBean" property="name" value="UserName" />

Could I not just do this on any jsp?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10751997
umm no. you must do that code on the page after the form because...

request.getParameter("Name");

gets the value from the form. So it has to be on the following page.

this part
<jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />
<jsp:getProperty name="firstBean" property="name" />

can be done on any jsp, but remember that you have to set the variable before you can use it :)
cheers,
Ghost
0
 

Author Comment

by:Ronayne
ID: 10752040

ok, so the value will be stored in "name" and I can use this like i'd use any java variable?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10752051
yeah I guess...

if you want to change it you will have to do...

<jsp:setProperty name="firstBean" property="name" value="newValue" />

if you want to get it you have to do..

<jsp:getProperty name="firstBean" property="name" />

is that what you mean?
Ghost
0
 

Author Comment

by:Ronayne
ID: 10752072

so I can just go : stmt = select * from table where names like "name";

0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 200 total points
ID: 10752100
ok to do that you would have to do this...

first declare bean..
<jsp:useBean class="myPackage.MyBean " id="firstBean" scope="session" />
next you would do this...

stmt = select * from table where names like %><jsp:getProperty name="firstBean" property="name" /><%;

does that make more sense?
ghost
0
 

Author Comment

by:Ronayne
ID: 10752191

ah now I see, the fog has lifted... thanks a million
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10752195
:)
no problem,
glad I can help,
Anything else?
Ghost
0
 

Author Comment

by:Ronayne
ID: 10752215

Thats grand as long as I can use this : %><jsp:getProperty name="firstBean" property="name" /><% as  a variable is all I need.
Thanks again
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10752310
sure anytime.
if you need anything else I'll be here..
did you look at your xsl question, I posted a reply
Ghost
0
 

Author Comment

by:Ronayne
ID: 10752883

Ive seen your reply, im just kinda busy with other stuff, I havent had a chance to test it
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10752887
cool no problem
laters
0
 

Author Comment

by:Ronayne
ID: 10768438

im just after trying out youor comments and I cant seem to get it working, How can I print out the value like so: out.print:%><jsp:getProperty name="firstBean" property="name" /><%

Thanks
0
 

Author Comment

by:Ronayne
ID: 10775370

its ok, i got it
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10775675
sorry I didn't get back to you fast enough,
glad it works though
cheers,
Ghost
0
 

Author Comment

by:Ronayne
ID: 10778877

Hi ghost, im just having trouble with this one line, I just want to put idnum into the result but its not working

This is my code:
String inServer = "localhost";
  String inDBName = "test";
  String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;
 
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  Connection con = DriverManager.getConnection(DATABASE_URL, "username", "password");
  Statement stmt = con.createStatement();
   
 ResultSet rs1 = stmt.executeQuery("select idnum from userdesc where names like %><jsp:getProperty name="AccessV" property="name" /><%");

This is the error:

C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\seanproj\main_jsp.java:77: unclosed string literal
 ResultSet rs1 = stmt.executeQuery("select idnum from userdesc where names like
                                   ^

Thanks

0
 

Author Comment

by:Ronayne
ID: 10778974

That should be:

ResultSet rs1 = stmt.executeQuery("select idnum from userdesc where names like
0
 

Author Comment

by:Ronayne
ID: 10779213

its ok again... i just got it there.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Check out the latest tech news, community articles, and expert highlights in August's newsletter.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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