方法一:网址重写

通过在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>");
}
}

  

java web用于保持状态的4种方法的更多相关文章

  1. JAVA 集合 List 分组的两种方法

    CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...

  2. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  3. 性能调优之提高 ASP.NET Web 应用性能的 24 种方法和技巧

    性能调优之提高 ASP.NET Web 应用性能的 24 种方法和技巧   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对 ...

  4. Java中创建数组的几种方法

    Java中创建数组的几种方法 public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intVa ...

  5. Java List转换为字符串的几种方法

    Java List转换为字符串的几种方法 import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import ...

  6. java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)

    转载地址:http://www.devba.com/index.php/archives/4581.html java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明); ...

  7. Java遍历List集合的三种方法

    Java遍历List集合的三种方法 List<String> list = new ArrayList<String>(); list.add("aaa") ...

  8. (转)java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)

    java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明);部分资料参考网络资源 1. java向MySQL插入当前时间的四种方式 第一种:将java.util.Date ...

  9. Java 读取clob字段的几种方法

    Java 读取clob字段的几种方法 一.第一种 Clob clob = rs.getClob("remark");//Java.sql.Clob String detailinf ...

随机推荐

  1. 【转】最近搞Hadoop集群迁移踩的坑杂记

    http://ju.outofmemory.cn/entry/237491 Overview 最近一段时间都在搞集群迁移.最早公司的hadoop数据集群实在阿里云上的,机器不多,大概4台的样子,据说每 ...

  2. 廖雪峰js教程笔记3

    JavaScript的函数在查找变量时从自身函数定义开始,从"内"向"外"查找.如果内部函数定义了与外部函数重名的变量,则内部函数的变量将"屏蔽&qu ...

  3. SpringMVC解析2-ContextLoaderListener

    对于SpringMVC功能实现的分析,我们首先从web.xml开始,在web.xml文件中我们首先配置的就是ContextLoaderListener,那么它所提供了功能有哪些又是如何实现的?当使用编 ...

  4. [bzoj2118]墨墨的等式【dijk+堆】

    10/30的update:如果是冲着dijk的板子来的,建议看多校联考contest中第二场day2的T2,那边的写法比较优秀... --------------------------------- ...

  5. ASP.NET MVC中使用highcharts 生成简单的折线图

        直接上步骤:   生成一个options,选项包含了一些基本的配置,如标题,坐标刻度,serial等: 配置X轴显示的Category数据,为一个数组: 配置Y轴显示的数据,也为一个数据: 用 ...

  6. enum枚举类型的使用

    修饰符为public static enum,不用加final,否则提示错误. 枚举类的所有实例必须在枚举类中显式列出(,分隔,; 结尾).列出的实例系统会自动添加 public static fin ...

  7. Codeforces Round #244 (Div. 2) B. Prison Transfer

    题目是选出c个连续的囚犯,而且囚犯的级别不能大于t #include <iostream> using namespace std; int main(){ int n,t,c; cin ...

  8. ACM 田忌赛马

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  9. BZOJ3653: 谈笑风生

    Description 设T 为一棵有根树,我们做如下的定义:• 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道高明到哪里去了”.• 设a 和 b 为 T 中的两个不同节点.如 ...

  10. 教你如何利用分布式的思想处理集群的参数配置信息——spring的configurer妙用

    引言 最近LZ的技术博文数量直线下降,实在是非常抱歉,之前LZ曾信誓旦旦的说一定要把<深入理解计算机系统>写完,现在看来,LZ似乎是在打自己脸了.尽管LZ内心一直没放弃,但从现状来看,需要 ...