JavaWeb之MVC模式设计(实例)
程序员文章站
2022-04-02 11:45:13
...
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller),这里概念什么的不多说,想了解更多可以点 https://www.baidu.com/ …
Servlet(写在前面):
Servlet 其实就是一个Java实现类,有doGet()、doPost() 两个方法可供使用,这两个方法分别处理Get请求和Post请求
Servlet2.5 如果像调用此类需要在项目的 web.xml 中添加路径,就像:
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>Servlet.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
从数据库到Java到前端
第一步、JavaBean式对信息进行面向对象类封装
public class login {
private int id;
private String uname;
private String upwd;
public login() {
// TODO Auto-generated constructor stub
}
public login(String uname, String upwd) {
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;
}
}
第二步、模型层涉及——链接数据库核对登录信息
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import entity.*;
public class loginDao {
public static int login(login login) {
int flag=-1;
int result=-1;
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testJDBC","root","123456");
String sql="select count(*) from user where uname= ? and upwd = ?";
pstmt=conn.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) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}finally {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
第三步、控制器层——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 Dao.loginDao;
import entity.login;
public class loginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 处理登录请求
req.setCharacterEncoding("utf-8");
String uname=req.getParameter("uname");
String upwd=req.getParameter("upwd");
login login=new login(uname, upwd);
int result=loginDao.login(login);
if(result>0) {
resp.sendRedirect("welcome.jsp");
}else {
resp.sendRedirect("login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
第四步、前端视图层的设计——这里就省略了,没必要过多深究,简单写两个jsp即可,一个登录页呈递表单数据,一个欢迎页
运行
打开服务器,浏览器可以观察到结果
当账号密码与数据库匹配时
账号或密码不匹配回到登录页
结束:给自己插个眼
后面学到东西要记得做总结(但愿能想起来。。。。)