过滤器Filter
1. 概念
Filter也称之为过滤器,它是Servlet技术中最激动人心的技术之一,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态html文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等 一些高级功能。
2.简易图
3.主要代码
//过滤器
@WebFilter("/*") // * 表示过滤全部内容
public class CharsetFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharsetFilter.init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("CharsetFilter.doFilter");
//解决接收和响应的乱码问题
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("CharsetFilter.destroy");
}
}
以上代码用过滤器解决了系统数据的乱码问题。
学生管理系统登陆界面
1.网页效果图
- 网页界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="common.jsp"%>
<style type="text/css">
#form{
/*background-color: #cccccc;*/
}
#fm{
margin-top:40px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6" id="form">
<h2>登录</h2>
<h4>${err_msg}</h4>
<form class="form-horizontal" action="<%=contextPath%>/login?a=login" method="post">
<div class="form-group">
<label for="account" class="col-sm-2 control-label">账号</label>
<div class="col-sm-10">
<input type="text" name="account" class="form-control" id="account" placeholder="账号">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control" id="password" placeholder="密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> 记住我
</label>