1.MVC设计模式
1.1MVC模式
1.2MVC模式示例
index.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>
<a href="WelcomeServlet">WelcomeServlet</a>
<form action="WelcomeServlet" method="post">
<input type="submit"/>
</form>
</body>
</html>
WelcomeServlet.java
package com.wjc.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WelcomeServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost...");
}
}
web.xml
···
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>com.wjc.servlet.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>
1.3Servlet执行流程
1.4Servlet2.5与Servlet3.0的区别
1.5 / 的使用
1.6Servlet的生命周期
1.7Servlet继承关系
ServletAPI的详解与源码分析可以多看两遍
1.8MVC案例实现
项目目录login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.wjc.servlet.LoginServlet" %>
<!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="登录"><br/>
</form>
</body>
</html>
LoginServlet.java
package com.wjc.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.wjc.dao.LoginDao;
import com.wjc.entity.LoginData;
//控制器层:接受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 password = request.getParameter("upwd");
LoginData login = new LoginData(name, password);//用户名和密码
//调用模型层的登录功能
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 {
doGet(request,response);
}
}
LoginDao.java
package com.wjc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.wjc.entity.LoginData;
//模型层:用于处理登录(查询数据)
public class LoginDao {
public static int login(LoginData login) {//登录
int flag = 0;//登陆成功或失败的标识 1: 0: -1:
int result = 0;
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
String UNAME = "root";
String UPWD = "diang520";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(URL,UNAME,UPWD);
String sql = "select count(*) from login where name = ? and password =?;";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, login.getName());
pstmt.setString(2, login.getPassword());
rs = pstmt.executeQuery();
if(rs.next()) {
result = rs.getInt(1);
}
if(result > 0) {
flag = 1;
}
return flag;
}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();
}
}
}
}
LoginData.java
package com.wjc.entity;
public class LoginData {
private String name;
private String password;
public LoginData() {
}
public LoginData(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
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>
网友评论