三层优化 1.加入接口 建议面向接口开发:先接口-再实现类 –service、dao加入接口 –接口与实现类的命名规范 接口:interface, 起名 I实体类Service IStudentService IStudentDao 实现类:implements 起名 实体类ServiceImpl StudentServiceImpl StudentDaoImpl 接口: I实体类层所在包名 IStudentService、IStudentDao 接口所在的包: xxx.service xx.dao
实现类: 实体类层所在包名Impl StudentServiceImpl、StudentDaoImpl
实现类所在的包:xxx.service.impl xx.dao.impl
以后使用接口/实现类时,推荐写法:
接口 x = new 实现类();
IStudentDao studentDao = new StudentDaoImpl();
案例:实现用户登录验证 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="uname"><br>
密码:<input type="password" name="upwd"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登录成功!
</body>
</html>
com.xdr.entity–Login.java
package com.xdr.entity;
public class Login {
private int id;
private String uname;
private String upwd;
public Login() {
}
public Login(String uname, String upwd) {
super();
this.id = id;
this.uname = uname;
this.upwd = upwd;
}
public Login(int id, String uname, String upwd) {
super();
this.id = id;
this.uname = uname;
this.upwd = upwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
com.xdr.dao–loginDao.java
package com.xdr.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.xdr.entity.Login;
//模型层:用于处理登录(查询数据库)
public class LoginDao {
public static int login(Login login) {//登录
//boolean flag = false;//登录成功与否标识
int flag = -1; //-1系统异常 ,0用户名或密码有误 1登陆成功
int result = -1;
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp01","root","123456");
String sql = "select count(*) from login where uname=? and upwd=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, login.getUname());
pstmt.setString(2, login.getUpwd());
rs = pstmt.executeQuery();
if(rs.next()) {
result = rs.getInt(1);
}
if(result>0) {
return 1;
}else {
return 0;//登录失败(用户名或密码有误!)
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
return -1;//登录失败(系统异常!)
}catch(SQLException e) {
e.printStackTrace();
return -1;
}catch(Exception e) {
e.printStackTrace();
return -1;
}finally {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
com.xdr.servlet–LoginServlet.java
package com.xdr.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xdr.dao.LoginDao;
import com.xdr.entity.Login;
//控制层:接受view请求,并分发给model
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理登录
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
Login login = new Login(name,pwd);//用户名,密码
//调用陪你过模型层的 登录功能
int result = LoginDao.login(login);
if(result>0) {//成功
response.sendRedirect("welcome.jsp");
}else {//登录失败
response.sendRedirect("login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
web.xml配置添加,就能直接从根目录下访问项目了
数据库信息如下:
输入正确的用户信息后:
输入错误的信息会重新跳转到登录面: