歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> SSH+Android之Struts

SSH+Android之Struts

日期:2017/3/1 10:37:48   编辑:Linux編程

使用json數據交互信息

首先搭配SSH服務端,目前只用了Struts2,首先導入包,下面是需要的包



web.xml的代碼

[html]

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <filter>
  11. <filter-name>struts2</filter-name>
  12. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>struts2</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. </web-app>
struts.xml代碼

[html]

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
  3. <struts>
  4. <package name="as" extends="json-default">
  5. <action name="login" class="as.action.LoginAction" method="login">
  6. <result type="json" ></result>
  7. </action>
  8. </package>
  9. </struts>
項目的結構

LoginAction.java代碼

[java]

  1. package as.action;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts2.interceptor.ServletRequestAware;
  7. import org.apache.struts2.interceptor.ServletResponseAware;
  8. import net.sf.json.JSONObject;
  9. import as.model.User;
  10. import com.opensymphony.xwork2.ActionSupport;
  11. import com.opensymphony.xwork2.ModelDriven;
  12. public class LoginAction extends ActionSupport implements ServletResponseAware ,ServletRequestAware,ModelDriven<User>{
  13. private User user;
  14. private HttpServletResponse response;
  15. private HttpServletRequest request;
  16. public User getUser() {
  17. return user;
  18. }
  19. public void setUser(User user) {
  20. this.user = user;
  21. }
  22. public void setServletResponse(HttpServletResponse arg0) {
  23. // TODO Auto-generated method stub
  24. this.response = arg0;
  25. }
  26. public void setServletRequest(HttpServletRequest arg0) {
  27. // TODO Auto-generated method stub
  28. this.request=arg0;
  29. }
  30. public void login() {
  31. try {
  32. // TODO Auto-generated method stub
  33. this.response.setContentType("text/html;charset=utf-8");
  34. this.response.setCharacterEncoding("UTF-8");
  35. JSONObject json=new JSONObject();
  36. Map map=new HashMap<Object, String>();
  37. map.put("name", user.getName());
  38. map.put("pwd", user.getPwd());
  39. json.put("LoginInfo", map);
  40. response.getWriter().write(json.toString());
  41. } catch (Exception e) {
  42. // TODO: handle exception
  43. e.printStackTrace();
  44. }
  45. }
  46. public User getModel() {
  47. // TODO Auto-generated method stub
  48. if (user==null) {
  49. return user=new User();
  50. }
  51. return user;
  52. }
  53. }
User.java

[java]

  1. package as.model;
  2. public class User {
  3. private String name;
  4. private String pwd;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public String getPwd() {
  12. return pwd;
  13. }
  14. public void setPwd(String pwd) {
  15. this.pwd = pwd;
  16. }
  17. }
view層 index.jsp 代碼

[html]

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <center>
  22. <form action="login" method="post">
  23. 用戶名: <input type="text" name="name"><br>
  24. 密 碼: <input type="password" name="pwd"><br>
  25. <input type="submit" value="登陸" name="submit"> <input type="reset" value="取消" name="reset">
  26. </form>
  27. </center>
  28. </body>
  29. </html>
  30. 測試 用戶名:2222 密碼:1111

現在在Android上面測試,Android 項目構架

Copyright © Linux教程網 All Rights Reserved