JavaWeb
JavaBean
-
必须有一个无参构造
-
属性必须私有化
-
必须有对应的 get/set 方法
-
一般用来和数据库的字段做映射 ORM(对象关系映射)
表 ——> 类
字段 ——> 属性
行记录 ——> 对象
MVC 三层架构
- 模型视图控制器(Model + view + Controller)
Model
- 业务处理:业务逻辑(Service)
- 数据持久层:CRUD(Dao)
View
- 展示数据
- 提供连接发起 Servlet 请求(a,form,img…)
Controller
- 接收用户的请求(request:请求参数,Session 信息…)
- 交给业务层处理对应的代码
- 控制视图的跳转
Filter
-
过滤器,用来过滤网站的数据
处理 web 服务器的垃圾请求
处理乱码问题
Filter 实现过程
- 在 pom.xml 中添加依赖
-
编写过滤器
实现 Filter 接口
//导入的包一定要对
import javax.servlet.*;
import java.io.IOException;
public class tetx implements Filter {
//初始化
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化");
}
//过滤器
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse); //让我们的请求继续走,如果不写,程序到这里就会停止,无法向下转交
}
//销毁,web服务器关闭时,过滤器销毁
@Override
public void destroy() {
System.out.println("已销毁");
}
}
<filter>
<filter-name>f1</filter-name>
<filter-class>pojo.tetx</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/servlet/*</url-pattern> <!--只要是/servlet下的请求都会通过此过滤器-->
</filter-mapping>
登录和注销的实现
登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="servlet/loading" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
处理请求
public class Loading extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if (username.equals("admin")&&password.equals("123456")){
req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId());
resp.sendRedirect("../sys/success.jsp");
}else{
resp.sendRedirect("../error/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class Logout extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object user_name = req.getSession().getAttribute(Constant.USER_SESSION);
if (user_name!=null){
req.getSession().removeAttribute(Constant.USER_SESSION);
resp.sendRedirect("../loading.jsp");
}else {
resp.sendRedirect("../loading.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
增加过滤器
- 账号注销后不能进入登入成功界面
public class text implements Filter {
//初始化
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) servletRequest;
HttpServletResponse response=(HttpServletResponse) servletResponse;
if (request.getSession().getAttribute(Constant.USER_SESSION)==null){
response.sendRedirect("../error/error.jsp");
}
filterChain.doFilter(servletRequest,servletResponse);
}
//销毁
@Override
public void destroy() {
}
}
注册
<filter>
<filter-name>f2</filter-name>
<filter-class>pojo.text</filter-class>
</filter>
<filter-mapping>
<filter-name>f2</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>loading</servlet-name>
<servlet-class>pojo.Loading</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loading</servlet-name>
<url-pattern>/servlet/loading</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>pojo.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/servlet/logout</url-pattern>
</servlet-mapping>
对于常用的常量可以将其封装在一个类中,方便调用和后期修改
package util;
public class Constant {
public final static String USER_SESSION="USER_SESSION";
}
Listener
- 监听器
- GUI 编程中经常使用
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class Listener implements HttpSessionListener {
/*创建session监听
* 一旦创建session就会触发一次这个时间*/
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext sc = se.getSession().getServletContext();
Integer number = (Integer) sc.getAttribute("number");
if (number==null){
number=new Integer(1);
}else {
int count=number.intValue();
number=new Integer(count+1);
}
sc.setAttribute("number",number);
}
//销毁session监听
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext sc = se.getSession().getServletContext();
Integer number = (Integer) sc.getAttribute("number");
if (number==null){
number=new Integer(0);
}else {
int count=number.intValue();
number=new Integer(count-1);
}
sc.setAttribute("number",number);
}
}
<h1>在线人数为:<%=this.getServletConfig().getServletContext().getAttribute("number")%></h1>
<listener>
<listener-class>pojo.Listener</listener-class>
</listener>
Junit 单元测试
- 依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
-
使用方法:@Text
只能对方法使用,只要加了这个注解的方法就可以直接运行,不需要走 main 方法
方法正确:
方法错误: