- 论坛徽章:
- 0
|
JSP开发中常用技巧
1. 在不同页面或者用户之间共享数据
在JSP中共享数据,大体上分为两种情况,第一种是在同一个用户的不同页面之间共享数据,另一种是在不同用户之间共享数据。
对于同一个用户会话,要想在不同的页面之间共享数据,可以分为以下几种:
- 把数据保存在Session中(这是非常常见的方式);
- 通过Cookie;
- 通过隐含的表单把数据提交到下一个页面;
- 通过ServletContext对象;
- 通过Application对象;
- 通过文件系统或者数据库;
在不同用户之间共享数据,通常的方法是:
- 通过ServletContext对象;
- 通过Application对象;
- 通过文件系统或者数据库;
可见,对于不用用户之间共享数据的实现方法在同一个用户的不同页面也能实现数据共享。
1.1 在不同页面之间共享数据
1) 使用Session共享数据
用户浏览网页时,由于Http协议是一种无状态的协议,往往在不同的页面之间存在数据交换的问题,这就需要在这些不同的页面之间共享数据,在编程实现中,我们最常用的实现方式就是把要共享的数据保存到Session中。比如在用户登录的页面中把一些用户的信息保存到Session中,然后在其他的页面中读取用户的信息。
这些共享的数据可以是字符串或者于Java的原始数据类型相关的对象,也可以是一个Java对象。
下面来看一个实际的例子,用户登录时,如果验证成功,就把登录的信息保存在一个userSession的类中。在其他的页面读取这个值。
userSession.java
package com.starxing.ch10;import java.util.Date;public class userSession { private boolean isLogin=false; private String userId; private Date lastLoginTime; private int logCount; public void setIsLogin(boolean isLogin) { this.isLogin = isLogin; } public void SetUserId(String userId) { this.userId = userId; } public void SetLastLoginTime(Date lastLoginTime) { this.lastLoginTime = lastLoginTime; } public void setLogCount(int logCount) { this.logCount = logCount; } public boolean getIsLogin() { return isLogin; } public String getUserId() { return userId; } public Date getLastLoginTime() { return lastLoginTime; } public int getLogCount() { return logCount; }}
index.jsp
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> "> My JSP 'index.jsp' starting page 用户名 密码
login.jsp
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> "> My JSP 'login.jsp' starting page --> String name = request.getParameter("name"); String password = request.getParameter("password"); //连接数据库 Class.forName("org.gjt.mm.mysql.Driver"); Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp","root","starxing"); Statement statement = con.createStatement(); String isCorrect="select * from user_info where userId='" + name + "' and password='"+password+"'" ; ResultSet result = statement.executeQuery(isCorrect); session.setAttribute("isLog",new String("0")); if(!result.next()) { response.sendRedirect("index.jsp");//???????????????? result.close(); statement.close(); con.close(); } else { //???????session??? userSession usersession = new userSession(); usersession.SetUserId(result.getString("name")); usersession.setIsLogin(true); usersession.SetLastLoginTime(new java.util.Date()); session.setAttribute("Usersession",usersession); statement.close(); con.close(); //把页面派发到目的 response.sendRedirect("welcome.jsp"); } %>
welcome.jsp
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> "> My JSP 'welcome.jsp' starting page --> userSession user = (userSession)session.getAttribute("Usersession"); if(user.getIsLogin()) { String name = user.getUserId(); Date date = user.getLastLoginTime(); %> 登录成功,欢迎。登录时间为() } else { response.sendRedirect("index.jsp"); } %>
也可以使用javaBean的方式,可是在login.jsp中使用javaBean。
注意,这里的JavaBean的范围是session。同样在另一个页面中也可以使用JavaBean来获得数据。
2) cookie
和Session不同Cookie是放在客户端的,由于客户端可能考虑安全等因素会禁止使用Cookie,这样在使用Cookie时可能会遇到一些麻烦。
例程 在客户端设置Cookie(setCookie.jsp)
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> "> My JSP 'setCookie.jsp' starting page Cookie cookie = new Cookie("lastTime",new java.util.Date().toLocaleString()); response.addCookie(cookie); %> 已经设置了cookie,你可以在这里使用他。
getCookie.jsp
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> "> My JSP 'getCookie.jsp' starting page --> Cookie[] cookies = request.getCookies(); for(int i=0;i { Cookie c = cookies; out.println("sss"+c.getValue()); } %>
3) 使用隐含表单
这种方式通过隐含表单的形式把数据传递到下一个页面,他有两个局限性:
- 只能在相邻的两个页面之间传输数据
- 客户端可以通过查看源代码的方式看到数据,安全性不好。
1.2 在不同的用户之间共享数据。
在不同页面之间共享数据的最常见方法是使用ServletContext和application对象,在一个用户那里设置一个属性,在另外一个用户那里可以获得这个属性。
1)使用ServletContext
在JSP页面中可以通过getServletContext()方法获取ServletContext对象。
在这中情况下,不同用户的页面之间可以共享数据,当然也可以在同一个用户不用页面共享数据。
聊天室常用这种方式。
简单的聊天室程序
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> "> My JSP 'chat.jsp' starting page --> String content = (String)getServletContext().getAttribute(new String("chatTopic_1")); getServletContext().setAttribute("chatTopic_1",content+(String)request.getParameter("content")+"
"); out.println(content); %>
2) 使用Application对象
application对象对于每个Web应用来说只有一个,他的使用和ServletContext差不多,下面我们简单地看一个应用。
testApplication.jsp
String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> "> My JSP 'chat.jsp' starting page --> String content = (String)application.getAttribute(new String("chatTopic_1")); application.setAttribute("chatTopic_1",content+(String)request.getParameter("content")+"
"); out.println(content); %>
2 JSP操作文件
在读取文件时可以有两种选择方式,一种是通过ServletContext来读取,另一种四直接使用java.io.FileReader等对象。前者只能获取Servlet上下文的文件,或者可以读取任何位置的文件。
2.1 使用ServletContext读取文件
readFile.jsp
">
My JSP 'readFile.jsp' starting page
采用缓冲机制的读取文件
readFiles.jsp
">
My JSP 'readFiles.jsp' starting page
2.2 使用FileReader
readfile3.jsp
">
My JSP 'readfile3.jsp' starting page
-->
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/28722/showart_219222.html |
|