Skip to content

Instantly share code, notes, and snippets.

@immanuel2305
Created October 30, 2012 11:57
Show Gist options
  • Save immanuel2305/3979784 to your computer and use it in GitHub Desktop.
Save immanuel2305/3979784 to your computer and use it in GitHub Desktop.
Http Session Using Java EE - http://arunimmanuel.blogspot.in
package com.mbs.daoFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mbs.model.*;
public class DAOFactory implements QueryInterface{
//Function to Establish the DB Connection
public static Connection connectionManager() throws ClassNotFoundException, SQLException
{
Class.forName(driver);
Connection connection = null;
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/SESSION","root","samgha");
return connection;
}
//Function for authenticate the user login in to the System
public static LoginBean authenticate(LoginBean lb) {
String username = lb.getUsername();
String pass = lb.getPass();
Connection currentCon = null;
ResultSet rs =null;
PreparedStatement pstmt = null;
try{
currentCon = connectionManager();
pstmt = currentCon.prepareStatement(loginQuery);
pstmt.setString(1,username);
System.out.println("UserName"+username);
System.out.println("Pass"+pass);
pstmt.setString(2,pass);
rs=pstmt.executeQuery();
boolean more = rs.next();
System.out.println("More"+more);
if(!more)
{
lb.setValid(false);
}
else if(more)
{
lb.setValid(true);
}
}
catch(Exception e)
{
System.out.println("Log In Failed!!!"+e);
}finally
{
if(rs !=null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(pstmt !=null)
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
currentCon.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(lb.isValid());
return lb;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@page import="com.mbs.model.*" %>
<%String path = request.getContextPath(); %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello WORLD</title>
</head>
<body>
<% HttpSession httpsession = request.getSession(false);
if(null!=httpsession.getAttribute("CurrentUser")){
}
else{
String redirectURL= path;
response.sendRedirect(redirectURL);
}
LoginBean loginbean = (LoginBean)httpsession.getAttribute("CurrentUser");
if(null!=httpsession.getAttribute("CurrentUser")){ %>
<p>Hey Its Working Good !!! U have Success fully carried out Session</p>
<p class="f-right"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong><a href="<%=path %>/view/logout.jsp" id="logout">Log out</a></strong></p>
<%} %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Login Here</title>
</head>
<body>
<p>Please Fill the Form To Login</p>
<form action="./Login" method="post">
User Name: <input type="text" name="usr">
Password: <input type="password" name="pwd">
<input type="hidden" name="context" value="Login">
<input type= "submit" value ="Login">
</form>
</body>
</html>
package com.mbs.model;
public class LoginBean {
private boolean valid;
private String username;
private String pass;
public String getUsername() {
return username;
}
public String getPass() {
return pass;
}
public void setUsername(String username) {
this.username = username;
}
public void setPass(String pass) {
this.pass = pass;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
</head>
<body>
<% HttpSession httpsession = request.getSession(false);
httpsession.invalidate();
response.sendRedirect("/Session");
%>
</body>
</html>
package com.mbs.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mbs.daoFactory.DAOFactory;
import com.mbs.model.LoginBean;
public class MyController extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
if(req.getParameter("context").equalsIgnoreCase("Login"))
{
login(req,resp);
}
}
private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String UName = req.getParameter("usr");
String Pass = req.getParameter("pwd");
LoginBean lb = new LoginBean();
lb.setUsername(UName);
lb.setPass(Pass);
lb = DAOFactory.authenticate(lb);
if(lb.isValid())
{
HttpSession session = req.getSession();
session.setAttribute("CurrentUser",lb);
RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/view/home.jsp");
dispatch.forward(req, resp);
}
}
}
package com.mbs.daoFactory;
public interface QueryInterface {
public static final String driver ="com.mysql.jdbc.Driver";
//Login Query
public static final String loginQuery = "select * from LOGIN where USERNAME=? AND PASSWORD=?";
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Session</display-name>
<welcome-file-list>
<welcome-file>view/index.html</welcome-file>
<welcome-file>view/index.htm</welcome-file>
<welcome-file>view/index.jsp</welcome-file>
<welcome-file>view/default.html</welcome-file>
<welcome-file>view/default.htm</welcome-file>
<welcome-file>view/default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<display-name>MyController</display-name>
<servlet-name>MyController</servlet-name>
<servlet-class>com.mbs.controller.MyController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyController</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
</web-app>
@jayabal90
Copy link

very useful code for beginners .super immanual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment