创建一个类实现Filter接口,重写三个方法
示例:
将所有接收到的参数。转码为 utf-8
package com.hz.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class CharEncodingFilter
* /admin/LoginServlet
* /admin/ProviderServlet
* /order/OrderInfoServlet
*
* /admin/*
* /admin/LoginServlet
*/
@WebFilter("/*")
public class CharEncodingFilter implements Filter {
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* /LoginServlet
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
request.setCharacterEncoding() : 设置编码格式
chain.doFilter(request, response): 放行
可以对获取到的session中是否有值进行判断,如果有值则放行,若没有的话,重定向到首页
示例:
package com.hz.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet Filter implementation class LoginFilter
*/
@WebFilter("/web/*")
public class LoginFilter implements Filter {
/**
* Default constructor.
*/
public LoginFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
if(req.getSession().getAttribute("user")!=null) {
chain.doFilter(request, response);
}else {
res.sendRedirect(req.getContextPath()+"/login.jsp");
}
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
由于重定向获取前登陆的路径,所以要在所有url前加个统一的路径信息。
使用Cookie存值流程,示例:
String username = URLEncoder.encode(uname,"utf-8");
Cookie cookie = new Cookie("uname",username);
cookie.setPath("/"); //设置使用路径
cookie.setMaxAge(20); //设置Cookie时间 秒为单位
response.addCookie(cookie); //向浏览器中添加Cookie信息
Cookie中是用键值的形式存入值,在存入值前,要先转码,否则可能会乱码
引入
1.添加一个"会话cookie"
$.cookie('the_cookie', 'the_value');
2.创建一个cookie并设置有效时间为 7天
$.cookie('the_cookie', 'the_value', { expires: 7 });
3.创建一个cookie并设置 cookie的有效路径
$.cookie(‘the_cookie’, ‘the_value’, { expires: 7, path: ‘/’ });
4.读取cookie
$.cookie('the_cookie');
5.删除cookie
$.cookie('the_cookie', null); //通过传递null作为cookie的值即可
6.可选参数
$.cookie('the_cookie','the_value',{
expires:7,
path:'/',
domain:'jquery.com',
secure:true
})
expires:(Number|Date)有效期;设置一个整数时,单位是天;也可以设置一个日期对象作为Cookie的过期日期;
path:(String)创建该Cookie的页面路径;
domain:(String)创建该Cookie的页面域名;
secure:(Booblean)如果设为true,那么此Cookie的传输会要求一个安全协议,例如:HTTPS;