原理:(先说明一下环境,服务器端我用的apache-tomcat-6.0.35,客户端我是用是android里面自带的HttpClient;因为不同的环境这个方法未必有效,这个可以看我转载的文章)
我们在第一次访问服务器端的时候,使用request.getSession(),服务器在后台就会创建一个session,系统会自动给session一个id,叫JSESSIONID;然后返回给客户端,下次客户端拿着这个JSESSIONID访问服务器;如果,服务器端request.getSession(),到服务器的内存区域找到了对应的id,那就可以实现长久登陆有效,而且可以访问session里面的数据。
原理实际对应的访问过程:
客户端第一次访问服务器:
服务器第一次返回的数据:(我用的浏览器是IE9的,不同的浏览器,结果会有所不同;还有不要禁用cookie,否则这个结果肯定的不出来)
客户端第二次访问服务器:(注意,客户端的JESESSIONID,与服务器端给的JSESSIONID是一致的;还有客户端第二次比第一次就多一个Cookie头)
服务器第二次返回的结果:
操作思想:
因为客户端第二次访问,比第一次访问就多一个Cookie头;我们将服务器第一次的返回结果里面的JSESSIONID提取出来,并保存;然后以后的访问我们加上JSESSIONID,就可以实现登陆长久有效了;
操作思想对应的代码:
客户端第一次访问的代码:
private void sendDataToServer() {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.1.106:8080/ag/servlet/CoreServlet");
HttpParams params = new BasicHttpParams();//
params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 8000); //连接超时
HttpConnectionParams.setSoTimeout(params, 5000); //响应超时
post.setParams(params);
if (null != sp.getString("JSESSIONID", null)) {
JSESSIONID = sp.getString("JSESSIONID", null);
post.setHeader("Cookie", "JSESSIONID=" + JSESSIONID);
}
ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
BasicNameValuePair pair1 = new BasicNameValuePair("transactiontype", "30002");
BasicNameValuePair pair2 = new BasicNameValuePair("verifcode", "123456");
pairList.add(pair1);
pairList.add(pair2);
try {
HttpEntity entity = new UrlEncodedFormEntity(pairList,"UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
cookies = client.getCookieStore().getCookies();
Cookie cookie = null;
if (cookies.isEmpty()) {
Log.i(TAG, "-------Cookie NONE---------");
} else {
for (int i = 0; i < cookies.size(); i++ ) {
//保存cookie
cookie = cookies.get(i);
Log.i(TAG, cookies.get(i).getName() + "=" + cookies.get(i).getValue());
if ("JSESSIONID".equals(cookies.get(i).getName())) {
JSESSIONID = cookies.get(i).getValue();
Editor editor = sp.edit();
editor.putString("JSESSIONID", JSESSIONID);
editor.commit();
Log.i(TAG, "JSESSIONID=" + cookies.get(i).getValue());
}
}
}
InputStream result = response.getEntity().getContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
客户端第二次访问的代码:
new Thread(new Runnable() {
@Override
public void run() {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.1.106:8080/ag/servlet/CoreServlet");
HttpParams params = new BasicHttpParams();//
params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 8000); //连接超时
HttpConnectionParams.setSoTimeout(params, 5000); //响应超时
post.setParams(params);
post.setHeader("Cookie", "JSESSIONID=" + JSESSIONID);//第二次访问拿着JSESSIONID就可以了
Log.i(TAG, "JSESSIONID=" + JSESSIONID + "::2");
// client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
BasicNameValuePair pair1 = new BasicNameValuePair("transactiontype", "30002");
pairList.add(pair1);
HttpEntity entity = null;
try {
entity = new UrlEncodedFormEntity(pairList,"UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
Log.i(TAG , "second");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
服务器端的核心代码:
HttpSession session = request.getSession();
if (session.isNew()) {
session.setMaxInactiveInterval(-1);
Agenter agenter = new Agenter();
agenter.setAgenterid(request.getParameter("agenterid"));
//TODO 这里要查询数据库,填充Agenter
agenter.setMoney("321456");
session.setAttribute("agenter", agenter);
// session.invalidate();
Cookie cookie = new Cookie("JSESSIONID",session.getId());
cookie.setMaxAge(Integer.MAX_VALUE);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);//自己从重写cookie,否则cookie的生命在浏览器关闭之后就结束了
System.out.println("session.isNew()");
}else{
Agenter agenter = (Agenter) session.getAttribute("agenter");
System.out.println("agenterid:" + agenter.getAgenterid());
System.out.println("money:" + agenter.getMoney());
System.out.println("session.isOld()");
}
代码就不上传了;按照注意事项,和参照核心代码是绝对可以实现的!!!