Created
October 30, 2012 11:57
-
-
Save immanuel2305/3979784 to your computer and use it in GitHub Desktop.
Http Session Using Java EE - http://arunimmanuel.blogspot.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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"> <strong><a href="<%=path %>/view/logout.jsp" id="logout">Log out</a></strong></p> | |
<%} %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=?"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very useful code for beginners .super immanual