Java Servlet Simple Login System (with MySQL)

Posted: November 12, 2009 in Java, Programming

Servlet :

package com.arham.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;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.sql.SQLException;

public class logincheck extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String username = request.getParameter(“username”).toString();

String password = request.getParameter(“password”).toString();


Connection connection = null;

try {


String driverName = “org.gjt.mm.mysql.Driver”; // MySQL MM JDBC driver

Class.forName(driverName);

String serverName = “localhost”;

String mydatabase = “arhamjava”;

String url = “jdbc:mysql://” + serverName + “/” + mydatabase; // a JDBC url

String sqlusername = “root”;

String sqlpassword = “MyP@SsW0rD”;


connection = DriverManager.getConnection(url, sqlusername, sqlpassword);

}

catch (ClassNotFoundException e) {


// Could not find the database driver

}

catch (SQLException e) {


// Could not connect to the database

}


Statement stmt = null;

ResultSet rs = null;

String checkUsername = null;

String checkPassword = null;

int checkAccessPower = 0;

try

{


stmt = connection.createStatement();

rs = stmt.executeQuery(“SELECT username, password, accesspower FROM account WHERE username = ‘” + username + “‘ AND id > 0”);


PrintWriter out2 = response.getWriter();


while(rs.next())

{

checkUsername = rs.getString(“username”);

checkPassword = rs.getString(“password”);

checkAccessPower = rs.getInt(“accesspower”);

}

if(checkUsername != null)

{

if(checkPassword.equals(password))

{

out2.println(“Welcome ” + username + “<br/> <h2>Login Success</h2>”);

if(checkAccessPower > 150)

out2.println(“Status : ADMIN”);

else if (checkAccessPower > 50)

out2.println(“Status : Normal User”);

else if (checkAccessPower < 9)

out2.println(“You have been banned by admin. Contact your administrator for clarification”);

}

else

out2.println(“<h1>Login Fail</h1> <br/> Click back to relogin!”);

}

else

out2.println(“<h2>Login Fail</h2> <br/> Reason : Your username not exist in our database.”);

}

catch (SQLException ex)

{


// handle any errors

System.out.println(“SQLException: ” + ex.getMessage());

System.out.println(“SQLState: ” + ex.getSQLState());

System.out.println(“VendorError: ” + ex.getErrorCode());

}


response.setContentType(“text/html;charset=UTF-8”);

PrintWriter out = response.getWriter();

try {

out.println(“<html>”);

out.println(“<head>”);

out.println(“<title>Servlet Login Check </title>”);

out.println(“</head>”);

out.println(“<body>”);

out.println(“</body>”);

out.println(“</html>”);

} finally

{

    out.close();

    }

}


// <editor-fold defaultstate=”collapsed” desc=”HttpServlet methods. Click on the + sign on the left to edit the code.”>

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

@Override

public String getServletInfo() {

return “Short description”;


}// </editor-fold>

}

***********************************************************************************

Comments
  1. acir says:

    i get this error msg:

    HTTP Status 500 –

    type Exception report

    message

    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

    exception

    java.lang.NullPointerException
    note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.

    GlassFish Server Open Source Edition 3.1.2.2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s