Solved

passing values to other pages

Posted on 2004-04-07
21
493 Views
Last Modified: 2008-02-07
in a html/jsp website how would you pass information from a 'login' page down several pages to a 'change password' page using jsp
0
Comment
Question by:Raftor
  • 11
  • 4
  • 4
  • +1
21 Comments
 
LVL 2

Expert Comment

by:astrohelp
ID: 10777930
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
 

Author Comment

by:Raftor
ID: 10777955
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
 
LVL 2

Expert Comment

by:astrohelp
ID: 10777967
Have you inported the drivers on the second page?
How about you list your code
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:astrohelp
ID: 10777979
<%@ 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
 

Author Comment

by:Raftor
ID: 10777993
good stuff ill see what i can do and get back to you.
0
 

Author Comment

by:Raftor
ID: 10778103
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
 

Author Comment

by:Raftor
ID: 10778123
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
 
LVL 2

Expert Comment

by:astrohelp
ID: 10778154
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
 

Author Comment

by:Raftor
ID: 10778162
ok cheers ill see what i can do with that.
0
 

Author Comment

by:Raftor
ID: 10778600
sorry but im gonna need your expert help on how to use that hidden form field.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 45 total points
ID: 10779004
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
 

Author Comment

by:Raftor
ID: 10779082

>>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
 
LVL 92

Expert Comment

by:objects
ID: 10779155
> response.sendRedirect("admin_home_page.jsp?strUserName=<%= strUserName

wheres the rest of the line?

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

Author Comment

by:Raftor
ID: 10779222
Its ok i figured it out you were missing quotes around  strUserName like this

response.sendRedirect("admin_home_page.jsp?strUserName="+strUserName+"");
0
 
LVL 92

Expert Comment

by:objects
ID: 10779252
doh, that'll teach me to start work before coffee :)
0
 

Author Comment

by:Raftor
ID: 10779429
can someone explain how hidden forms work and how storing values in sessions works?
0
 
LVL 4

Accepted Solution

by:
kokchoon78 earned 55 total points
ID: 10781472
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
 

Author Comment

by:Raftor
ID: 10783528
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
 

Author Comment

by:Raftor
ID: 10783530
top right sorry.
0
 
LVL 4

Expert Comment

by:kokchoon78
ID: 10784594
AFAIK, session is based on browser window, if the browser is closed, the session will be destroyed.
0
 
LVL 92

Expert Comment

by:objects
ID: 10787632
> Would the sessions be reset or would they remain for the next session?

the session would remain until it timed out.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with Pie chart in Jsp 20 223
dateFashion java challenge 9 146
Clear browser cache on site login, is it possible? 3 33
Use tracing facilities in your browser steps 2 122
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question