java web project login and logout with JSP and servlet with Mysql.

In this tutorial, we are going to cover complete Login and Logout with JSP and servlet and MySQL. Before start login and logout with JSP and servlet please take a look at login and registration activity. 

Here is complete video tutorial with practical which will show you how to create java application and connect it with MSQL a database. or an idea for java database connectivity(JDBC) with MySQL so please watch the complete video first.

login and logout with JSP and servlet with Mysql

Following are requirements for creating a dynamic web application with Eclipse?

Database:    MySQL workbench.

Language : HTML or JSP,servlet.

Server:         Glassfish or Tomcat.

Let’s start the Login and Logout with JSP and MySQL.

Configure your  MYSQL database:

Create a new database with name “mydb” and table name “user_reg”.

CREATE TABLE `mydb`.`user_reg` (
  `uid` INT NOT NULL AUTO_INCREMENT,
  `uname` VARCHAR(45) NULL,
  `upass` VARCHAR(45) NULL,
  `uemail` VARCHAR(45) NULL,
  PRIMARY KEY (`uid`));

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Login and Lgout activity using JSP</h1>
<a href="http://localhost:8080/loginDemo/home.jsp"> Home</a>
<a href="http://localhost:8080/loginDemo/login.jsp"> Login</a>
<a href="http://localhost:8080/loginDemo/logout.jsp"> Logout</a>
</body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

  <h1>Login Here</h1>

  <form action="UserProfile" method="post">
    <pre>
UserName: <input type="text" name="uname">

Password: <input type="text" name="upass">

    <input type="submit" value="Login">

</pre>
  </form>


</body>
</html>

WelcomeUser.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% 
request.getSession(false);
if (session == null) { %>
  <a href="http://localhost:8080/loginDemo/home.jsp"> Home</a>
  <a href="http://localhost:8080/loginDemo/login.jsp"> Login</a>
  <% 
} else {
    // Already created.
    %>
  <a href="http://localhost:8080/loginDemo/logout.jsp"> Logout</a><% 
}

String name = request.getParameter("name");
%>
<br><br>

<%

out.println("Welcome: "+name);
%>



</body>
</html>

UserProfile.java(Servlet)

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class UserProfile
 */
@WebServlet("/UserProfile")
public class UserProfile extends HttpServlet {
  private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserProfile() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    PrintWriter out = response.getWriter();
    String uname = request.getParameter("uname");
    String upass = request.getParameter("upass");
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select uname,upass from user_reg where uname='"+uname+"' and upass='"+upass+"'");
      
      if(rs.next()) {
        response.sendRedirect("http://localhost:8080/loginDemo/WelcomeUser.jsp?name="+rs.getString("uname"));
        HttpSession session = request.getSession();
        session.setAttribute("uname", uname);
        
        
      }else {
        out.println("Wrong id and password");
      }
      
      
      
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    
    
  }

}

 

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

sixteen + three =