1. 程式人生 > 其它 >web登入小專案(原生不使用框架)2020-12-12

web登入小專案(原生不使用框架)2020-12-12

技術標籤:java

不使用框架
個人水平較菜

1建立資料庫

在這裡插入圖片描述

2 pojo

public class Users {

	private int id;
	private String username;
	private String password;

生成getset方法

3 dao

先寫介面

public interface UsersDao {

	Users checkLoginDao(Users user);
}

實現介面

/**
 * 
 * @author 
 * //宣告jdbc物件conn prepare result
 * //宣告資料儲存物件
 *	//載入驅動
			//獲取連線物件
			//建立sql物件-命令
			//給佔位符賦值
			//執行
			//遍歷結果(放到users物件中)
			//關閉資源(順序為先開後關)
		//返回u
 */
public class UsersDaoImpl implements UsersDao{ @Override public Users checkLoginDao(Users user) { //宣告jdbc物件conn prepare result Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; //宣告資料儲存物件 Users users = null ; try{ //載入驅動 Class.forName
("com.mysql.jdbc.Driver"); //獲取連線物件 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssm","root","admin"); //建立sql物件-命令 preparedStatement = connection.prepareStatement("select * from users where username=? and password=?"); //給佔位符賦值
String username = user.getUsername(); String password = user.getPassword() ; preparedStatement.setString(1, username); preparedStatement.setString(2, password); //執行 resultSet = preparedStatement.executeQuery(); //遍歷結果(放到users物件中) while(resultSet.next()){ users = new Users(); users.setId(resultSet.getInt("id")); users.setUsername(resultSet.getString("username")); users.setUsername(resultSet.getString("password")); } }catch (Exception e) { e.printStackTrace(); }finally { try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { preparedStatement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return users; }

4service

先寫介面

public interface LoginService {
	
	Users checkLoginService(Users user);

}

寫實現類

public class LoginServiceImpl implements LoginService {

	@Override
	public Users checkLoginService(Users user) {
		UsersDao ld = new UsersDaoImpl();
		Users checkLoginDao = ld.checkLoginDao(user);
		return checkLoginDao;
	}

}

5servlet

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
	LoginServiceImpl LoginServiceImpl = new LoginServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		//req.setCharacterEncoding("utf-8	");
		
		String username = req.getParameter("username");
		if (username != null && !username.equals(""))
			username = new String(username.getBytes("iso-8859-1"), "utf-8");
		String password = req.getParameter("password");
		if (password != null && !password.equals(""))
			password = new String(password.getBytes("iso-8859-1"), "utf-8");
		Users user = new Users();
		user.setPassword(password);
		user.setUsername(username);
		Users LoginService = LoginServiceImpl.checkLoginService(user);
		if (LoginService!=null) {
			res.sendRedirect("main.jsp");
		}else{
			req.setAttribute("error", "使用者名稱密碼不正確");
			req.getRequestDispatcher("index.jsp").forward(req, res);
		}
	//	System.out.println(username+":"+password);
	}
}

6.jsp

<body>
${error }
<form action="login" method="post">
	使用者名稱:<input type="text" name="username"/><br/>
	密碼:<input type="password" name="password"/><br/>
	<input type="submit" value="登入"/><input type="reset" value="重置"/>
</form>
</body>

總結

問題:在給sql命令賦值時,接不到值,
解決:在dao層沒有給方法傳參 導致無法使傳值,改正確後給使用引數物件user給佔位符賦值

preparedStatement = connection.prepareStatement("select * from users where username=? and password=?");
//給佔位符賦值
String username = user.getUsername();`
String password = user.getPassword() ;
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);

問題:
報錯
java.io.UnsupportedEncodingException: The character encoding [uft-8] is not supported
解決
檢查使用的是req.setCharacterEncoding("utf-8 ");對應的post請求方式沒有錯誤仍然報錯
改用為保險形式後解決

String username = req.getParameter("username");
if (username != null && !username.equals(""))
	username = new String(username.getBytes("iso-8859-1"), "utf-8");
String password = req.getParameter("password");
if (password != null && !password.equals(""))
	password = new String(password.getBytes("iso-8859-1"), "utf-8");

疑問
jps中使用的都是utf-8為什麼原來方式不可以更改後不報錯可以執行

<%@ 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">