什么是MVC
MVC三层架构:Model View Controller 模型、视图、控制器
Model
- 业务处理 :业务逻辑(Service)
- 数据持久层:CRUD (Dao - 数据持久化对象)
View
- 展示数据
- 提供链接发起Servlet请求 (a,form,img…)
Controller (Servlet)
- 接收用户的请求 :(req:请求参数、Session信息….)
- 交给业务层处理对应的代码
- 控制视图的跳转
过滤器Filter
过滤器用来过滤网站的数据,比如解决中文乱码,用于登录验证
使用它的步骤与servlet很相似: 1、实现接口,注意是javax.servlet下的Filter 2、重写对应的方法:
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter初始化");
}
@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);
}
@Override
public void destroy() {
System.out.println("CharacterEncodingFilter销毁");
}
我们的过滤内容是在doFilter方法中,注意其中的filterChain参数,要在里面执行 filterChain.doFilter(servletRequest, servletResponse); 为了是让过滤后的代码继续执行
销毁Filter的时刻是:Web服务器关闭的 时候,过滤器会自动销毁
3、在web.xml中配置 Filter
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.tunan.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/servlet
注意这里的url-pattern按照自己的过滤目标来设置
实现登录拦截
当我们输入正确的用户名时(通常是保存在数据库中的已注册用户,这里可以简单表示为admin),成功登录并跳转至主页;当输入不存在的用户名时, 跳转至错误页面。保证主页和错误页面上都有注销登录的操作。
登录拦截: 直接输入登录主页的url,不能够登陆进行,保证安全。
首先编写主页,登录页面以及错误页面的jsp文件: 主页success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>主页</h1>
<p><a href="/servlet/logout">注销</a></p>
</body>
</html>
登录页面Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录</h1>
<form action="/servlet/login" method="post">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
错误页面error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>ERROR</h1>
<p> <a href="/Login.jsp">返回登录页面</a> </p>
</body>
</html>
注意:我们的主页路径在sys文件夹下,我们是不允许直接访问这个文件夹下的jsp文件,因此需要登录拦截。
然后编写Servlet
登录LoginServlet.java
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
if(username.equals("admin")){
req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
resp.sendRedirect("/sys/success.jsp");
}else{
resp.sendRedirect("/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
如果用户输入的username是admin则跳转至主页/sys/success.jsp;否则跳转至错误页面/error.jsp
注销操作Servlet
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session = req.getSession().getAttribute("USER_SESSION");
if(user_session != null){
req.getSession().removeAttribute("USER_SESSION");
resp.sendRedirect("/Login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
这里是首先req.getSession().removeAttribute("USER_SESSION") ,清除了这里的Session。然后把页面跳转到了登录页面。
过滤器SysFilter
public class SysFilter 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("USER_SESSION")==null) {
response.sendRedirect("/error.jsp");
}
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
当没有Session的时候,直接跳转到ERROR页面,实现访问拦截
|