Androidで簡単なログインを実現


WEBにログインしてからセッションで返事をしていることをずっと知っていたのに、今はアンドロイドがどうしたのか分からない.
検討して、記録しておきます.直接コード:
サーバ側:
index.jsp
<%@page import="java.io.PrintWriter"%>
<%@page import="com.sun.org.apache.xml.internal.serialize.Printer"%>
<%@page import="com.google.gson.JsonObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//  ,         ,    
	String userName = request.getParameter("userName");

	PrintWriter pw = response.getWriter();
	//           
	if(userName.equals("miquan")) {
		//      json
		JsonObject json = new JsonObject();
		json.addProperty("success", true);
		json.addProperty("sessionId", request.getSession().getId());
		
		//        session
		request.getSession().setAttribute("userName", "I am a test name.");
		
		pw.write(json.toString());
		pw.flush();
	} else {
		JsonObject obj = new JsonObject();
		obj.addProperty("success", false);
		pw.write(obj.toString());
		pw.flush();
	}
%>

session.jsp
<%@page import="javax.websocket.Session"%>
<%@page import="java.io.PrintWriter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	PrintWriter pw = response.getWriter();
	//      session,         session,           
	pw.write((String)session.getAttribute("userName"));
	pw.flush();
%>

安卓端:
			//  
			btnLogin.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					FinalHttp fh = new FinalHttp();
					fh.get("http://172.27.35.1:8080/login/index.jsp?userName=miquan", new AjaxCallBack<Object>() {
						@Override
						public void onSuccess(Object t) {
							//      json
							String str = t.toString();
							str = str.trim();
							try {
								JSONObject obj = new JSONObject(str);
								boolean success = obj.getBoolean("success");
								//    
								if(success) {
									//app = (MyApplication) this.getApplication();
									//MyApplication     sessionId isLogin
									app.setLogin(true);
									app.setSessionId(obj.getString("sessionId"));
									Toast.makeText(app, "    ", Toast.LENGTH_SHORT).show();
								} else {
									Toast.makeText(app, "    ", Toast.LENGTH_SHORT).show();
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
							super.onSuccess(t);
						}
						@Override
						public void onFailure(Throwable t, int errorNo,
								String strMsg) {
							Log.e("miquan", "failure  " + strMsg);
							super.onFailure(t, errorNo, strMsg);
						}
					});
				}
			});
			//  
			btnSession.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					FinalHttp fh = new FinalHttp();
					//  session,       ,        java   
					fh.addHeader("Cookie", "JSESSIONID=" + app.getSessionId());
					fh.get("http://172.27.35.1:8080/login/session.jsp", new AjaxCallBack<Object>() {
						@Override
						public void onSuccess(Object t) {
							Log.e("miquan", t.toString());
							super.onSuccess(t);
						}
						@Override
						public void onFailure(Throwable t, int errorNo,
								String strMsg) {
							Log.e("miquan", "failure  " + strMsg);
							super.onFailure(t, errorNo, strMsg);
						}
					});
				}
			});

すぐに退勤して、あまり書かない...