方法一:网址重写
通过在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<String> londonAttractions;
private List<String> parisAttractions;
@Override
public void init() throws ServletException {
londonAttractions = new ArrayList<String>(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<String>(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("<html><head>"
+ "<title>Top 10 Tourist Attractions</title>"
+ "</head><body>"
+"please select a city:"
+"<br/><a href='?city=london'>London</a>"
+"<br/><a href='?city=paris'>Paris</a>"
+"</body></html>");
} 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<String> 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("<html><head>"
+"<title>Top 10 Tourist Attractions</title>"
+"</head><body>");
writer.println("<a href='top10'>Select City</a>");
writer.println("<hr/>Page"+page+"<hr/>"); int start = page*5-5;
for(int i = start; i < start+5; i++){
writer.println(attractions.get(i)+"<br/>");
}
writer.print("<hr style='color:blue'/>"
+ "<a href='?city="+city
+"&page=1'>Page 1</a>");
writer.print(" <a href='?city="
+city+"&page=2'>Page 2</a>");
writer.println("</body></html>"); }
}
方法二:隐藏域
主要适用于页面当中含有表单的情况,当用户提交表单时,隐藏域中的值也传送到服务器。只有当页面包含表单,或者可以在页面添加表单时,才适合使用隐藏域。
此技术胜过网址重写的地方在于可以将更多的字符传递到服务器,且不需要进行字符编码。但仅当所需传递的信息不需要跨越多个页面时,才适合使用这种技术。
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<Customer> customers = new ArrayList<Customer>(); @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("<html><head><title>Customer</title></head>"
+"<body><h2>Customers </h2>");
writer.println("<ul>");
for(Customer customer : customers){
writer.println("<li>"+customer.getName()
+"("+customer.getCity()+") "
+"<a href='editCustomer?id="+customer.getId()
+"'>edit</a>");
}
writer.println("</ul>");
writer.println("</body></html>");
} 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("<html><head>"
+"<title>Edit Customer</title></head>"
+"<body><h2>EditCustomer<h2>"
+"<form method='post'"
+"action='updateCustomer'>");
writer.println("<input type='hidden' name='id' value='"
+customerId+"'/>");
writer.println("<table>");
writer.println("<tr><td>Name:</td><td>"
+"<input name='name' value='"
+customer.getName().replaceAll("'", "'")
+"'/></td></tr>");
writer.println("<tr><td>City:</td><td>"
+"<input name='city' value='"
+customer.getCity().replaceAll("'", "'")
+"'/></td></tr>");
writer.println("<tr>"
+"<td colspan='2' style='text-align:right'>"
+"<input type='submit' value='Update'/></td>"
+"</tr>");
writer.println("<tr><td colspan='2'>"
+"<a href='customer'>Customer List</a>"
+"</td></tr>");
writer.println("</table>");
writer.println("</form></body>");
}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 =
"<div style='background:#e8e8e8;"
+"padding:15px'>"
+"<a href='cookieClass'>Cookie Class</a> "
+"<a href='cookieInfo'>Cookie Info</a> "
+"<a href='preference'>Preference</a>"+"</div>";
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("<html><head>"+"<title>Preference</title>"
+"<style>table{"+"font-size:small;"
+"background:NavajoWhite}</style>"
+"</head><body>"
+MENU
+"Please select the values below:"
+"<form method='post'>"
+"<table>"
+"<tr><td>Title Font Size:</td>"
+"<td><select name='titleFontSize'>"
+"<option>large</option>"
+"<option>x-large</option>"
+"<option>xx-large</option>"
+"</select></td>"
+"</tr>"
+"<tr><td>Title Style & Weight:</td>"
+"<td><select name='titleStyleAndWeight' multiple>"
+"<option>italic</option>"
+"<option>bold</option>"
+"</select></td>"
+"</tr>"
+"<tr><td>Max. Records in Table: </td>"
+"<td><select name='maxRecords'>"
+"<option>5</option>"
+"<option>10</option>"
+"</select></td>"
+"</tr>"
+"<tr><td rowspan='2'>"
+"<input type='submit' value='Set'/></td>"
+"</tr>"
+"</table>"+"</form>"+"</body></html>");
} @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("<html><head>"+"<title>Preference</title>"
+"</head><body>"+MENU
+"Your preference has been set."
+"<br/><br/>Max. Records in Table: "+maxRecords
+"<br/>Title Font Size: "+titleFontSize
+"<br/>Title Font Style & Weight: ");
if(titleStyleAndWeight!=null){
writer.println("<ul>");
for(String style : titleStyleAndWeight){
writer.print("<li>"+style+"</li>");
}
writer.println("</ul>");
}
writer.println("</body></html>");
}
}
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("<html><head>"+"<title>Cookie Class</title>"
+"</head><body>"
+PreferenceServlet.MENU
+"<div>Here are some of the methods in "+
"javax.servlet.http.Cookie");
writer.print("<ul>"); for(int i = 0;i < maxRecords; i++){
writer.print("<li>"+methods[i]+"</li>");
}
writer.print("</ul>");
writer.print("</div></body></html>");
}
}
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("<html><head>"+"<title>Cookie Info</title>"
+"<style>"+styles.toString()+"</style>"
+"</head><body>"+PreferenceServlet.MENU
+"<div class='title'>"
+"Session Management with Cookies:</div>");
writer.print("<div>"); if(cookies==null){
writer.print("No cookie in this Http response");
}else{
writer.println("<br/>Cookie in this Http response");
for(javax.servlet.http.Cookie cookie : cookies){
writer.println("<br/>"+cookie.getName()+":"+cookie.getValue());
}
}
writer.print("</div>");
writer.print("</body></html>");
}
}
方法四: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<Product> products = new ArrayList<Product>();
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<ShoppingItem> cart = (List<ShoppingItem>)session.getAttribute(CART_ATTRIBUTE);
if(cart==null){
cart = new ArrayList<ShoppingItem>();
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("<html><head><title>Shopping Cart</title>"
+"</head>");
writer.println("<body><a href='products'>"
+"Product List</a>");
HttpSession session = request.getSession();
List<ShoppingItem> cart = (List<ShoppingItem>)session.getAttribute(CART_ATTRIBUTE);
if(cart!=null){
writer.println("<table>");
writer.println("<tr><td style='width:150px'>Quantity"
+"</td>"
+"<td style='width:150px'>Product</td>"
+"<td style='width:150px'>Price</td>"
+"<td>Amout</td></tr>");
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("<tr>");
writer.println("<td>"+quantity+"</td>");
writer.println("<td>"+product.getName()+"</td>");
writer.println("<td>"+currencyFormat.format(price)+"</td>");
double subtotal = price*quantity;
writer.println("<td>"+currencyFormat.format(subtotal)+"</td>");
total += subtotal;
writer.println("</tr>");
}
}
writer.println("<tr><td colspan='4'"
+"style='text-align:right'>"
+"Total:"
+currencyFormat.format(total)
+"</td></tr>");
}
writer.println("</table></body></html>");
} 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("<html><head>"
+"<title>Product Details</title></head>"
+"<body><h2>Product Details</h2>"
+"<form method='post' action='addToCart'>");
writer.println("<input type='hidden' name='id' "
+"value='"+productId+"'/>");
writer.println("<table>");
writer.println("<tr><td>Name:</td><td>"
+product.getName()+"</td></tr>");
writer.println("<tr><td>Description:</td><td>"
+product.getDescription()+"</td></tr>");
writer.println("<tr>"+"<tr>"
+"<td><input name='quantity'/></td>"
+"<td><input type='submit' value='Buy'/>"
+"</td>"
+"</tr>");
writer.println("<tr><td colspan='2'>"
+"<a href-'products'>Product List</a>"
+"</td></tr>");
writer.println("</table>");
writer.println("</form></body>");
}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("<html><head><title>Products</title>"
+"</head><body><h2>Products</h2>");
writer.println("<ul>");
for(Product product :products){
writer.println("<li>"+product.getName()+"("
+currencyFormat.format(product.getPrice())
+") ("+"<a href='viewProductDetails?id="
+product.getId()+"'>Details</a>)");
}
writer.println("</ul>");
writer.println("<a href='viewCart'>View Cart</a>");
writer.println("</body></html>");
}
}