package com.parabola.action;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.parabola.entity.User;
import com.parabola.service.impl.IUserServcie;
/**
* @作者 song shi chao
* @QQ 421271944
* @创建日期 2011-8-22
* @版本 V 1.0
*/
public class UserAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
public IUserServcie userService;
public User user = new User();
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public IUserServcie getUserService() {
return userService;
}
public void setUserService(IUserServcie userService) {
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void save(){
System.out.println("---save()");
String method = request.getMethod();
String format = request.getParameter("format");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String address = request.getParameter("address");
user.setName(name);
user.setSex(sex);
user.setAge(age);
user.setAddress(address);
userService.add(user);
System.out.println("username1 ="+ name+sex+age+address);
JSONArray jsonArray = new JSONArray();
if(method.equals("GET")){
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", "1");
jsonObject.put("title", "哈利波特");
jsonObject.put("timelength", 89);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", 2);
jsonObject1.put("title", "速度与激情");
jsonObject1.put("timelength", 120);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("id", 3);
jsonObject2.put("title", "变形金刚3");
jsonObject2.put("timelength", 100);
jsonArray.add(0, jsonObject);
jsonArray.add(1, jsonObject1);
jsonArray.add(2, jsonObject2);
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().write(jsonArray.toString());
} catch (IOException e) {
e.printStackTrace();
}
}else{
// 如果请求方式是"POST",那么就以XML文件的形式返回到客户端
toXML();
}
}
public String register(){
return "register";
}
/**
* 这里拼接一个xml文件,数据是直接写进去的,
* 真实的项目应该是从数据库中读取出来的,这看以参考开发文档中的第三个网址
*/
public void toXML(){
StringBuilder sb = new StringBuilder();
String s = new String();
String[] strTemp = new String[3];
try {
FileWriter fw = new FileWriter("songshichao.xml");
BufferedWriter outXml = new BufferedWriter(fw);
outXml.write("<?xml version= \"1.0\" encoding=\"UTF-8\"?>");
outXml.newLine();
outXml.write("<people>");
strTemp[0] = "song";
strTemp[1] = "shi";
strTemp[2] = "chao";
outXml.newLine();
outXml.write("<student>");
outXml.newLine();
outXml.write("<name>" + strTemp[0] + "</name>");
outXml.newLine();
outXml.write("<sex>" + strTemp[1] + "</sex>");
outXml.newLine();
outXml.write("<age>" + strTemp[2] + "</age>");
outXml.newLine();
outXml.write("</student>");
outXml.newLine();
outXml.write("</people>");
outXml.flush();
// 这里采用的字节读取,如果有中文数据,会出错的。所以应该改成字符读取,或者用缓冲流或者数据流读取
// try {
// byte[] b = new byte[30];
// File f = new File("songshichao.xml");
// FileInputStream ins = new FileInputStream(f);
// int n = 0;
// while((n = ins.read(b,0,2)) != -1){
// s = new String(b,0,n);
// sb.append(s);
// }
// 这里采用字符流读取数据
// try{
// char[] b = new char[30];
// File f = new File("songshichao.xml");
// FileReader fr = new FileReader(f);
// int n = 0;
// while((n = fr.read(b,0,2)) != -1){
// s = new String(b,0,n);
// sb.append(s);
// }
//
// response.setCharacterEncoding("UTF-8");
// System.out.println(sb);
// response.getWriter().write(sb.toString());
// } catch (Exception e) {
// e.printStackTrace();
// }
}catch (Exception e1) {
e1.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Java看的HTTP网络编程 网络通信编程实例 适用于Android网络编程 在测试代码中服务端我使用三种方式:S2SH 只用Struts2 Servlet 客户端就一种 只不过换个请求的URL 服务端和客户端交互使用了两种方式:XML JSON 目前开放平台返回的数据都是支持这两种数据格式的 所以我也实现了这两种数据格式 其实还有一种格式 就是google的protocal buffer 这种形式在国内用的人比较少 我也没弄 感兴趣的同学看我提供的文档 自己研究吧 注意把里面的IP换成自己电脑的IP地址 在使用Servlet的服务端 可以通过下面这种方式设置返回类型 但是其实没用 你设置了 它也不返回 你只能自己封装XML文件或者JSON字符串 然后返回 response setContentType "application json;charset utf 8" ;">Java看的HTTP网络编程 网络通信编程实例 适用于Android网络编程 在测试代码中服务端我使用三种方式:S2SH 只用Struts2 Servlet 客户端就一种 只不过换个请求的URL 服务端和客户端交互使用了两种方式:XML JSON 目前开放 [更多]
资源推荐
资源详情
资源评论
收起资源包目录
ServletTest.rar (99个子文件)
codefans.net
ServletTest1
.project 1KB
.mymetadata 309B
src
data.java 2KB
WebRoot
WEB-INF
classes
data.class 2KB
lib
commons-logging.jar 59KB
web.xml 757B
index.jsp 831B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
.classpath 1KB
ReadMe 1KB
WebAndroid
.project 1KB
.mymetadata 303B
src
applicationContext-actions.xml 843B
struts.xml 1000B
hibernate.cfg.xml 1KB
com
parabola
service
impl
UserServiceImpl.java 934B
IUserServcie.java 515B
action
UserAction.java 5KB
dao
impl
UserDaoImpl.java 2KB
IUserDao.java 531B
entity
User.java 829B
User.hbm.xml 982B
applicationContext-beans.xml 1KB
applicationContext-commons.xml 2KB
WebRoot
WEB-INF
classes
applicationContext-actions.xml 843B
struts.xml 1000B
hibernate.cfg.xml 1KB
com
parabola
service
impl
IUserServcie.class 480B
UserServiceImpl.class 2KB
action
UserAction.class 5KB
dao
impl
IUserDao.class 486B
UserDaoImpl.class 1KB
entity
User.class 1KB
User.hbm.xml 982B
applicationContext-beans.xml 1KB
applicationContext-commons.xml 2KB
lib
page
register.jsp 1KB
index.jsp 816B
web.xml 2KB
index.jsp 829B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
开发文档
开发文档 2KB
.classpath 11KB
WebAndroidClient
.project 852B
proguard.cfg 1KB
src
com
parabola
main
User.java 635B
LoginActivity.java 2KB
WelcomeActivity.java 330B
LoginProtocol.java 4KB
Cache.java 531B
AndroidManifest.xml 841B
readme 156B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
welcome.xml 417B
main.xml 1KB
values
strings.xml 179B
assets
gen
com
parabola
main
R.java 1KB
default.properties 362B
.classpath 280B
bin
resources.ap_ 11KB
com
parabola
main
R$layout.class 422B
User.class 1KB
R$drawable.class 397B
R$attr.class 337B
LoginActivity.class 3KB
LoginActivity$1.class 803B
R$id.class 523B
Cache.class 846B
LoginProtocol.class 4KB
R.class 524B
R$string.class 424B
Cache$CacheHolder.class 636B
WelcomeActivity.class 524B
classes.dex 9KB
WebAndroidClient.apk 17KB
StrAnd
.project 1KB
.mymetadata 291B
src
struts.xml 1KB
com
dongzi
action
loginAction.java 2KB
WebRoot
WEB-INF
classes
struts.xml 1KB
com
dongzi
action
loginAction.class 2KB
lib
web.xml 863B
index.jsp 829B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
.classpath 2KB
共 99 条
- 1
wl3780
- 粉丝: 1
- 资源: 40
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页