servlet連線資料庫實現登入
阿新 • • 發佈:2018-11-04
實現了一個簡單的連線資料庫查詢使用者名稱是否匹配,並登入且使用cookie實現記住賬戶密碼的操作。
一下是程式碼。
封裝的一個Connection工具類
package com.xx;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUitl2 {
private JDBCUitl2(){
}
private static Connection connection;
private static Properties properties;
private static Properties readFile() {
// 通過類載入器獲取bin資料夾下的檔案的位元組流
InputStream is = JDBCUitl2.class.getClassLoader().getResourceAsStream ("dbinfo.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
static {
// 只讀一次檔案
properties = readFile();
try {
Class.forName(properties.getProperty("driverClass"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("註冊驅動失敗");
}
}
public static Connection getConnection() {
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("connection failure");
}
return connection;
}
//關閉資源
public static void JDBCClose(Connection connection, java.sql.Statement statement, ResultSet rs) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
登入servlet
package com.xx;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Cookie[] cookies = request.getCookies();
String username = "";
String password = "";
String chck="";
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username = cookie.getValue();
chck ="checked='checked'";
}
if (cookie.getName().equals("password")) {
password = cookie.getValue();
}
}
}
response.getWriter().write("<form action='/day33work/work1' method='post'>"
+ " 姓名:<input type='text' name='username' value='" + username + "'><br/>"
+ " 密碼:<input type='text' name='password' value='" + password + "'><br/>"
+ " 記住密碼:<input type='checkbox' name='chck' " + chck + "><br/>"
+ " <input type='submit' value='提交'>"
+ " </form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
處理登入請求servlet
package com.xx;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class work1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
String sql = "select * from users where username=? and password=?";
String username = request.getParameter("username");
String password = request.getParameter("password");
String chck = request.getParameter("chck");
Connection connection = JDBCUitl2.getConnection();
PreparedStatement prepareStatement = null;
try {
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, username);
prepareStatement.setString(2, password);
ResultSet rs = prepareStatement.executeQuery();
while (rs.next()) {
String user = rs.getString("username");
String pw = rs.getString("password");
if (user.equals(username) && pw.equals(password)) {
response.getWriter().write("登入成功");
Cookie cookie1 = new Cookie("username", username);
Cookie cookie2 = new Cookie("password", password);
cookie1.setPath("/");
cookie2.setPath("/");
if (chck != null) {
cookie1.setMaxAge(60*5);
cookie2.setMaxAge(60*5);
} else {
cookie1.setMaxAge(0);
cookie2.setMaxAge(0);
}
response.addCookie(cookie1);
response.addCookie(cookie2);
response.getWriter().write(username);
response.getWriter().write(password);
} else {
response.getWriter().write("登入失敗嗷");
response.setHeader("refresh", "3;/day33work/login");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.xml配置
<?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" 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>day33work</display-name>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.xx.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>work1</servlet-name>
<servlet-class>com.xx.work1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>work1</servlet-name>
<url-pattern>/work1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>