第N页/共M页 首页 上一页 1 2 3 4 5 6 7… 下一页 尾页 页面的数据都是Servlet传递来的 Servlet需要的数据:
当前页:pageCode,pc,如果页面没有传递当前页码,默认为第一页总页数:totalPages,tp,为总记录数/每页记录数总记录数:totalReport,tr,dao获取:select count(*) from t_customer每页记录数当前页数据: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):移除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抛弃了我:(
a.jsp
<body> <h1>向session中保存数据</h1> <% session.setAttribute("xxx","(~ ̄▽ ̄session"); %> </body>b.jsp
<body> <h1>获取session中的数据</h1> <% out.print(session.getAttribute("xxx")); %> </body> 启动服务器,访问a.jsp,访问b.jsp–>(~ ̄▽ ̄session启动服务器,访问a.jsp,重启服务器,访问b.jsp—>仍然有(~ ̄▽ ̄session 原因是关闭服务器时,自动生成了一个SESSIONS.ser的文件,用于保存所有的session,服务器启动时,该文件被读取后又消失 context.xml中增加<Manager pathname="">(取消其注释即可)能关闭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:
一分钟后被钝化保存到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一起回来了!
即一个页面可以根据用户需要转换语言。 实现:
我们写两个配置文件,分别存放中英文信息 资源文件名称格式:基本名称+_Loclale+.properties 如:res_zh_CN.properties(其中res为基本名称)使用一个类(ResourceBundle)根据语言环境(Locale)判断加载哪一个文件的信息 举例: res_en_US.properties中添加键值对 login=Login username=Username password=Passwordres_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>