Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help in Login

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help in Login

    Hi. I have a problem with my Login. It shows me a blank page. The following is my codes:

    Account.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 + "]";
    }
    }
    AccountDAO.java:
    /*
    * 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;
    }
    LoginAccount.Servlet
    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>
    }
    index.jsp - Login Webpage.
    <%--
    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>
    Please do advice! Thank you.


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Help in Login

    Hi. I have a problem with my Login. It shows me a blank page. The following is my codes:
    when its showing you blank page.when you are requesting for page , when login fails , when login successful.

    when?
    Please be clear with your problem.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Need help in login page in struts
    By vinothkumarrvk in forum Web Frameworks
    Replies: 4
    Last Post: October 4th, 2011, 02:10 PM
  2. login to website as user, no browser
    By chopficaro in forum Java Theory & Questions
    Replies: 6
    Last Post: May 27th, 2010, 06:55 PM
  3. Http post to login to forum
    By durenir in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2010, 10:03 AM
  4. Login form on webpage
    By rosebabz in forum JDBC & Databases
    Replies: 0
    Last Post: January 14th, 2010, 09:34 AM
  5. simple login web service
    By mr_aliagha in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 5th, 2010, 02:49 PM