Link to home
Start Free TrialLog in
Avatar of Raftor
Raftor

asked on

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
Avatar of astrohelp
astrohelp

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"); %>
Avatar of Raftor

ASKER

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.
Have you inported the drivers on the second page?
How about you list your code
<%@ 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
Avatar of Raftor

ASKER

good stuff ill see what i can do and get back to you.
Avatar of Raftor

ASKER

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>
Avatar of Raftor

ASKER

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

Avatar of Raftor

ASKER

ok cheers ill see what i can do with that.
Avatar of Raftor

ASKER

sorry but im gonna need your expert help on how to use that hidden form field.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Raftor

ASKER


>>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?
> response.sendRedirect("admin_home_page.jsp?strUserName=<%= strUserName

wheres the rest of the line?

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

ASKER

Its ok i figured it out you were missing quotes around  strUserName like this

response.sendRedirect("admin_home_page.jsp?strUserName="+strUserName+"");
doh, that'll teach me to start work before coffee :)
Avatar of Raftor

ASKER

can someone explain how hidden forms work and how storing values in sessions works?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Raftor

ASKER

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?
Avatar of Raftor

ASKER

top right sorry.
AFAIK, session is based on browser window, if the browser is closed, the session will be destroyed.
> Would the sessions be reset or would they remain for the next session?

the session would remain until it timed out.