Servlet开发
Posted zyxsblogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet开发相关的知识,希望对你有一定的参考价值。
Servlet开发
Servlet过滤器:
主要是对访问主页的用户进行过滤,通过登录系统进入的用户可以看到主页内容,在session中存在currentuser。
可以对此进行判断:
1 package com.java1234.web; 2 import java.io.IOException; 3 import javax.servlet.Filter; 4 import javax.servlet.FilterChain; 5 import javax.servlet.FilterConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.ServletRequest; 8 import javax.servlet.ServletResponse; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpSession; 11 12 public class Loginfilter implements Filter { 13 14 @Override 15 public void destroy() { 16 // TODO Auto-generated method stub 17 18 } 19 20 @Override 21 public void doFilter(ServletRequest ServletRequest, ServletResponse ServletResponse, FilterChain FilterChain) 22 throws IOException, ServletException { 23 // TODO Auto-generated method stub 24 HttpServletRequest request=(HttpServletRequest)ServletRequest;//将ServletRequest强制转化为HttpServetRequest 25 HttpSession session=request.getSession();//获取sessin(如果用户登录就存在session) 26 Object o=session.getAttribute("currentUser");//获取currentUser对象 27 String path=request.getServletPath();//获取登录路径 28 if(o == null && path.indexOf("login")< 0){ 29 /*服务器内部跳转 - -> 没有登录不能能访问主页*/ 30 request.getRequestDispatcher("Login.jsp").forward(ServletRequest, ServletResponse); 31 }else{ 32 FilterChain.doFilter(ServletRequest,ServletResponse); 33 } 34 } 35 36 @Override 37 public void init(FilterConfig arg0) throws ServletException { 38 // TODO Auto-generated method stub 39 40 } 41 42 }
配置类似于 Servlet :
1 </web-app> 2 <filter> 3 <filter-name>Loginfilter</filter-name> 4 <filter-class>com.java1234.web.Loginfilter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>Loginfilter</filter-name> 8 <url-pattern>/login</url-pattern> 9 </filter-mapping> 10 </web-app>
Servlet监听器:
监听web事件;如application,session,request。
写一个监听session的例子:
首先当用户登录时,设置session值,这时候会添加属性,监听到。。。
其次注销的时候,删除session值,这时候是删除属性,监听到。。
so
代码如下:
1 package com.java1234.listener; 2 3 import javax.servlet.http.HttpSessionAttributeListener; 4 import javax.servlet.http.HttpSessionBindingEvent; 5 6 public class SessionAttributeListener implements HttpSessionAttributeListener{ 7 8 @Override 9 public void attributeAdded(HttpSessionBindingEvent HttpSessionBindingEvent) { 10 // TODO Auto-generated method stub 11 System.out.println("添加的属性名:"+HttpSessionBindingEvent.getName()+"属性值:"+HttpSessionBindingEvent.getValue()); 12 } 13 14 @Override 15 public void attributeRemoved(HttpSessionBindingEvent HttpSessionBindingEvent) { 16 // TODO Auto-generated method stub 17 System.out.println("删除的属性名:"+HttpSessionBindingEvent.getName()+"属性值 :"+HttpSessionBindingEvent.getValue()); 18 } 19 20 @Override 21 public void attributeReplaced(HttpSessionBindingEvent arg0) { 22 // TODO Auto-generated method stub 23 24 } 25 26 }
退出登录的java代码:
1 package com.java1234.web; 2 import java.io.IOException; 3 import java.io.PrintWriter; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 public class LogoutServlet extends HttpServlet{ 9 10 /** 11 * 12 */ 13 private static final long serialVersionUID = 1L; 14 @Override 15 protected void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 // TODO Auto-generated method stub 18 this.doPost(request,response); 19 } 20 21 @Override 22 protected void doPost(HttpServletRequest request, HttpServletResponse response) 23 throws ServletException, IOException { 24 // TODO Auto-generated method stub 25 //清除session 26 request.getSession().invalidate(); 27 response.sendRedirect("Login.jsp"); 28 } 29 @Override 30 public void destroy() { 31 // TODO Auto-generated method stub 32 33 } 34 @Override 35 public void init() throws ServletException { 36 // TODO Auto-generated method stub 37 38 } 39 }
配置web.xml
1 <!-- 添加监听器 --> 2 <listener> 3 <listener-class> 4 com.java1234.listener.SessionAttributeListener 5 </listener-class> 6 </listener> 7 <!-- 设置logout的servlet --> 8 <servlet> 9 <servlet-name>LogoutServlet</servlet-name> 10 <servlet-class>com.java1234.web.LogoutServlet</servlet-class> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>LogoutServlet</servlet-name> 14 <url-pattern>/logout</url-pattern> 15 </servlet-mapping>
启动登录------>注销
运行结果
至此ok了。。
以上是关于Servlet开发的主要内容,如果未能解决你的问题,请参考以下文章
java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理(代码片段