Created
December 10, 2013 10:08
-
-
Save MedinskyAlexander/7888401 to your computer and use it in GitHub Desktop.
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 example; | |
import com.mysql.jdbc.Driver; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
import java.util.Properties; | |
public class ConnectionManager { | |
static Connection con; | |
static String url; | |
public static Connection getConnection() { | |
// work | |
// url = "jdbc:mysql://127.0.0.1:3306/mydb?user=root&password=1234"; | |
url = "jdbc:mysql://127.0.0.1:3306/mydb"; | |
//home | |
// url = "jdbc:mysql://127.0.0.1:3306/mydb?user=user&password=1234"; | |
// url = "jdbc:mysql://127.0.0.1:3306/mydb"; | |
try { | |
// try { | |
// Class.forName("com.mysql.jdbc.Driver"); | |
// } catch (ClassNotFoundException e) { | |
// System.out.println("\nDriver not found....\n"); | |
// e.printStackTrace(); | |
// } | |
Driver dr = new com.mysql.jdbc.Driver(); | |
Properties properties = new Properties(); | |
properties.put("user","root"); | |
// properties.put("user","user"); | |
properties.put("password","1234"); | |
con = dr. connect(url,properties); | |
// con = DriverManager.getConnection(url); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return con; | |
} | |
} |
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 example; | |
import org.springframework.beans.factory.BeanFactory; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
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 java.io.IOException; | |
public class LoginServlet extends HttpServlet { | |
private UserBean user; | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
try{ | |
ApplicationContext context = new ClassPathXmlApplicationContext("/app_Context.xml"); | |
BeanFactory factory = (BeanFactory) context; | |
user = (UserBean)factory.getBean("User"); | |
// user = new UserBean(); | |
user.setUserName(req.getParameter("login")); | |
user.setPassword(req.getParameter("pass")); | |
user = UserDAO.login(user); | |
if(user.isValid()){ | |
HttpSession session = req.getSession(true); | |
session.setAttribute("currentUser", user); | |
resp.sendRedirect("userLogged.jsp"); | |
} else { | |
resp.sendRedirect("invalidLogin.jsp"); | |
} | |
} catch (Throwable ex){ | |
System.out.println(ex); | |
} | |
} | |
} |
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 example; | |
public class UserBean { | |
private String userName; | |
private String password; | |
private String fiRstName; | |
private String lastName; | |
private boolean valid; | |
public void setUserName(String userName) { | |
this.userName = userName; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public void setFirstName(String fiRstName) { | |
this.fiRstName = fiRstName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public void setValid(boolean valid) { | |
this.valid = valid; | |
} | |
public String getUserName() { | |
return userName; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public String getFiRstName() { | |
return fiRstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public boolean isValid() { | |
return 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
package example; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class UserDAO { | |
static Connection currentCon = null; | |
static ResultSet rs = null; | |
public static UserBean login(UserBean bean) { | |
Statement statement = null; | |
String username = bean.getUserName(); | |
String password = bean.getPassword(); | |
String query = "SELECT * FROM users WHERE username='" + username + "' AND password='" + password+"'"; | |
// connetc to db | |
try{ | |
currentCon = ConnectionManager.getConnection(); | |
statement = currentCon.createStatement(); | |
rs = statement.executeQuery(query); | |
boolean more = rs.next(); | |
// if users does not exist setisValid to false | |
if (!more){ | |
System.out.println("Sorry? you are not a registered user! Please sing up first"); | |
bean.setValid(false); | |
} else { | |
// if user exist | |
String firstName = rs.getString("FirstName"); | |
String lastName = rs.getString("LastName"); | |
bean.setFirstName(firstName); | |
bean.setLastName(lastName); | |
bean.setValid(true); | |
} | |
} catch (SQLException ex){ | |
}finally { | |
if (rs != null){ | |
try { | |
rs.close(); | |
} catch (SQLException e) { | |
rs = null; | |
} | |
} | |
if (statement != null){ | |
try{ | |
statement.close(); | |
} catch (SQLException e) { | |
statement = null; | |
} | |
} | |
if (currentCon != null){ | |
try{ | |
currentCon.close(); | |
} catch (SQLException e) { | |
currentCon = null; | |
} | |
} | |
} | |
return bean; | |
} | |
} |
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 jdbc.test; | |
import java.sql.SQLException; | |
public class AAA { | |
public static void main(String[] args) throws SQLException { | |
// String url = "jdbc:mysql://127.0.0.1:3306/mydb?user=root&password=1234"; | |
// try { | |
// Connection con = DriverManager.getConnection(url); | |
// Statement stm = con.createStatement(); | |
// ResultSet rs = stm.executeQuery("select * from users"); | |
// | |
// } catch (SQLException e) { | |
// | |
// } | |
// MysqlDataSource dataSource = new MysqlDataSource(); | |
// dataSource.setServerName("127.0.0.1:3306"); | |
// dataSource.setUser("root"); | |
// dataSource.setPassword("1234"); | |
// Connection con = dataSource.getConnection(); | |
// Statement stm = con.createStatement(); | |
// ResultSet rs = stm.executeQuery("select * from users"); | |
// | |
// Connection con = ConnectionManager.getConnection(); | |
// Statement stm = con.createStatement(); | |
// ResultSet rs = stm.executeQuery("select * from users"); | |
// | |
// if (rs.next()){ | |
// System.out.println(rs.getString("FirstName")); | |
// System.out.println(rs.getString("LastName")); | |
// } | |
} | |
} |
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"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<!--<bean id="User" class="example.UserBean" scope="singleton"/>--> | |
<bean id="User" class="example.UserBean" scope="singleton"> | |
<!--<property name="firstName" ref=""--> | |
</bean> | |
<!--<bean id="UserDAO" class="example.UserDAO" scope="singleton"/>--> | |
</beans> |
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=windows-1256" | |
pageEncoding="windows-1256" | |
%> | |
<html> | |
<head> | |
<title>Login page</title> | |
</head> | |
<body> | |
<h1 align="center">LOGIN PAGE</h1> | |
<br> | |
<br> | |
<form action="verify_login.do" method="get"> | |
Please enter your name | |
<br> | |
<input name="login" id="login" type="text"> | |
<br> | |
Please enter your password | |
<br> | |
<input name="pass" id="pass" type="text"> | |
<br> | |
<input type="submit"> | |
</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
<%@ page language="java" | |
contentType="text/html; charset=windows-1256" | |
pageEncoding="windows-1256" | |
%> | |
<!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=windows-1256"> | |
<title>Invalid Login</title> | |
</head> | |
<body> | |
<center> | |
Sorry, you are not a registered user! Please sign up first | |
</center> | |
</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=windows-1256" | |
pageEncoding="windows-1256" | |
import="example.UserBean" | |
%> | |
<!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=windows-1256"> | |
<title> User Logged Successfully </title> | |
</head> | |
<body> | |
<center> | |
<%--<% UserBean currentUser = (UserBean) (session.getAttribute("currentSessionUser"));%>--%> | |
<% UserBean currentUser = (UserBean) (session.getAttribute("currentUser"));%> | |
Welcome <%=currentUser.getFiRstName()+" "+ currentUser.getLastName()+""%> | |
</center> | |
</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
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | |
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
version="3.0"> | |
<servlet> | |
<servlet-name>LoginPage</servlet-name> | |
<servlet-class>example.LoginServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>LoginPage</servlet-name> | |
<url-pattern>/verify_login.do</url-pattern> | |
</servlet-mapping> | |
<welcome-file-list> | |
<welcome-file>/index.jsp</welcome-file> | |
</welcome-file-list> | |
<!--<filter>--> | |
<!--<filter-name>LoginFilter</filter-name>--> | |
<!--<filter-class>filters.LoginFilter</filter-class>--> | |
<!--</filter>--> | |
<!--<filter-mapping>--> | |
<!--<filter-name>LoginFilter</filter-name>--> | |
<!--<url-pattern>/login</url-pattern>--> | |
<!--</filter-mapping>--> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment