passing values to other pages

in a html/jsp website how would you pass information from a 'login' page down several pages to a 'change password' page using jsp
RaftorAsked:
Who is Participating?
 
kokchoon78Commented:
how to use session in your case ?

1) in your login_successful.jsp just add this :

...
{
         String strUserName = request.getParameter("txtUsername");
         String strPassword = request.getParameter("txtPassword");    

          if (strUserName != null)
          {
             out.print("User Name: " +strUserName);
             //Add the strUserName to session with 'username' as the session key.
             session.setAttribute( "username", strUserName  );  
          }
...


2) In your admin_home_page.jsp

...
<!-- This is where i want to be able to retrieve the name from the previous page. -->
<%
    //String strAdministrator = request.getParameter("strUserName");
    //Get the strUsername from the session
    String strAdministrator = (String)session.getAttribute("username")
    out.println("strAdministrator is: " + strAdministrator);
%>
...

3) When you logout, just simply add this code in your logout.jsp

...
session.removeAttribute("username");
...
0
 
astrohelpCommented:
Page 1 needs a submit button

<INPUT TYPE=submit VALUE="Submit">
</FORM>

Page 2 needs a way to receive the information

<% String p_reportnum = request.getParameter("reportnum"); %>
0
 
RaftorAuthor Commented:
ive done that but passing the name on after that  is the problem because <% String p_reportnum = request.getParameter("reportnum"); %> doesnt seem to want to work for me.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
astrohelpCommented:
Have you inported the drivers on the second page?
How about you list your code
0
 
astrohelpCommented:
<%@ page import="java.sql.*,oracle.jdbc.driver.*, oracle.sql.*,java.io.*,javax.sql.*,java.awt.*,java.util.*"%>

This is the import string that I use for a lot of my projects
0
 
RaftorAuthor Commented:
good stuff ill see what i can do and get back to you.
0
 
RaftorAuthor Commented:
there are three jsp pages involved the first is login.jsp that sends the users information to a page where it is stored. The second page is admin_home_page.jsp where i want to have the following fields passed to.  These lines get the values from the login page successfully but passing them on is the problem.  
                String strUserName = request.getParameter("txtUsername");
            String strPassword = request.getParameter("txtPassword");      

The following is login_successful.jsp and admin_home_page.jsp respectively.

//////////////////////////////////////////////////////login_successful.jsp////////////////////////////////////////////////////////
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>

<%
      try
      {
            String strUserName = request.getParameter("txtUsername");
            String strPassword = request.getParameter("txtPassword");      

            if (strUserName != null)
            out.print("User Name: " +strUserName);

            int login = 0;
            String inServer = "localhost";
            String inDBName = "colin";
            String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(DATABASE_URL, "colinrafter", "keyhole");
            Statement stmt = con.createStatement();
            Statement state = con.createStatement();

            ResultSet rs1 = stmt.executeQuery("select password from members where userName like \""+strUserName+"\"");
                        
            String passwordtocompare = "";
            
            if (rs1.next())
            {
                   passwordtocompare = rs1.getString(1);

                   if((passwordtocompare.compareToIgnoreCase(strPassword) == 0) && (strUserName.compareToIgnoreCase("GHicks")==0))
                         login = 2;
                   else if(passwordtocompare.compareTo(strPassword) == 0)
                         login = 1;
                   else
                         login = 0;
            }
            if(login == 0)
                    response.sendRedirect("login_error.jsp");
            else if(login == 1)
                  response.sendRedirect("home_page.jsp");
            else if(login == 2)
                  response.sendRedirect("admin_home_page.jsp");
      }
      catch (ClassNotFoundException cnfe) {out.println(cnfe.toString());}
      catch (SQLException sqle) {out.println(sqle.toString());}  
      
%>

/////////////////////////////////////////////////////////////////admin_home_page.jsp//////////////////////////////////////

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>


