1.Listener
1.1Servlet的监听器
总共有8个,分为3种类型:
1.1.1.监听三个作用域的创建和销毁【常用】
1.ServletContextListener
利用它在ServletContext创建的时候,完成自己想要的初始化工作; 执行自定义的任务调度;
2.ServletRequestListener
3.HttpSessionListener
统计在线人数
使用方法:1.定义一个类,实现接口;
public class MyServletContextListener implements ServletContextListener {
@Override//初始化
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("这是初始化状态..........");
}
@Override//销毁监听器
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("已销毁.............");
}
}
2.配置监听器:web.xml里配置
<listener>
<listener-class>zheng.MyServletContextListener</listener-class>
</listener>
1.1.2.监听三个作用域属性状态变更【了解】
可以监听在作用域中值的添加,替换,移除的动作【3个】
session监听器:
HttpSessionAttributeListener;
request监听器:
ServletRequestAttributeListener;
servletContext监听器:
ServletContextAtrributeListener;
三个的写法基本相同,session监听器做示例
1 页面
<body>
<%
session.setAttribute("name","lisi");
/* 替换 */
session.setAttribute("name", "aobama");
/* 移除 */
session.removeAttribute("name");
%>
</body>
2.MyHttpSession类实现session监听器的接口HttpSessionAttributeListener
public class MyHttpSession implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("属性被添加");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("属性被移除");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("属性被替代");
}
}
3.web.xml里配置
<listener>
<listener-class>zheng.MyHttpSession</listener-class>
</listener>
4.浏览器访问页面
http://localhost:8077/ListenerDemo/demo1/demo1.jsp
对应的控制台监听
属性被添加
属性被替代
属性被移除
1.1.3.监听HttpSession里的存值状态变更;
这类监听器不需要注册【2个】
HttpSessionBindingListener【了解】
用于监听对象与session绑定和解除绑定
1.让javaBean实现该接口
public class Bean implements HttpSessionBindingListener {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Bean(String name) {
super();
this.name = name;
}
public Bean() {
super();
}
@Override
public String toString() {
return "Bean [name=" + name + "]";
}
@Override
public void valueBound(HttpSessionBindingEvent arg0) {
System.out.println("值被绑定");
}
@Override
public void valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println("解除绑定");
}
}
2.jsp
<%
Bean bean = new Bean();
bean.setName("lisi");
session.setAttribute("bean", bean);
session.removeAttribute("bean");
%>
访问页面控制台会监听到javaBean里的数据
HttpSesssionActivationListener
用于监听现在session的值是钝化还是活化
钝化(序列化):把内存数据存储到硬盘上;
活化(反序列化):把硬盘数据存储到内存中;
Filter
模拟自动登陆案例实现
1.分析
前期准备:准备好必要的jar文件;创建好数据库,c3p0连接数据库,JDBCUtil连接对象;
第一步:创建login.jsp登陆页面
<body>
<form action="LoginServlet" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="checkbox" name="autologin">自动登陆<br>
<input type="submit" value="登陆">
</form>
</body>
第二步:创建LoginServlet收jsp页面传来的数据;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String autologin = request.getParameter("autologin");
System.out.println("username:"+username+"===="+"&&password:"+password+"=="+autologin);
//将用户信息封装到user,然后传给login方法
UserBean user = new UserBean();
user.setUsername(username);
user.setPassword(password);
UserDao dao = new UserDaoImpl();
UserBean userBean = dao.login(user);
System.out.println("userBean======="+userBean);
//判断是否登陆成功
if(userBean != null){//登陆成功:跳转新页面
//页面提交是否勾选了自动登陆
if("on".equals(autologin)){
//发送cookie给客户端
Cookie cookie = new Cookie("auto_login", username+"==="+password);
cookie.setMaxAge(60*60*24*7);//7天
cookie.setPath(request.getContextPath());//那个网站
response.addCookie(cookie);
}
//存值:用于succes.jsp页面获取该数据
request.getSession().setAttribute("userBean", userBean);
response.sendRedirect("success.jsp");
}else{
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
第三步:domain层创建UserBean用户类
public class UserBean {
private int id;
private String username;
private String password;
public UserBean() {
super();
}
public UserBean(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserBean [id=" + id + ", username=" + username + ", password="
+ password + "]";
}
}
第四步:创建dao层UserDao及其实现类
/**
* @author Leonard
* 执行登陆并且返回该用户所有信息
*
* user:执行登陆的用户数量 ;封装成一个对象
*/
public interface UserDao {
UserBean login(UserBean user) throws SQLException;
}
public class UserDaoImpl implements UserDao {
@Override
public UserBean login(UserBean user) throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());
String sql = "select * from t_user where username=? and password=?";
UserBean query = runner.query(sql, new BeanHandler<UserBean>(UserBean.class), user.getUsername(),user.getPassword());
return query;
}
}