Hi,
i have created one
servlet for user and password validation considering one servlet example.The thing is when ever i login with database username and password it will redirect to the next page.but when ever i login with invalid username and password it doesnt show the error message with in(like alert message) that login page...shows blank login page.If i set the ${error} in login.jsp after the submit button in form.with out entering any thing it displaying the ${error} message.If i set the response.sendRedirect ("error.html") file in else part instead of request dispatcher it not taking the if(condition) condition at all.As am a beginner in web technologies.So please suggest me a way to work the else part and its error message.
Here is my
jsp,servlet,web.xml...
----------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
public class School extends HttpServlet{
public void service (HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/track?user=root&password=root" ;
String driver = "com.mysql.jdbc.Driver";
String username=new String("");
String password=new String(" ");
String strQuery= "";
Statement st=null;
ResultSet rs=null;
//HttpSession session = request.getSession(false);
try {
Class.forName(driver).newInstance();
conn=DriverManager.getConnection(url);
strQuery="select userID,password from user";
System.out.println(strQuery);
st = conn.createStatement();
//rs=st.executeQuery("select * from user where userID='"+user+"' and password='"+pass+"'");
rs = st.executeQuery(strQuery);
//int count=0;
while(rs.next())
{
//session.setAttribute("user",rs.getString(1));
//session.setAttribute("pass",rs.getString(2));
username=rs.getString("userID");
password=rs.getString("password");
}
rs.close();
st.close();
// count++;
}
catch(Exception e)
{
e.printStackTrace();
}
String user=request.getParameter("user");
String pass=request.getParameter("pass");
if(username.equals(user)&& password.equals(pass))
{
//response.sendRedirect("Map2.jsp");
RequestDispatcher dispatcher = request.getRequestDispatcher("Map2.jsp");
dispatcher.forward( request, response);
}
else if (!username.equals(user)&& !password.equals(pass))
{
request.setAttribute("errorMessage", "Invalid username or password");
RequestDispatcher dispatcher = request.getRequestDispatcher("Track.jsp");
dispatcher.forward( request, response);
}
}
}
---------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>schoolTrack</display-name>
<description>Vehicle tracking</description>
<welcome-file-list>
<welcome-file>Track.html</welcome-file>
<welcome-file>Track.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Tracklogin</servlet-name>
<servlet-class>Track</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Tracklogin</servlet-name>
<url-pattern>/Track</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Schooltrack</servlet-name>
<servlet-class>School</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Schooltrack</servlet-name>
<url-pattern>/School</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>java.lang.NullPointerException</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
--------------------------------------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- <!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' xmlns:v='urn:schemas-microsoft-com:vml'>
<!-- Head -->
<head>
<!-- meta -->
<meta name="author" content="GeoTelematic Solutions, Inc."/>
<meta http-equiv="content-type" content='text/html; charset=UTF-8'/>
<meta http-equiv="cache-control" content='no-cache'/>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta name="robots" content="none"/>
<!-- page title -->
<title> System</title>
<link rel="stylesheet" type="text/css" href="./css/General.css"/>
<!-- javascript -->
<script src= "/js/utils.js" type="text/javascript"></script>
<!-- script the username and password validation -->
<script>
function Validate()
{ var user=document.Login.user.value;
var invalid=/\W/; //Alphanumeric characters and Underscore permitted
if(user=="")
{
alert("Enter your username!");
return false;
}
if(invalid.test(user))
{
alert("Username contain Invalid characters!");
return false;
}
if(user.length<3 || user.length>15)
{ //username must be 6 - 15 letters length
alert("Entered Invalid user!");
return false;
}
var pass=document.Login.pass.value;
invalid=/[\W_]/; //Alphabets and digits only allowed
if(pass=="")
{
alert("Enter your password!");
return false;
}
if(pass.length<3 ){
alert("Password must contain atleast 3 letters!");
return false;
}
if(invalid.test(pass))
{
alert("Password contain Invalid characters!");
return false;
}
return true;
if(user)
{
alert("Username is invalid !");
return false;
}
return true;
}
</script>
<!-- local style -->
<style type="text/css">
BODY {
background-color: #FFFFFF;
}
TD.titleText {
background: #FFFFFF url('./images/Banner_WhiteShadow.png') center no-repeat;
font-family: arial,verdana,sans-serif;
font-size: 28pt;
font-weight: bold;
text-align: center;
color: #000000;
}
</style>
<!-- page specific style -->
<link rel="stylesheet" type="text/css" href="./css/AccountLogin.css"/>
<!-- custom override style -->
<link rel='stylesheet' type='text/css' href='custom/Custom.css'/>
</head>
<!-- ======================================================================================= -->
<body >
<table width="100%" height="100%" align="center" border="0" cellspacing="0" cellpadding="0" style="padding-top: 5px;">
<tbody>
<!-- Begin Page header/navigation ======================================== -->
<tr>
<td width="100%">
<table class="bannerTable" width="860" border="0" cellpadding="0" cellspacing="0" align="center">
<tbody>
<tr>
<!-- -->
<td width="860" height="120" halign="center">
<div align="center"><img src="./images/logo.jpg" alt="logo" width="1000" height="200">
</div>
</td>
<!-- -->
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
</tr>
</tbody>
</table>
</td>
</tr>
<!-- Begin Page contents ======================================== -->
<tr height="100%">
<td>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td valign='top' align='center' height='100%'>
<table class="accountLoginTable_nobord" cellspacing='0' cellpadding='0' border='0'>
<tbody>
<!-- The MenuBar is display iff 'content.class.table' is "contentTable"/"contentMapTable" -->
<tr><!-- no menubar ['accountLoginTable_nobord'] --></tr>
<tr height='100%'>
<td class="accountLoginCell_nobord">
<table class='accountLoginContentTable' width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td class='accountLoginTextCell'>
<span style='font-size:11pt'>Enter your Login ID and Password</span>
<hr style='height: 5px;'/><!--<div>${message}</div> -->
<!-- <div style="color:red">${errorMessage}</div>-->
<form name="Login" method="post" action="/schoolTrack/School" onsubmit="javascript:return Validate();">
<table class='accountLoginFormTable' cellpadding='0' cellspacing='0' border='0'>
<tr><td class='accountLoginFieldLabel'>User:</td><td class='accountLoginFieldValue'><input id='userLoginField' class='textInput' type='text' name='user' value='' size='24' maxlength='32'></td></tr>
<tr><td class='accountLoginFieldLabel'>Password:</td><td class='accountLoginFieldValue'><input class='textInput' type='password' name='pass' value='' size='24' maxlength='32'> </td></tr>
</table>
<br><input type='submit' name='submit' value='Login' >
</form>
<br/>
<hr style='height: 5px;'/>
<span style='font-size:8pt'><i><a href='register.jsp'>Sign up new account </a></i></span>
</td>
</tr>
</table>
<script type="text/javascript">
var loginFocusField = document.getElementById('accountLoginField');
if (loginFocusField) {
loginFocusField.focus();
loginFocusField.select();
}
</script>
</td>
</tr>
<tr>
<td id="contentMessage" class="contentMessage">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td style="font-size: 1pt; border-bottom: 1px solid #888888;"> </td>
</tr>
<tr>
<td>
<table class="copyrightFooterClear" width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="padding: 0px 0px 2px 5px;"> </td>
<td width="100%" align="center">
</td>
<td nowrap style="padding-bottom: 2px;">
<span style="font-size: 1pt; font-style: oblique; color: #888888;">
2.2.8
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>