<HTML>
<HEAD>
<TITLE> Administrator Home Page </TITLE>
</HEAD>
<CENTER>
<!-- This is where i want to be able to retrieve the name from the previous page. -->
<%
      String strAdministrator = request.getParameter("strUserName");
      out.println("strAdministrator is: " + strAdministrator);
%>


<IMG ALIGN="center" SRC="images/header.bmp">

<FONT SIZE="+2" COLOR="9400D3 "><B><U><P> Administrator Home Page </B></U></font>

  <div id=Layer1 style="HEIGHT: 244px; LEFT: 71px; POSITION: absolute; TOP: 213px; WIDTH: 137px; Z-INDEX: 1">
    <p><a href="admin_home_page.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a>
    </p>
    <p><a href="manage_current_requests.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a>
    </p>
    <p><a href="view_previous_requests.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a><a href="book_request_head.asp">
      </a></p>
    <p><a href="view_member_details.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a></p>
   
    <p><a href="manage_budgets.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a></p>
      
    <p><a href="manage_budgets.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a></p>
      
    <p><a href="change_password.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a></p>
      
    <p><a href="login.jsp"><img border=0 height=32 src="images/button.gif" width=105 img></a></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </div>
  <FONT color=#940d00 size=+2><B><U> </u></B></FONT> <FONT color=#000000 size=+0>
  <P>&nbsp;
</FONT><P><FONT color=#000000 size=+0>Welcome to the GMIT Administration Home Page </font><br><br>
Please choose one of the options from the left


  <P><A href="mailto:colinrafter@yahoo.com"><FONT color=blue>Web design by Colin Rafter 2003</font></A></P>
  </CENTER>
  </BODY>
  </HTML>
0
 
RaftorAuthor Commented:
Sorry i made a mistake.  I want to pass the user name from login_successful.jsp that originally retrieved the name from login.jsp and pass it to admin_home_page.jsp.
0
 
astrohelpCommented:
Ok I just glanced through it really quick, but you need to set a hidden field
<INPUT TYPE=hidden NAME=user VALUE="<%= strUsername %>">
then submit to the next page just like before

I have a meeting at 4:15 so i hope this helps, otherwise i may be back before 5:00, if not I will check your code further tomorrow!

0
 
RaftorAuthor Commented:
ok cheers ill see what i can do with that.
0
 
RaftorAuthor Commented:
sorry but im gonna need your expert help on how to use that hidden form field.
0
 
objectsCommented:
try passing them in the url:

response.sendRedirect("admin_home_page.jsp?strUserName=<%= strUserName %>");

or another approach would be to store them in the session.
0
 
RaftorAuthor Commented:

>>try passing them in the url:

it says:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\Project\login_success_jsp.java:59: unclosed string literal
            response.sendRedirect("admin_home_page.jsp?strUserName=<%= strUserName
                                                 ^
Is that the right format?
0
 
objectsCommented:
> response.sendRedirect("admin_home_page.jsp?strUserName=<%= strUserName

wheres the rest of the line?

response.sendRedirect("admin_home_page.jsp?strUserName=<%= strUserName %>");
0
 
RaftorAuthor Commented:
Its ok i figured it out you were missing quotes around  strUserName like this

response.sendRedirect("admin_home_page.jsp?strUserName="+strUserName+"");
0
 
objectsCommented:
doh, that'll teach me to start work before coffee :)
0
 
RaftorAuthor Commented:
can someone explain how hidden forms work and how storing values in sessions works?
0
 
RaftorAuthor Commented:
That code worked perfectly but i was wondering what would happen if someone didnt hit 'logout' and instead hit the close button on the top left of the page? Would the sessions be reset or would they remain for the next session?
0
 
RaftorAuthor Commented:
top right sorry.
0
 
kokchoon78Commented:
AFAIK, session is based on browser window, if the browser is closed, the session will be destroyed.
0
 
objectsCommented:
> Would the sessions be reset or would they remain for the next session?

the session would remain until it timed out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.