上次已经完成了Android客户端向服务器端传值的功能,接下来的任务是服务器端通过连接数据库对传来的值进行判定,并将判定结果返回客户端。
对传来的数值进行判定的处理过程与web开发相同,比较简单。重点是将判定结果返回客户端的实现:
Servlet部分重点代码:
- /**
- * 功能:验证手机用户登录
- */
- public void androidCheckUser(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- PrintWriter out = response.getWriter();
- String userName=request.getParameter("userName");
- String passWord=request.getParameter("passWord");
- //在服务器端解决中文乱码问题
- userName=NewString.getNewString(userName);
- passWord=NewString.getNewString(passWord);
- System.out.println("账号:"+userName);
- System.out.println("密码:"+passWord);
- //调用dao
- UserDao uDao=new UserDao();
- if(uDao.checkUser(userName, passWord))
- { //得到登录用户的权限类型
- String type=uDao.getType(userName);
- if(type.equals("高级")){
- request.setAttribute("result","high");
- request.getRequestDispatcher("login.jsp").forward(request, response);
- }else if(type.equals("普通")){
- request.setAttribute("result","common");
- request.getRequestDispatcher("login.jsp").forward(request, response);
- }else if(type.equals("超级")){
- request.setAttribute("result","super");
- request.getRequestDispatcher("login.jsp").forward(request, response);
- }
- }else
- {
- request.setAttribute("result","false");
- request.getRequestDispatcher("login.jsp").forward(request, response);
- }
/**
* 功能:验证手机用户登录
*/
public void androidCheckUser(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String userName=request.getParameter("userName");
String passWord=request.getParameter("passWord");
//在服务器端解决中文乱码问题
userName=NewString.getNewString(userName);
passWord=NewString.getNewString(passWord);
System.out.println("账号:"+userName);
System.out.println("密码:"+passWord);
//调用dao
UserDao uDao=new UserDao();
if(uDao.checkUser(userName, passWord))
{ //得到登录用户的权限类型
String type=uDao.getType(userName);
if(type.equals("高级")){
request.setAttribute("result","high");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else if(type.equals("普通")){
request.setAttribute("result","common");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else if(type.equals("超级")){
request.setAttribute("result","super");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}else
{
request.setAttribute("result","false");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
如上所示,Servlet调用Dao进行判定,将判定标识写入XML格式的JSP页面中。
JSP页面结构:
- <%@ page language="java" import="java.util.*" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><?xml version="1.0" encoding="UTF-8"?>
- <%String result=(String)request.getAttribute("result");%>
- <result>
- <flag><%=result%></flag>
- </result>
<%@ page language="java" import="java.util.*" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><?xml version="1.0" encoding="UTF-8"?>
<%String result=(String)request.getAttribute("result");%>
<result>
<flag><%=result%></flag>
</result>
Android客户端解析XML格式的JSP页面,得到服务器端传来的判定结果,根据结果弹出提示框。
- /**
- * 验证用户登录是否合法
- * 返回值:服务器端判断的标识
- */
- public static String check(String name, String pass) throws XmlPullParserException, MalformedURLException, IOException {
- String path="http://135.32.89.17:8080/lss/UserServlet?flag=androidCheck";
- //将用户名和密码放入HashMap中
- Map<String,String> params=new HashMap<String,String>();
- params.put("userName", name);
- params.put("passWord", pass);
- String checkr=uMethod.checkUser(path,params,"UTF-8");
- System.out.println("check"+checkr);
- return checkr;
- }
/**
* 验证用户登录是否合法
* 返回值:服务器端判断的标识
*/
public static String check(String name, String pass) throws XmlPullParserException, MalformedURLException, IOException {
String path="http://135.32.89.17:8080/lss/UserServlet?flag=androidCheck";
//将用户名和密码放入HashMap中
Map<String,String> params=new HashMap<String,String>();
params.put("userName", name);
params.put("passWord", pass);
String checkr=uMethod.checkUser(path,params,"UTF-8");
System.out.println("check"+checkr);
return checkr;
}
- public String checkUser(String path,
- Map<String, String> params,String encode) throws MalformedURLException, IOException, XmlPullParserException {
- StringBuilder url=new StringBuilder(path);
- url.append("&");
- for(Map.Entry<String, String> entry:params.entrySet())
- {
- url.append(entry.getKey()).append("=");
- url.append(URLEncoder.encode(entry.getValue(),encode));
- url.append("&");
- }
- //删掉最后一个&
- url.deleteCharAt(url.length()-1);
- HttpURLConnection conn=(HttpURLConnection)new URL(url.toString()).openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- if(conn.getResponseCode()==200)
- {
- InputStream inputStream=conn.getInputStream();
- System.out.println("准备解析!");
- String s=parseXML(inputStream);
- System.out.println("准备解析!"+s);
- //将得到的XML内容的解析
- if(s.equals("high")){ //权限为高级用户
- return "high";
- }else if(s.equals("common")){ //权限为普通用户
- return "common";
- }else if(s.equals("super")){ //权限为超级用户
- return "super";
- }else if(s.equals("false")){ //权限为超级用户
- return "false";
- }
- }
- return "false";
- }
- /**
- * 解析服务器端返回的XML
- */
- public String parseXML(InputStream inputStream) throws XmlPullParserException, IOException {
- XmlPullParser parser=Xml.newPullParser();
- parser.setInput(inputStream,"UTF-8");
- String result=null;
- //得到开始文档事件
- int event=parser.getEventType();
- while(event!=XmlPullParser.END_DOCUMENT)
- {
- switch(event)
- {
- case XmlPullParser.START_TAG:
- if("flag".equals(parser.getName()))
- {
- result=parser.nextText();
- System.out.println("flag"+result);
- }
- break;
- //一定要加,不然报错Caused by: org.xmlpull.v1.XmlPullParserException: Premature end of document.
- case XmlPullParser.END_TAG:
- break;
- }
- event = parser.next();
- }
- return result;
- }
public String checkUser(String path,
Map<String, String> params,String encode) throws MalformedURLException, IOException, XmlPullParserException {
StringBuilder url=new StringBuilder(path);
url.append("&");
for(Map.Entry<String, String> entry:params.entrySet())
{
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(),encode));
url.append("&");
}
//删掉最后一个&
url.deleteCharAt(url.length()-1);
HttpURLConnection conn=(HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200)
{
InputStream inputStream=conn.getInputStream();
System.out.println("准备解析!");
String s=parseXML(inputStream);
System.out.println("准备解析!"+s);
//将得到的XML内容的解析
if(s.equals("high")){ //权限为高级用户
return "high";
}else if(s.equals("common")){ //权限为普通用户
return "common";
}else if(s.equals("super")){ //权限为超级用户
return "super";
}else if(s.equals("false")){ //权限为超级用户
return "false";
}
}
return "false";
}
/**
* 解析服务器端返回的XML
*/
public String parseXML(InputStream inputStream) throws XmlPullParserException, IOException {
XmlPullParser parser=Xml.newPullParser();
parser.setInput(inputStream,"UTF-8");
String result=null;
//得到开始文档事件
int event=parser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT)
{
switch(event)
{
case XmlPullParser.START_TAG:
if("flag".equals(parser.getName()))
{
result=parser.nextText();
System.out.println("flag"+result);
}
break;
//一定要加,不然报错Caused by: org.xmlpull.v1.XmlPullParserException: Premature end of document.
case XmlPullParser.END_TAG:
break;
}
event = parser.next();
}
return result;
}
运行效果:
总结:
服务器端将所传值写入XML格式的JSP页面中,Android对XML进行解析,得到标识值