Need Help: how can i pass value in one page of jsp to another?

Hi all,

I have two jsp pages as below:

test1.jsp

I try to get the vaule from my textbox by using:

String strUser = request.getParameter (“strUserName”);

Then i print out by using: out.print(strUser); then i can get the value and put on my page (For example, i get ABC on my page).

test2.jsp

Next, i want to get the value from strUser (which mean that the one i already display on page in test1.jsp, ABC) to insert into my table by using INSERT INTO statement. Then i try by using

String strUser1 = request.getParameter (“strUser”);

Is it possbile for me to do that? I cannot get anything to insert into my table. Then i tried out.print(strUser1); then i found that i get NULL value.


So is it possible for me to do that?? Or could you please advise how can i process it?

B.Regards,
Kimsan

Hi,

test1.jsp


I try to get the vaule from my textbox by using:

String strUser = request.getParameter (“strUserName”);

Ok. But, I’m not sure what you mean by this:

Then i print out by using: out.print(strUser); then i can get the value and put on my page (For example, i get ABC on my page).

First, out.print() looks like something you would use in a servlet to output html–not something you would use in a .jsp page. Second, out.print() wouldn’t be needed to get the value–you already obtained the value using request.getParameter(). If you want to display the value on a .jsp page, you could do something like this:

<div>You entered the name: [COLOR=Blue]<%= strUser %>[/COLOR]</div>

To get the value on your second .jsp page, you can use sessions to save the value from the first .jsp page:

test1.jsp
---------
<%
String strUser = request.getParameter("strUserName");
[COLOR=Blue]session.setAttribute("myUser", strUser);[/COLOR]
%>

<div>User: <%= strUser %></div>

You can retrieve the value on the second .jsp page like this:

test2.jsp
---------
<%
[COLOR=Blue]String thisUser = (String) session.getAttribute("myUser");[/COLOR]
%>

<div>The user is: <%= thisUser %></div>

Hi,

Thanks you very much for your help. I can move my data around on my page now. However, i have another problems while i put it with many rows of data. I called the data from my microsoft access which has many rows and columns. While i try moving my data round then i can get only the value the last row. I try with the following code that display data from my table:


String strUsername = request.getParameter(“username”);
session.setAttribute(“myUserName”, strUsername);
.
.
.
.

while(myResult1.next())

	{
	
	[B]String  strProName = myResult1.getString(1);
	session.setAttribute("myProName", strProName);[/B]		
	out.print("&lt;table border=1 CELLSPACING=0&gt;");
	
	out.print("&lt;TR&gt;");
	out.print("&lt;TD width = 200&gt;");
	
	[B]out.println(strProName);[/B]
	
	out.print("&lt;/TD&gt;");
	out.print("&lt;TD width = 200&gt;");
	out.println(myResult1.getString(3));
	out.print("&lt;/TD&gt;");
	out.print("&lt;TD width = 200&gt;");
	out.println(myResult1.getString(2));
	out.print("&lt;/TD&gt;");
	out.print("&lt;TD width = 200&gt;");
	out.println(myResult1.getString(4));
    out.print("&lt;/TD&gt;");
    out.print("&lt;TD&gt;");
	out.print("&lt;form action=bid_page.jsp method=post&gt;");
	out.print("&lt;input type=text name=place_bid&gt;");
	out.print("&lt;input type=submit name=okfunc value=Bid&gt;");
	out.print("&lt;/TD&gt;");
	out.print("&lt;/form&gt;");
	
	out.print("&lt;/TR&gt;");
	out.print("&lt;/table&gt;");
	
	}
	out.println("&lt;/font&gt;");
	out.print("&lt;/center&gt;");		

On my other page i used the following code to call the data from the above code and insert into my table:


<HTML>
<HEAD>
<TITLE>Welcome to the online Auction…</TITLE>
</HEAD>
<BODY>

<%@ page language =“java” import = “java.io." import = "java.lang.” import = “java.sql.*” %>
<% try

{

[B]String thisUserName = (String) session.getAttribute("myUserName");
String thisProName = (String) session.getAttribute("myProName");[/B]	
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection myConn = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Auction/Auction.mdb");

PreparedStatement myStatement = myConn.prepareStatement("INSERT INTO tblHistory VALUES ('"+thisUserName+"', '"+thisProName+"', '"+request.getParameter("place_bid")+"')");
	
	myStatement.executeUpdate();
	myConn.commit();
	myStatement.close();
	myConn.close();

}

	catch(Exception e){}

%>

</BODY>
</HTML>


Could you please advise what are the problems with the above code?

I am so sorry my above code is a bit messy, because i am still very new to this language.

Thanks you very much for your advise and any further advise you may give to me.

Best Regards,

Kimsan

If you do this:


String myProName = "a";
myProName = "b";
myProName = "c";

String thisProName = myProName;
System.out.print(thisProName);
System.out.print(thisProName);
System.out.print(thisProName);

will it output “abc”?

Hi,

I already tried with the code, but i get blank value on my page.

Best Regards,
Kimsan

Hi,

I already tried with the code, but i get blank value on my page.

It was not code that was intended for you to put on your page. It was code intended for you to look at, and then use to answer my question. If you don’t know what simple code like that does(or how to execute it as a stand alone java application), then you need to get a beginning java book and study it. You can’t program servlets and jsp’s without knowing java.