Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

passing values to other pages

Posted on 2004-04-07
21
Medium Priority
?
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 180 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 220 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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