Results 1 to 11 of 11
  1. #1
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

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

    ** This space for rent **

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default 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") %>">

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

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

    ** This space for rent **

  5. #5
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Default 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

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default 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:

    Java Code:
    <input type="hidden" name="user" value="<% out.print(session.getAttribute("username"); %>"/>
    Or you can also use an EL (Expression Language)

    Java Code:
    <input type="hidden" name="user" value="${sessionScope["username"]}"/>
    If error, what was the error message?
    Last edited by wsaryada; 09-04-2012 at 10:56 AM.

  7. #7
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Default 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"));
    Java Code:
    ResultSet rs = s.executeQuery( "select * from userdata where id='"+ session.getAttribute("username") +"';");
    Though there are no errors, the value being passed is 'null'

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default 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?

  9. #9
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Default 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 :)

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

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

    ** This space for rent **

  11. #11
    megabull is offline Member
    Join Date
    Aug 2012
    Posts
    22
    Rep Power
    0

    Default Re: How do i pass parameters from one page to the other

    Oh. Din't knew that. Thanks Tolls

Similar Threads

  1. pass parameters from jsp to another jsp
    By chathura chaminda in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-21-2012, 10:37 AM
  2. How to pass parameters to frames
    By nino23 in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2011, 09:39 PM
  3. Replies: 5
    Last Post: 11-21-2010, 02:08 PM
  4. how to use picturebox and pass parameters to a url
    By Nubkadiya in forum New To Java
    Replies: 5
    Last Post: 06-02-2010, 02:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •