Hi. I have a problem with my Login. It shows me a blank page. The following is my codes:
Account.java
AccountDAO.java:/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
*/
public class Account {
private String username;
private String password;
private String name;
private String address;
private String email;
private String contact;
private String usertype;
/**
* @return the accountid
*/
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the contact
*/
public String getContact() {
return contact;
}
/**
* @param contact the contact to set
*/
public void setContact(String contact) {
this.contact = contact;
}
/**
* @return the usertype
*/
public String getUsertype() {
return usertype;
}
/**
* @param usertype the usertype to set
*/
public void setUsertype(String usertype) {
this.usertype = usertype;
}
public String toString() {
return "[" + username + "/" + password + "/" + name + "/" +
address + "/" + email + "/" + contact + "/" + usertype + "]";
}
}
LoginAccount.Servlet/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.util.ArrayList;
/**
*
* @author user
*/
public class AccountDAO {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/hrdb";
private static final String USER = "root";
private static final String PWD = "";
private static final String RETRIEVE_SQL = "SELECT username, password FROM hrdb.account where username = ?";
private static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(DATABASE_URL, USER, PWD);
return con;
}
public static Account get(String username) throws Exception {
Account a = null;
Connection con = getConnection();
PreparedStatement stmt = con.prepareStatement(RETRIEVE_SQL);
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
if(rs.next()) {
a = new Account();
a.setUsername(rs.getString("username"));
a.setPassword(rs.getString("password"));
}
stmt.close();
con.close();
return a;
}
index.jsp - Login Webpage.import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginAccountServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
Account a = null;
try {
a = AccountDAO.get(username);
} catch (Exception e) {
request.setAttribute("messages", "Application error - Please fix it");
throw new ServletException(e);
}
if (a == null || !a.getPassword().equals(password)) {
request.setAttribute("errors", "Login Error! Please try again.");
request.getRequestDispatcher("CompanyPage.jsp").fo rward(request, response);
return;
}
if (a.getUsertype().equals("Company")) {
request.getRequestDispatcher("CompanyPage.jsp").fo rward(request, response);
} else {
request.getRequestDispatcher("CandidatePage.jsp"). forward(request, response);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Please do advice! Thank you.<%--
Document : index
Created on : Jan 25, 2011, 12:45:52 PM
Author : user
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Log In</title>
<style type="text/css">
.style1 {
margin-left: 112px;
margin-right: 10px;
margin-top: 60px;
margin-bottom: 0px;
}
.style2 {
margin-left: 112px;
margin-right: 0px;
margin-top: 0px;
}
.style3 {
font-size: 35px;
}
</style>
</head>
<body background="http://i608.photobucket.com/albums/tt163/flyingpapers/background.jpg" text="#FFFFFF" title="Log In">
<div>
<img alt="" class="style1" height="153" src="http://i608.photobucket.com/albums/tt163/flyingpapers/Infotechnics_Banner.jpg" width="1025" /></div>
<div align="center" class="style2" style="height: 496px; width: 1025px; font: 20px Arial, Helvetica, sans-serif; border: 1px #FFFFFF solid; background-color: #181D54">
<br />
<br />
<img alt="" src="http://www.footballfreetv.com/includes/images/effects/login_key.png" height="140" width="136" />
<br />
<br />
<label id="Login"><span class="style3">Log In<br /></span></label>
${errors}<br>
${messages}<br>
<form action = "LoginAccountServlet" method="POST">
<label id="username">Username: </label><input name="username" type="text" value =""/><br/><br/>
<label id="password">Password: </label><input name="password" type="password" value =""/><br/>
<br/>
</form>
<form action="CompanyPage.jsp"> <input name="Login" type="submit" value="login" /><a href="CompanyPage.jsp"></a></form>
<input name="Clear" type="reset" value="clear" />
<FORM action="Signup.jsp"><INPUT type=submit value="Signup"><a href="Signup.jsp" ></a></FORM>
</div>
</body>
</html>