Results 1 to 11 of 11
- 09-04-2012, 07:12 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 22
- Rep Power
- 0
How do i pass parameters from one page to the other
I have to pass a parameter as a value (which has been set using session.setAttribute) to a function. How should i do so. The point is that, it is displaying using session.getAttribute, but not passing as a string parameter. Any suggestions on how to do so
Here's the page
Java Code:<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.io.*" %> <%@ page import="java.lang.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Thanks for logging in</title> </head> <body> <% out.println("Hi " +(session.getAttribute("username")) + " Hope you're having a great day"); %> <p> <form method="post" action="login.jsp"> <input type="hidden" name="user" value="session.Attribute("username")"> <p><button value="Edit Data" onclick="window.location = 'info.jsp'"> Edit Data </button> </form> </body> </html>
- 09-04-2012, 09:57 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do i pass parameters from one page to the other
What are you seeing and what are you expecting to see?
What class is the attribute referenced by "username"?Please do not ask for code as refusal often offends.
- 09-04-2012, 10:13 AM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 698
- Rep Power
- 6
Re: How do i pass parameters from one page to the other
You should use a JSP expression get value from the session object before setting it as a value of the HTML input. For example:
Java Code:<input type="hidden" name="user" value="<%= session.getAttribute("username") %>">Website: Learn Java by Examples
- 09-04-2012, 10:40 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do i pass parameters from one page to the other
Ohhhhh!
I didn't even see the hidden field (which is slightly ironic)...Please do not ask for code as refusal often offends.
- 09-04-2012, 10:44 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 22
- Rep Power
- 0
Re: How do i pass parameters from one page to the other
@tolls : yes, "username' a session value that i've declared in the previous jsp page(which leads to this page that whose code you're seeing) using session.setAttribute . I'm trying to receive that and put it to some use. Actually, i need this value to match it with another variable.
Should i mention, on line 14, session.getAttribute("username") displays the correct value. The line 18 one passes 'null'. That's when i realized am not able to pass this as a parameter.
@wsaryada : <% %> Doesnt help. Tried it. Though i tried <%= as u suggested, caused an error.
The reason i chose session.setAttribute and session.getAttribute was that it wouldn't change for the session, so no issue.Last edited by megabull; 09-04-2012 at 10:49 AM. Reason: added details
- 09-04-2012, 10:48 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 698
- Rep Power
- 6
Re: How do i pass parameters from one page to the other
Are you sure it doesn't work? <%= session.getAttribute("username") %>. With no no semicolon at the end.
If you use a scriptlet it should be something like:
Or you can also use an EL (Expression Language)Java Code:<input type="hidden" name="user" value="<% out.print(session.getAttribute("username"); %>"/>
If error, what was the error message?Java Code:<input type="hidden" name="user" value="${sessionScope["username"]}"/>Last edited by wsaryada; 09-04-2012 at 10:56 AM.
Website: Learn Java by Examples
- 09-04-2012, 10:59 AM #7
Member
- Join Date
- Aug 2012
- Posts
- 22
- Rep Power
- 0
Re: How do i pass parameters from one page to the other
@wsaryada: Absolutely. Doesnt work. though the error was a one time show, the parameter value still shows 'null'. This page above redirects to info.jsp. Even there it shows 'null'. Here's the code for the line in info.jsp
Java Code:System.out.println(session.getAttribute("username"));Though there are no errors, the value being passed is 'null'Java Code:ResultSet rs = s.executeQuery( "select * from userdata where id='"+ session.getAttribute("username") +"';");
- 09-04-2012, 11:29 AM #8
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 698
- Rep Power
- 6
Re: How do i pass parameters from one page to the other
Do you mean the redirect to info.jsp is by pressing the Edit Data button on your page? This button is not submitting your form. So the value of name field will not be submitted. You are simply change the url to info.jsp. And also have you check that the session has a username attribute?
Website: Learn Java by Examples
- 09-04-2012, 12:09 PM #9
Member
- Join Date
- Aug 2012
- Posts
- 22
- Rep Power
- 0
Re: How do i pass parameters from one page to the other
Hey. i found the problem. Its with <% session.invalidate(); %>; . I had a button to invalidate session. I never thought that inspite of being within a javascript onClick action, <% session.invalidate(); %>; would execute as soon as it is read.
Thanks for your time guys :)
- 09-04-2012, 01:39 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do i pass parameters from one page to the other
The Java stuff executes onthe server.
It is not affected by Javascript at all (or anything on the client).
All you said to the server by putting in <% session.invalidate(); %> in the JSP is "at this point of processing the JSP page invalidate the session".Please do not ask for code as refusal often offends.
- 09-05-2012, 05:34 AM #11
Member
- Join Date
- Aug 2012
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
pass parameters from jsp to another jsp
By chathura chaminda in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-21-2012, 10:37 AM -
How to pass parameters to frames
By nino23 in forum AWT / SwingReplies: 1Last Post: 06-29-2011, 09:39 PM -
how to pass parameters from a method to another which accepts to parameters?possible?
By amrmb09 in forum Advanced JavaReplies: 5Last Post: 11-21-2010, 02:08 PM -
how to use picturebox and pass parameters to a url
By Nubkadiya in forum New To JavaReplies: 5Last Post: 06-02-2010, 02:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks