【JavaWeb】18 JDBC分页/监听器/session

分页(待续)

第N页/共M页 首页 上一页 1 2 3 4 5 6 7… 下一页 尾页
页面的数据都是Servlet传递来的
Servlet需要的数据:

  1. 当前页:pageCode,pc,如果页面没有传递当前页码,默认为第一页
  2. 总页数:totalPages,tp,为总记录数/每页记录数
  3. 总记录数:totalReport,tr,dao获取:select count(*) from t_customer
  4. 每页记录数
  5. 当前页数据:beanList

这些分页数据总要在各层之间来回的传递,我们把这些分页数据封装到一个Javabean中

监听器

  • 是一个接口,需要我们实现内容
  • 需要注册,例如注册在按钮上
  • 监听器中的方法,在特殊事件发生时被调用

JavaWeb中的监听器

事件源:三大域

ServletContext

  • 生命周期监听:ServletContextListener:两个方法,一个出生时调用,一个死亡时调用
    void contextInitialized(ServletContextEvent sce):创建
    void contextDestroyed(ServletContextEvent sce):销毁
  • 属性监听:ServletContextAttributeListener:三个方法,一个在添加属性时调用,一个替换属性时调用,一个移除属性时调用
    void attributeAdded(ServletContextAttributeEvent event):添加
    void attributeReplaced(ServletContextAttributeEvent event):替换
    void attributeRemoved(ServletContextAttributeEvent event):移除

HttpSession

  • HttpSessionListener
    void sessionCreated(HttpSessionEvent se):创建
    void sessionDestroyed(HttpSessionEvent se):销毁
  • HttpSessionAttributeListener
    void attributeAdded(HttpSessionBindingEvent event):添加
    void attributeReplaced(HttpSessionBindingEvent event):替换
    void attributeRemoved(HttpSessionBindingEvent event):移除

ServletRequest

  • ServletRequestListener
    void requestInitialized(ServletRequestEvent sre):创建
    void requestDestroyed(ServletRequestEvent sre):销毁
  • ServletRequestAttributeListener
    void attributeAdded(ServletRequestAttributeEvent event):添加
    void attributeReplaced(ServletRequestAttributeEvent event):替换
    void attributeRemoved(ServletRequestAttributeEvent event):移除

事件对象

  • ServletContextEvent:ServletContext getServletContext()
  • HttpSessionEvent:HttpSession getSession()
  • ServletRequest:
    ServletContext getServletContext()
    ServletRequest getServletRequest()
  • ServletContextAttributeEvent:
    ServletContext getServletContext()
    String getName()
    String getValue()
  • HttpSessionBindingEvent…
  • ServletRequestAttributeEvent…

感知监听

  • 用来添加到JavaBean上,而不是三大域
  • 都不需要在web.xml中注册
  • 都与HttpSession相关
    HttpSessionBidingListener:添加到JavaBean上,JavaBean就知道自己在session被添加/移除
public class User implements HttpSessionBindingListener{
	private String username;
	private String password;
	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;
	}
	public void valueBound(HttpSessionBindingEvent event) {//添加对象监听
		System.out.println("session添加了我:)");
	}
	public void valueUnbound(HttpSessionBindingEvent event) {//移除对象监听
		System.out.println("session抛弃了我:(");
	}
	
}

add.jsp

<body>
/* 感知监听测试——添加对象 */
<%
listener.User user=new listener.User();		
session.setAttribute("user",user);
%>
</body>

remove.jsp

<body>
/* 感知监听测试——移除对象 */
<%
session.removeAttribute("user");	
%>
</body>

访问add.jsp页面后,控制台打印:session添加了我:)
访问remove.jsp页面后,控制台打印:session抛弃了我:(

session

session的序列化

a.jsp

<body>
<h1>向session中保存数据</h1>
<%
session.setAttribute("xxx","(~ ̄▽ ̄session");
%>
</body>

b.jsp

<body>
<h1>获取session中的数据</h1>
<%
out.print(session.getAttribute("xxx"));
%>
</body>
  1. 启动服务器,访问a.jsp,访问b.jsp–>(~ ̄▽ ̄session
  2. 启动服务器,访问a.jsp,重启服务器,访问b.jsp—>仍然有(~ ̄▽ ̄session
    原因是关闭服务器时,自动生成了一个SESSIONS.ser的文件,用于保存所有的session,服务器启动时,该文件被读取后又消失
    context.xml中增加<Manager pathname="">(取消其注释即可)能关闭session的序列化

session的钝化和活化

当session数量很多,服务器将长时间未使用的session保存到硬盘上(钝化),需要使用时,再从硬盘读取(活化)
配置方法:
将下面的配置文件放到tomcat\conf\catalina\localhost目录下,文件名为项目名称:

<Context>
	<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
	<Store className="org.apache.catalina.session.FileStore" directory="mysession"/>
	</Manager>
</Context>

以上代码使得session:

  1. 一分钟后被钝化
  2. 保存到Tomcat\work\Catalina\localhost\listener\mysession文件夹中,一个session一个文件保存
    测试:
public class User implements HttpSessionActivationListener,java.io.Serializable{
	
	public void sessionDidActivate(HttpSessionEvent se) {
		System.out.println("和session一起回来了!");
	}

	public void sessionWillPassivate(HttpSessionEvent se) {
		System.out.println("和session去硬盘了!~");
	}
}

a.jsp

<body>
<h1>向session中保存数据</h1>
<%
session.setAttribute("xxx",new session.User());
%>
</body>

b.jsp

<body>
<h1>获取session中的数据</h1>
<%
out.print(session.getAttribute("xxx"));
%>
</body>

开启服务器,访问a.jsp,访问b.jsp,等待一分钟后,控制台打印:和session去硬盘了!~
再次访问b.jsp,控制台打印:和session一起回来了!

国际化

即一个页面可以根据用户需要转换语言。
实现:

  1. 我们写两个配置文件,分别存放中英文信息
    资源文件名称格式:基本名称+_Loclale+.properties
    如:res_zh_CN.properties(其中res为基本名称)
  2. 使用一个类(ResourceBundle)根据语言环境(Locale)判断加载哪一个文件的信息
    举例:
    res_en_US.properties中添加键值对
login=Login
username=Username
password=Password

res_zh_CN.properties中添加键值对:

login=登录
username=用户名
password=密码

Demo1.java中,改变语言环境即可输出不同语言

public class Demo1 {
	@Test
	public void fun1() {
		//创建语言环境
		Locale locale=Locale.US;
		
		//得到ResourceBundle。使用ResourceBundle.getBundle("基本名称",locale);
		ResourceBundle rb=ResourceBundle.getBundle("res",locale);
		
		//使用ResourceBundle获取资源信息
		System.out.println(rb.getString("username"));
		System.out.println(rb.getString("password"));
		System.out.println(rb.getString("login"));
	}
}

在网页中,浏览器请求头中包含如:Accept-Language:zh-CN,en-US;的信息,服务器使用Locale locale.getLocale();得到浏览器使用的语言

<body>
<%--
1.创建语言环境
2.创建ResourceBundle
3.替换所有文字,从rb中获取信息
--%>
<%
Locale locale=request.getLocale();
ResourceBundle rb=ResourceBundle.getBundle("res",locale);
%>
<h1><%=rb.getString("login") %></h1>
<form>
<%=rb.getString("username") %>:<input type="text" name="username"/><br/>
<%=rb.getString("password") %>:<input type="password" name="password"/><br/>
<input type="submit" value="<%=rb.getString("login") %>"/>
</form>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值