易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
首页
/
操作系统
/
Linux
/
SSH+Android之Struts
使用json数据交互信息首先搭配SSH服务端,目前只用了Struts2,首先导入包,下面是需要的包
web.xml的代码
[html]
<?xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<web-app
version
=
"2.5"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<filter>
<filter-name>
struts2
</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>
struts2
</filter-name>
<url-pattern>
/*
</url-pattern>
</filter-mapping>
</web-app>
struts.xml代码
[html]
<?xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"
>
<struts>
<package
name
=
"as"
extends
=
"json-default"
>
<action
name
=
"login"
class
=
"as.action.LoginAction"
method
=
"login"
>
<result
type
=
"json"
></result>
</action>
</package>
</struts>
项目的结构
LoginAction.java代码
[java]
package
as.action;
import
java.util.HashMap;
import
java.util.Map;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts2.interceptor.ServletRequestAware;
import
org.apache.struts2.interceptor.ServletResponseAware;
import
net.sf.json.JSONObject;
import
as.model.User;
import
com.opensymphony.xwork2.ActionSupport;
import
com.opensymphony.xwork2.ModelDriven;
public
class
LoginAction
extends
ActionSupport
implements
ServletResponseAware ,ServletRequestAware,ModelDriven<User>{
private
User user;
private
HttpServletResponse response;
private
HttpServletRequest request;
public
User getUser() {
return
user;
}
public
void
setUser(User user) {
this
.user = user;
}
public
void
setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
this
.response = arg0;
}
public
void
setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this
.request=arg0;
}
public
void
login() {
try
{
// TODO Auto-generated method stub
this
.response.setContentType(
"text/html;charset=utf-8"
);
this
.response.setCharacterEncoding(
"UTF-8"
);
JSONObject json=
new
JSONObject();
Map map=
new
HashMap<Object, String>();
map.put(
"name"
, user.getName());
map.put(
"pwd"
, user.getPwd());
json.put(
"LoginInfo"
, map);
response.getWriter().write(json.toString());
}
catch
(Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public
User getModel() {
// TODO Auto-generated method stub
if
(user==
null
) {
return
user=
new
User();
}
return
user;
}
}
User.java
[java]
package
as.model;
public
class
User {
private
String name;
private
String pwd;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getPwd() {
return
pwd;
}
public
void
setPwd(String pwd) {
this
.pwd = pwd;
}
}
view层 index.jsp 代码
[html]
<
%@ page
language
=
"java"
import
=
"java.util.*"
pageEncoding
=
"UTF-8"
%
>
<
%
String
path
=
request
.getContextPath();
String
basePath
=
request
.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<html>
<head>
<base
href
=
"<%=basePath%>"
>
<title>
My JSP "index.jsp" starting page
</title>
<meta
http-equiv
=
"pragma"
content
=
"no-cache"
>
<meta
http-equiv
=
"cache-control"
content
=
"no-cache"
>
<meta
http-equiv
=
"expires"
content
=
"0"
>
<meta
http-equiv
=
"keywords"
content
=
"keyword1,keyword2,keyword3"
>
<meta
http-equiv
=
"description"
content
=
"This is my page"
>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<form
action
=
"login"
method
=
"post"
>
用户名:
<input
type
=
"text"
name
=
"name"
><br
>
密 码:
<input
type
=
"password"
name
=
"pwd"
><br
>
<input
type
=
"submit"
value
=
"登陆"
name
=
"submit"
>
<input
type
=
"reset"
value
=
"取消"
name
=
"reset"
>
</form>
</center>
</body>
</html>
测试 用户名:2222 密码:1111
现在在Android上面测试,Android 项目构架
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图