5 Replies - 7718 Views - Last Post: 05 October 2005 - 07:07 AM Rate Topic: -----

#1 lilygrace  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 17-August 05

JSP: Check code please

Posted 03 October 2005 - 02:19 AM

Hi all... my friend and I were studying JSP and we were attemp to make a very simple user name login.

CODES:

form
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>LogIn Form</title>

<script language="javascript">
function ChkLogin() {
	var message;
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	
	if (username=="" || password=="") {
  if (username=="" && password=="") {
 	 message = "Please input your username and password.";
  } else if (username=="") {
 	 message = "Please input your username.";
  } else if (password =="") {	
 	 message = "Please input your password.";
  }
  
  alert(message);
  return false;
  
	} else {
  return true;
	}
}

</script>

<style type="text/css">
<!--
.style3 {font-family: "Courier New", Courier, monospace; font-size: small; }
.style4 {font-size: small}
-->
</style>
</head>

<body>
<form id="form1" name="frmLogIn" method="post" action="ValidateUser.jsp" onsubmit="return ChkLogin(this);">
  <table width="269" border="0" align="center">
    <tr>
      <td width="100"><span class="style3">Username:</span></td>
      <td width="159"><input name="username" type="text" size="25" maxlength="15" /></td>
    </tr>
    <tr>
      <td><span class="style3">Password:</span></td>
      <td><input name="password" type="password" size="25" maxlength="15"/></td>
    </tr>
    <tr>
      <td><span class="style4"></span></td>
      <td><span class="style3">
        <input type="submit" name="btnSubmit" value="Submit"/>
        <input type="reset" name="btnReset" value="Reset" />
      </span></td>
    </tr>
  </table>
</form>
</body>
</html>



validate
<%
String userName = request.getParameter("username");
String password = request.getParameter("password");

if (userName != "Jose") {
	out.println("You have entered an invalid username. " + userName);
	%>
<br/>
<span class="style3">Click <a href="LogIn.jsp">here</a> to re-login.</span>
<%
} else {
	out.println("Welcome " + userName + "!");
}
%>



our problem is with the validation. We got a wrong output. If we put anything (even the 'Jose' username) we have the invalid message...

what's wrong with our code? can someone checked on it...

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: JSP: Check code please

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: JSP: Check code please

Posted 03 October 2005 - 05:25 AM

Hmm...first things first...print out the value of the variable, and make sure that it is indeed coming across. If it is, then perhaps we can try the .equals or .compareTo methods of the jsp string class (examples shown here) to see if it will pick up the right condition.
Was This Post Helpful? 0
  • +
  • -

#3 lilygrace  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 17-August 05

Re: JSP: Check code please

Posted 03 October 2005 - 08:58 AM

Thanks Amadeus *hugs*... we will check out on that....AAAHH!! YOU'RE SIMPLY THE BEST!!

I will let you know what happened

Edit: On behalf of my partner, Jose, thank you so much. It works now.

This post has been edited by lilygrace: 04 October 2005 - 06:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 lilygrace  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 17-August 05

Re: JSP: Check code please

Posted 04 October 2005 - 10:06 PM

follow up question...

we already got it right..

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome Page</title>
</head>

<body>
<%
String sessionUser = String.valueOf(session.getAttribute("currentUser"));
if (sessionUser.equals("null") == true) {
	response.sendRedirect("LogIn.jsp");
} else {
%>
<h1>Welcome <%out.print(session.getAttribute("currentUser"));%></h1>
<br />To check your session's effectivity, click <a href="LogIn.jsp">here</a> to re-login.
<br />To end session, click <a href="LogOut.jsp" onclick="">log-out</a>
<%}%>
</body>
</html>



but... when we already logout the page and try to push the back button, the page was still there (instead of going back to login page since the session is already expired)... we are using IE and FireFox...

what could be an explanation for this... or there's any code to prevent viewing the page after logging out.

thank you in advance.

This post has been edited by lilygrace: 04 October 2005 - 11:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: JSP: Check code please

Posted 05 October 2005 - 06:35 AM

Hmmm....sounds like it's loading from cache (you can try setting the cache to null)...am I correct in assuming that if someone tries to access that page besides going through a set series of events, then you want them redirected? You need to force a reload of the page.
Was This Post Helpful? 0
  • +
  • -

#6 lilygrace  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 17-August 05

Re: JSP: Check code please

Posted 05 October 2005 - 07:07 AM

actually that incident is very odd in using firefox...I use IE always and the behavior of IE towards session or cookies, destroying session or cookie not working too well...*i guess*

our supervisor tried our script. He logged out and then he tried to access the welcomepage again by hitting the back space. If the session is already destroy.. the page will redirect to login page...but it did not happened that way...instead it viewed the welcome page....so we forced to reload the page again for the script make effective.

This post has been edited by lilygrace: 05 October 2005 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1