package com.swift;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; /**
* Servlet implementation class DeleteServlet
*/
@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public DeleteServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
File file=new File("d:/student.json");
//获取全部学生对象
List<Student> list=getAllStudent(file);
System.out.println(list.toString());
//获取要删除学生姓名
String name=request.getParameter("name");
System.out.println(name);
//从list对象列表中删除匹配姓名的学生对象
deleteStudentByName(response, list, name);
System.out.println(list.toString());
//将删除后的对象列表写入磁盘文件
StringBuffer sb=new StringBuffer();//将剩下的json信息保存到sb中,不然文件中只能得到最后一个json数据
Gson gson=new Gson();
for(Student stu:list) {
String json=gson.toJson(stu);
sb.append(json);
sb.append("\r\n");
}
writeToFile(file, sb.toString());
} private void deleteStudentByName(HttpServletResponse response, List<Student> list, String name) throws IOException {
for(int i=0;i<list.size();i++) {
if(list.get(i).getName().equals(name)) {
list.remove(i);
response.getWriter().println("You have already delete the name "+name);
}
}
} private void writeToFile(File file,String json) {
PrintWriter pw = null;
try {
try {
pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pw.println(json);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
pw.close();
}
} private List<Student> getAllStudent(File file) throws UnsupportedEncodingException, FileNotFoundException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
List<Student> list=new ArrayList<Student>();
String str;
while((str=br.readLine())!=null) {
Gson gson=new Gson();
Student stu=gson.fromJson(str, Student.class);
list.add(stu);
}
br.close();
return list;
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

使用Servlet根据浏览器request的get方法获取值,将磁盘中与之对应的json数据删除的方法的更多相关文章

  1. AJAX跨域请求json数据的实现方法

    这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...

  2. 使用Python解析JSON数据的基本方法

    这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下:     ----------------------------------- ...

  3. JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  4. 【jmeter】JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  5. JMeter中返回Json数据的处理方法(转)

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  6. JMeter 中对于Json数据的处理方法

    JMeter中对于Json数据的处理方法 http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/ J ...

  7. 转-oracle中比较两表表结构差异和数据差异的方法

    oracle中比较两表表结构差异和数据差异的方法 原作者:li2008xue2008ling  出处:http://blog.csdn.net       在工作中需要完成这么一个需求:比较两个表的表 ...

  8. 【转】JMeter中对于Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  9. jmeter(二十)JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

随机推荐

  1. jmeter 正则获取参数集合和ForEach控制器结合使用(转)

    怎么把第一个请求获取的返回的多个id,在第二个请求中逐个以单个id作为请求参数来请求? 为了解决这个问题,模拟下该场景 1.请求www.163.com 主页,获取响应中的所有数字,这个获取的数字集合暂 ...

  2. Visitor模式(访问者设计模式)

    Visitor ? 在Visitor模式中,数据结构与处理被分离开来.我们编写一个表示"访问者"的类来访问数据结构中的元素, 并把对各元素的处理交给访问者类.这样,当需要增加新的处 ...

  3. ACM-ICPC 2018 南京赛区网络预赛 E. AC Challenge (状态压缩DP)

    Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answe ...

  4. Python——连接数据库

    好用的教程(*^▽^*):https://www.cnblogs.com/fatcat132006/p/4081576.html

  5. python import error:no module named yaml

    解决办法: python2 : sudo apt-get install python-yaml python3 : sudo apt-get install python3-yaml

  6. I - Defeat the Enemy UVALive - 7146 二分 + 贪心

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. Unity UGUI按钮添加点击事件

    1. 可视化创建及事件绑定 # 1 : 通过 Hierarchy 面板创建 UI > Button. 2 : 创建一个脚本 TestClick.cs, 定义了一个 Click 的 public ...

  8. (转)linux自定义开机启动服务和chkconfig使用方法

    原文:https://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html linux自定义开机启动服务和chkconfig使用方法 1. 服 ...

  9. Practice encryptedblobstore

    C++ BlobStore 使用范例(C++伪代码) 一个可能的接口设计示例(C++) Java BlobStore 使用范例(Java伪代码) 一个可能的接口设计示例(Java) 一个基于Key/V ...

  10. java打印堆栈信息

    StackTraceElement[] stackElements = new Throwable().getStackTrace(); if(stackElements != null){ for( ...