目录
我们每次访问每个servlet都要书写处理请求和响应乱码的代码,这样代码十分冗余,所以我们可以在过滤中
@WebFilter("/*") public class EncodeFilter 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; //your code.. //解决请求乱码 request.setCharacterEncoding("utf-8"); //解决响应乱码 response.setContentType("text/html;charset=utf-8"); //放行 filterChain.doFilter(request,response); } @Override public void destroy() { } }
@WebServlet("/servletDemo1") public class servletDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); System.out.println(username); response.getWriter().print("哈哈哈"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Titletitle> head> <body> <form action="/servletDemo1" method="post"> <input type="text" name="username"> <input type="submit" value="提交"> form> body> html>
步骤
1.访问hhh.html这个页面时必须先要登录,不登录不能访问,使用过滤器代码书写代码让其跳转到登录页面login.html
2.登录loginServlet中获取用户名和密码,存到user对象中,然后存在session中(不关闭服务器,这个session就一直在服务器里面),最后重定向到hhh.html
3. 使用一个过滤器对hhh.html进行拦截,先从session中获取用户信息,如果没有登录,获取的是null,就跳转到登录页面,如果不为null,说明登录成功,放行
hhh.html
html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Titletitle> head> <body> <h1>这就是终极文件h1> body> html>login.html
html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录title> head> <body> <form action="/loginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="pwd"><br> <input type="submit" value="提交"> form> body> html>loginServlet
@WebServlet("/loginServlet") public class loginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求参数 String username = request.getParameter("username"); String pwd = request.getParameter("pwd"); User user = new User(); user.setUsername(username); user.setPwd(pwd); //这里在实际开发中,应该调用业务层的方法,与数据库交互 //获取session对象 HttpSession session = request.getSession(); session.setAttribute("u",user); //登陆成功,就跳转到hhh.html response.sendRedirect("/hhh.html"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }loginFilter过滤器,过滤hhh.html
@WebFilter("/hhh.html") public class loginFilter 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; //your code.. //从session中获取对象 HttpSession session = request.getSession(); User u = (User) session.getAttribute("u"); if(u==null){ //没有登录 //跳转到登录页面 response.sendRedirect("/login.html"); }else{ //放行 filterChain.doFilter(request,response); } } @Override public void destroy() { } }先登录
在显示页面
可以共享数据的对象
1.request:只能在一次请求一次响应中进行数据共享-->请求转发
2.session:只能在一次会话过程中,可以有多次请求和响应
3.ServletContext:只要项目存在就可以共享数据,多次会话,多次请求多次响应都可以共享数据,操作整个项目的配置文件
范围大小:
ServletContext>session>request
ServletContext属于接口,在tomcat启动的时候tomcat创建门面类(ServletContext的子类)
我们可以在Servlet类中直接获取ServletContext对象;getServletContext()属于父类GenericServlet中的方法
@WebServlet("/servletContextDemo1") public class servletContextDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext servletContext = getServletContext(); //获取指定文件的MIME类型 String mimeType = servletContext.getMimeType("demo1.html");//相对于webapp的路径 System.out.println("mimeType="+mimeType);//mimeType=text/html //获取指定文件的真实路径 String realPath = servletContext.getRealPath("demo1.html"); System.out.println("realPath="+realPath); //realPath=D:\java code\web6_filter_listener\target\web6_filter_listener-1.0-SNAPSHOT\demo1.html } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
说明:
javaweb的监听器就是监听ServletContext HttpServletRequest HttpSession这三个对象的创建和销毁,同时监听是哪个对象数据的变化,就是监听属性的变化:setAttibute(),removeAttribute()
1.ServletContext是tomcat启动创建,关闭tomcat销毁
2.HttpServlet是浏览器第一次访问执行request.getSession()创建,销毁时间 1)30min 2)执行invalidate()
3.HttpServletRequest时浏览器第一次访问Servlet创建,浏览器收到服务器的响应就销毁
//配置监听器 @WebListener public class conTextListerner implements ServletContextListener {//实现接口 //当ServletContext创建(启动tomcat)时执行该方法 @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("ServletContext创建"); } //当ServletContext(关闭tomcat)销毁时执行该方法 @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("ServletContext销毁"); } }不使用注解可以在web.xml中配置
<listener> <listener-class>com.hhh.listener.conTextListernerlistener-class> listener>