方法一:网址重写
通过在url地址后面添加若干的token作为查询字符串来实现。token的值一般为 键=值
url?key1=value1&key2=value2&...&keyn=valuen
url与token之间需要用?分开,两个token之间则是需要用一个&符号隔开。
此方法适用于token不需要在多个页面中使用时使用。
缺点是
a.在某些浏览器当中url长度有限制
b.url中的信息是可见的,安全性差
c.某些字符需要进行编码
package com.SessionManage.Test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Top10Servlet", urlPatterns = {"/top10"})
public class Top10Servlet extends HttpServlet {
private static final long serialVersionUID = 987654321L;
private List londonAttractions;
private List parisAttractions;
@Override
public void init() throws ServletException {
londonAttractions = new ArrayList(10);
londonAttractions.add("Buckingham Palace");
londonAttractions.add("London Eye");
londonAttractions.add("British Museum");
londonAttractions.add("National Gallery");
londonAttractions.add("Big Ben");
londonAttractions.add("Tower of London");
londonAttractions.add("Natural History Museum");
londonAttractions.add("Canary Wharf");
londonAttractions.add("2012 Olympic Park");
londonAttractions.add("St Paul's Cathedral");
parisAttractions = new ArrayList(10);
parisAttractions.add("Eiffel Tower");
parisAttractions.add("Notre Dame");
parisAttractions.add("The Louvre");
parisAttractions.add("Champs Elysees");
parisAttractions.add("Arc de Triomphe");
parisAttractions.add("Sainte Chapelle Church");
parisAttractions.add("Les Invalides");
parisAttractions.add("Musee d'Orsay");
parisAttractions.add("Montmarte");
parisAttractions.add("Sacre Couer Basilica");
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String city = request.getParameter("city");
if(city!=null&&(city.equals("london")||city.equals("paris"))){
showAttractions(request,response,city);
}else{
showMainPage(request,response);
}
}
private void showMainPage(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("
"+ "
Top 10 Tourist Attractions"+ "
"+"please select a city:"
+"London"
+"Paris"
+"");
}
private void showAttractions(HttpServletRequest request,
HttpServletResponse response, String city) throws ServletException,IOException {
// TODO Auto-generated method stub
int page = 1;
String pageParameter = request.getParameter("page");
if(pageParameter!=null){
try{
page = Integer.parseInt(pageParameter);
}catch(NumberFormatException e){
e.printStackTrace();
}
if(page>2){
page = 1;
}
}
List attractions = null;
if(city.equals("london")){
attractions = londonAttractions;
}else if(city.equals("paris")){
attractions = parisAttractions;
}
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("
"+"
Top 10 Tourist Attractions"+"
");writer.println("Select City");
writer.println("
Page"+page+"
");
int start = page*5-5;
for(int i = start; i < start+5; i++){
writer.println(attractions.get(i)+"
");
}
writer.print("
"
+ "Page 1");
writer.print(" Page 2");
writer.println("");
}
}
方法二:隐藏域
主要适用于页面当中含有表单的情况,当用户提交表单时,隐藏域中的值也传送到服务器。只有当页面包含表单,或者可以在页面添加表单时,才适合使用隐藏域。
此技术胜过网址重写的地方在于可以将更多的字符传递到服务器,且不需要进行字符编码。但仅当所需传递的信息不需要跨越多个页面时,才适合使用这种技术。
package com.SessionManage.Test2;
public class Customer {
private int id;
private String name;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package com.SessionManage.Test2;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "CustomerServlet",urlPatterns = {
"/customer","/editCustomer","/updateCustomer"})
public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = -20L;
private List customers = new ArrayList();
@Override
public void init() throws ServletException{
Customer customer1 = new Customer();
customer1.setId(1);
customer1.setName("Donald D.");
customer1.setCity("Miami");
customers.add(customer1);
Customer customer2 = new Customer();
customer2.setId(2);
customer2.setName("Mickey M.");
customer2.setCity("Orlando");
customers.add(customer2);
}
private void sendCustomerList(HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("
Customer"+"
Customers
");writer.println("
- ");
for(Customer customer : customers){
writer.println("
"+customer.getName()+"("+customer.getCity()+") "
+"edit");
}
writer.println("
");writer.println("");
}
private Customer getCustomer(int customerId){
for(Customer customer : customers){
if(customer.getId()==customerId){
return customer;
}
}
return null;
}
private void sendEditCustomerForm(HttpServletRequest request,HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
int customerId = 0;
try{
customerId = Integer.parseInt(request.getParameter("id"));
}catch(NumberFormatException e){
e.printStackTrace();
}
Customer customer = this.getCustomer(customerId);
if(customer!=null){
writer.println("
"+"
Edit Customer"+"
EditCustomer
"
+"
+"action='updateCustomer'>");
writer.println("");
writer.println("
writer.println("
Name:"+"
+"'/>
");writer.println("
City:"+"
+"'/>
");writer.println("
"+"
"+"
"+"
");writer.println("
"+"Customer List"
+"
");writer.println("
");writer.println("
");}else{
writer.println("No customer found");
}
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
String uri = request.getRequestURI();
if(uri.endsWith("/customer")){
this.sendCustomerList(response);
}else if(uri.endsWith("/editCustomer")){
this.sendEditCustomerForm(request, response);
}
}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException{
int customerId = 0;
try{
customerId = Integer.parseInt(request.getParameter("id"));
}catch(NumberFormatException e){
e.printStackTrace();
}
Customer customer = this.getCustomer(customerId);
if(customer!=null){
customer.setName(request.getParameter("name"));
customer.setCity(request.getParameter("city"));
}
this.sendCustomerList(response);
}
}
方法三:cookie
cookie信息可以跨越多个页面,这点是采用网址重写和隐藏域所无法实现的。cookie是自动在web服务器和浏览器之间传递的一小块信息。
cookie适用于那些需要跨越许多页面的信息。因为cookie是作为http标头嵌入的,因此传输它的过程由http协议处理。此外,可以根据自
己的需要设置cookie的有效期限。对于web浏览器而言,每台web服务器最多可以支持20个cookie。
cookie的不足之处是用户可以通过修改其浏览器设置来拒绝接受cookie。
关于cookie的代码如下:
package com.SessionManage.Test3;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="PreferenceServlet",urlPatterns={"/preference"})
public class PreferenceServlet extends HttpServlet {
private static final long serialVersionUID = 888L;
public static final String MENU =
"
+"Cookie Class "
+"Cookie Info "
+"Preference"+"
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("
"+"Preference"+"
+"background:NavajoWhite}"
+"
"+MENU
+"Please select the values below:"
+"
"+"
+"
Title Font Size:"+"
"+"large"
+"x-large"
+"xx-large"
+"
"+"
"+"
Title Style & Weight:"+"
"+"italic"
+"bold"
+"
"+"
"+"
Max. Records in Table: "+"
"+"5"
+"10"
+"
"+"
"+"
"+"
"+"
"+"
"+""+"");}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String maxRecords = request.getParameter("maxRecords");
String[] titleStyleAndWeight = request.getParameterValues("titleStyleAndWeight");
String titleFontSize = request.getParameter("titleFontSize");
response.addCookie(new Cookie("maxRecords",maxRecords));
response.addCookie(new Cookie("titleFontSize",titleFontSize));
Cookie cookie = new Cookie("titleFontWeight","");
cookie.setMaxAge(0);
response.addCookie(cookie);
cookie = new Cookie("titleFontStyle","");
cookie.setMaxAge(0);
response.addCookie(cookie);
if(titleStyleAndWeight!=null){
for(String style : titleStyleAndWeight){
if(style.equals("bold")){
response.addCookie(new Cookie("titleFontWeight","bold"));
}else if(style.equals("italic")){
response.addCookie(new Cookie("titleFontStyle","italic"));
}
}
}
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("
"+"Preference"+"
"+MENU+"Your preference has been set."
+"
Max. Records in Table: "+maxRecords
+"
Title Font Size: "+titleFontSize
+"
Title Font Style & Weight: ");
if(titleStyleAndWeight!=null){
writer.println("
- ");
for(String style : titleStyleAndWeight){
writer.print("
"+style+"");}
writer.println("
");}
writer.println("");
}
}
package com.SessionManage.Test3;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="CookieClassServlet",urlPatterns={"/cookieClass"})
public class CookieClassServlet extends HttpServlet{
private static final long serialVersionUID = 837369L;
private String[] methods = {
"clone","getComment","getDomain",
"getMaxAge","getName","getPath",
"getSecure","getValue","getVersion",
"isHttpOnly","setComment","setDomain",
"setHttpOnly","setMaxAge","setPath",
"setSecure","setValue","setVersion"
};
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
Cookie[] cookies = request.getCookies();
Cookie maxRecordsCookie = null;
if(cookies!=null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("maxRecords")){
maxRecordsCookie = cookie;
break;
}
}
}
int maxRecords = 5;
if(maxRecordsCookie!=null){
try{
maxRecords = Integer.parseInt(maxRecordsCookie.getValue());
}catch(NumberFormatException e){
e.printStackTrace();
}
}
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("
"+"Cookie Class"+"
"+PreferenceServlet.MENU
+"
"javax.servlet.http.Cookie");
writer.print("
- ");
for(int i = 0;i < maxRecords; i++){
writer.print("
"+methods[i]+"");}
writer.print("
");writer.print("
}
}
package com.SessionManage.Test3;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Cookie;
@WebServlet(name="CookieInfoServlet",urlPatterns={"/cookieInfo"})
public class CookieInfoServlet extends HttpServlet {
private static final long serialVersionUID = 3829L;
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
javax.servlet.http.Cookie[] cookies = request.getCookies();
StringBuilder styles = new StringBuilder();
styles.append(".title{");
if(cookies!=null){
for(javax.servlet.http.Cookie cookie : cookies){
String name = cookie.getName();
String value = cookie.getValue();
if(name.equals("titleFontSize")){
styles.append("font-size:"+value+";");
}else if(name.equals("titleFontWeight")){
styles.append("font-weight:"+value+";");
}else if(name.equals("titleFontStyle")){
styles.append("font-style:"+value+";");
}
}
}
styles.append("}");
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("
"+"Cookie Info"+""
+"
"+PreferenceServlet.MENU+"
+"Session Management with Cookies:
writer.print("
if(cookies==null){
writer.print("No cookie in this Http response");
}else{
writer.println("
Cookie in this Http response");
for(javax.servlet.http.Cookie cookie : cookies){
writer.println("
"+cookie.getName()+":"+cookie.getValue());
}
}
writer.print("
writer.print("");
}
}
方法四:HttpSession对象
用户可以没有或者有一个HttpSession,并且只能访问自己的HttpSession。HttpSession是当一个用户第一次访问某个站点时创建的。通过request中getSession()方法,可以获取用户的HttpSession。而通过HttpSession的setAttribut(name,value)方法可以将值放入到HttpSession当中。
同网址重写、隐藏域和cookie所不同的地方在于,放在HttpSession中的值是保存在内存中的。因此,你只能将尽可能小的对象放在里面,并且数量不能太多。
添加到HttpSession当中的值不一定是String,可以为任意的java对象,只要它的类实现了java.io.Serializable接口即可,以便当Servlet容器认为有必要的时候,保存的对象可以序列化成一个文件或者保存到数据库中。
setAttribute方法要求不同的对象有不同的名称。如果传递一个之前用过的属性名称,那么该名称将与旧值无关联,而与新值关联。
通过HttpSession的getAttribute(name)属性可以获取HttpSession中保存的对象。其另外一个方法getAttributeNames()返回一个Enumeration,迭代一个HttpSession中的
所有属性。
注:HttpSession中保存的值不发送到客户端,这与其它的Session管理方法不同。而是Servlet容器为它所创建的每一个HttpSession生成一个唯一标示符,并将这个标示符作为一个token发送给浏览器,一般是作为一个名为JSESSIONID的cookie,或者作为一个jsessionid参数添加到url后面。在后续的请求中,浏览器会将这个token发送回服务器,使服务器知道是哪个用户在发出请求。
相关代码如下:
package com.SessionManage.Test4;
public class Product {
private int id;
private String name;
private String description;
private float price;
public Product(int id,String name,String description,float price){
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
package com.SessionManage.Test4;
public class ShoppingItem {
private Product product;
private int quantity;
public ShoppingItem(Product product,int quantity){
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
package com.SessionManage.Test4;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(name="ShoppingCartServlet",urlPatterns={"/products","/viewProductDetails",
"/addToCart","/viewCart"
})
public class ShoppingCartServlet extends HttpServlet {
private static final long serialVersionUID = -20L;
private static final String CART_ATTRIBUTE = "cart";
private List products = new ArrayList();
private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
@Override
public void init() throws ServletException{
products.add(new Product(1,"Bravo 32' HDTV","Low-cost HDTV from renowned TV manufacturer",159.95F));
products.add(new Product(2,"Bravo BluRay Player","High quality stylish BluRay player",99.95F));
products.add(new Product(3,"Bravo Stereo System","5 speaker hifi system with iPod player",129.95F));
products.add(new Product(4,"Bravo iPod player","An iPod plug-in that can play multiple formats",39.95F));
}
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String uri = request.getRequestURI();
if(uri.endsWith("/products")){
sendProductList(response);
}else if(uri.endsWith("/viewProductDetails")){
sendProductDetail(request,response);
}else if(uri.endsWith("/viewCart")){
showCart(request,response);
}
}
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
int productId = 0;
int quantity = 0;
try{
productId = Integer.parseInt(request.getParameter("id"));
quantity = Integer.parseInt(request.getParameter("quantity"));
}catch(NumberFormatException e){
e.printStackTrace();
}
Product product = getProduct(productId);
if(product!=null&&quantity>=0){
ShoppingItem shoppingItem = new ShoppingItem(product,quantity);
HttpSession session = request.getSession();
List cart = (List)session.getAttribute(CART_ATTRIBUTE);
if(cart==null){
cart = new ArrayList();
session.setAttribute(CART_ATTRIBUTE, cart);
}
cart.add(shoppingItem);
}
sendProductList(response);
}
private Product getProduct(int productId) {
// TODO Auto-generated method stub
for(Product product : products){
if(product.getId()==productId){
return product;
}
}
return null;
}
private void showCart(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("
Shopping Cart"+"");
writer.println("
"+"Product List
");HttpSession session = request.getSession();
List cart = (List)session.getAttribute(CART_ATTRIBUTE);
if(cart!=null){
writer.println("
writer.println("
Quantity"+"
"+"
Product"+"
Price"+"
Amout");double total = 0.0;
for(ShoppingItem shoppingItem : cart){
Product product = shoppingItem.getProduct();
int quantity = shoppingItem.getQuantity();
if(quantity!=0){
float price = product.getPrice();
writer.println("
");writer.println("
"+quantity+"");writer.println("
"+product.getName()+"");writer.println("
"+currencyFormat.format(price)+"");double subtotal = price*quantity;
writer.println("
"+currencyFormat.format(subtotal)+"");total += subtotal;
writer.println("
");}
}
writer.println("
+"style='text-align:right'>"
+"Total:"
+currencyFormat.format(total)
+"
");}
writer.println("
");}
private void sendProductDetail(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
int productId = 0;
try{
productId = Integer.parseInt(request.getParameter("id"));
}catch(NumberFormatException e){
e.printStackTrace();
}
Product product = this.getProduct(productId);
if(product!=null){
writer.println("
"+"
Product Details"+"
Product Details
"+"
");writer.println("
+"value='"+productId+"'/>");
writer.println("
writer.println("
Name:"+product.getName()+"
");writer.println("
Description:"+product.getDescription()+"
");writer.println("
"+""+"
"+"
"+"
"+"
");writer.println("
"+"Product List"
+"
");writer.println("
");writer.println("
");}else{
writer.println("No product found");
}
}
private void sendProductList(HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("
Products"+"
Products
");writer.println("
- ");
for(Product product :products){
writer.println("
"+product.getName()+"("+currencyFormat.format(product.getPrice())
+") ("+"Details)");
}
writer.println("
");writer.println("View Cart");
writer.println("");
}
}