Writing Login.jsp and authenticating a user who have stored in MySql DB
843842Nov 27 2008 — edited Feb 26 2009Hi Friends,
My project requirement is: Need to write a login page must send the request to servlet is the user and password avail in mysql db, if yes servlet should forward the home page else error message. Tools i need to use is IDE=eclipse, Server = tomcat, database = MySql
Here is source:
pls tell me where i m wrong.
Login.jsp
<%@ page language="java" %>
<html>
<head>
<title>Login Page</title>
<script language = "Javascript">
function Validate(){
var user=document.frm.user
var pass=document.frm.pass
if ((user.value==null)||(user.value=="")){
alert("Please Enter user name")
user.focus()
return false
}
if ((pass.value==null)||(pass.value=="")){
alert("Please Enter password")
pass.focus()
return false
}
return true
}
</script>
</head>
<body>
<h1>Login
<br>
</h1>
<form name="frm" action="/LoginAuthentication" method="Post" onSubmit="return Validate()" >
Name:
<input type="text" name="user" value=""/><br>
Password:<input type="password" name="pass" value=""/><br>
<br>
<input type="submit" value="Login" />
<input type="reset" value="forgot Password" />
</form>
</body>
</html>
Servlet Code:
LoginAuthentication.java
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.ArrayList;
public class LoginAuthentication extends HttpServlet{
private ServletConfig config;
public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
//public void init() {
// Normally you would load the prices from a database.
//ServletContext ctx = getServletContext();
// RequestDispatcher dispatcher = ctx.getRequestDispatcher("/HomePage.jsp");
//dispatcher.forward(req, res);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
PrintWriter out = response.getWriter();
String connectionURL = "jdbc:mysql://127.0.0.1/SRAT";
//String connectionURL = "jdbc:mysql://192.168.10.59/SRAT";
//127.0.0.1
//http://localhost:3306/mysql
Connection connection=null;
ResultSet rs;
String userName=new String("");
String passwrd=new String("");
response.setContentType("text/html");
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "admin", "admin");
//Add the data into the database
String sql = "select user,password from login";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
userName=rs.getString("user");
passwrd=rs.getString("password");
}
rs.close ();
s.close ();
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
if(userName.equals(request.getParameter("user"))
&& passwrd.equals(request.getParameter("pass"))){
out.println("WELCOME "+userName);
}
else{
out.println("Please enter correct username and password");
out.println("<a href='Login.jsp'><br>Login again</a>");
}
}
}
---------------------------
Deployment Descriptor for TOMCAT
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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">
<display-name>
SRAT</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginAuthentication</servlet-name>
<servlet-class>LoginAuthentication</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginAuthentication</servlet-name>
<url-pattern>/LoginAuthentication</url-pattern>
</servlet-mapping>
</web-app>
PLS HELP ME.
S. Udaya Chandrika