服务器端servlet怎么将json的值返回客户端,请详细讲解下

qq_26579407 2016-07-01 12:55:25

客户端代码:
public class SkimFile {
public String Skim(){
String rvalue="";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8080/httpclientweb/skim.action");
List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
formparams=null;
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");//urlEncoding
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI()); //获取httppst访问地址
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity(); //获得响应实体
if (entity != null) {
rvalue=EntityUtils.toString(entity, "UTF-8");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return rvalue;
}
}

客户端:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Userdao userdao=new Userdao();
List list=userdao.queryUser();
String temp=new String();
for(int i=0;i<list.size();i++){
User user=(User)list.get(i);
System.out.println(user.getId());
temp +=user.getId()+" ";
temp +=user.getPassword()+" ";
temp +=user.getName()+"|";
}

temp=temp.substring(0,temp.length()-1);
System.out.println("Start"+temp);
out.flush();
out.close();
}



...全文
213 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_26579407 2016-07-02
  • 打赏
  • 举报
回复
out 怎么写才能被客户端引用
阳光越来越暖 2016-07-01
  • 打赏
  • 举报
回复
temp=temp.substring(0,temp.length()-1); System.out.println("Start"+temp); out.flush(); out.close(); out.write() 怎么没写? 不写的话怎么flush 这个只是输出在控制台吧 System.out.println("Start"+temp);

50,545

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