使用Servlet根据浏览器request的get方法获取值,将磁盘中与之对应的json数据删除的方法
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数据删除的方法的更多相关文章
- AJAX跨域请求json数据的实现方法
这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...
- 使用Python解析JSON数据的基本方法
这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下: ----------------------------------- ...
- JMeter中返回Json数据的处理方法
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
- 【jmeter】JMeter中返回Json数据的处理方法
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
- JMeter中返回Json数据的处理方法(转)
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
- JMeter 中对于Json数据的处理方法
JMeter中对于Json数据的处理方法 http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/ J ...
- 转-oracle中比较两表表结构差异和数据差异的方法
oracle中比较两表表结构差异和数据差异的方法 原作者:li2008xue2008ling 出处:http://blog.csdn.net 在工作中需要完成这么一个需求:比较两个表的表 ...
- 【转】JMeter中对于Json数据的处理方法
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
- jmeter(二十)JMeter中返回Json数据的处理方法
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
随机推荐
- 记录一下今天犯得错误,public static function init()写成了public function initialize()
tp5模型事件是放在函数 public static function init() 而我写成了初始化函数,编辑器生成的 public function initialize() 开始时用着没出问题, ...
- Migration-添加表
public partial class _111111 : DbMigration { public override void Up() { CreateTable( "dbo.Asse ...
- Luogu P2455 [SDOI2006]线性方程组 真•高斯消元板子
果然如Miracle学长所说...调了一天...qwq..还是过不了线下的Hack upd after 40min:刚刚过了 就是多了一个判无解的操作... 当系数都为0,且常数项不为0时,即为无解. ...
- ubuntu操作技巧
1打开终端:ctrl+alt 2退出crtl+d
- JVM基础知识1--JAVA内存区域与内存溢出异常
1,运行时数据区域 根据JAVA虚拟机规范的规定:JAVA虚拟机所管理的内存将会包括以下几个运行时数据区域 程序计数器(Program Counter Register)是一块较小的内存空间,它的作用 ...
- android 开发-系统设置界面的实现
具体与Preference的用法类似,这里就不做过多解释,直接贴示例代码,需要在res下新建xml文件夹,在xml文件夹下添加xml文件. xml:(注意:root节点是:PreferenceScre ...
- xenserver 更新源
在xenserver上安装vnc软件时,报错 [root@cloud yum-3.4.3]# ./yummain.py install yumThere are no enabled repos.Ru ...
- orcale函数
字符函数 1.ASCII 返回与指定的字符对应的十进制数; select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space ...
- Docker for mac 安装 kong
首先安装一个 PostgreSQL,选的版本是 9.5 $ docker run -d --name kong-database \ -p : \ -e "POSTGRES_USER=kon ...
- 构建第一个Spring Boot2.0应用之项目创建(一)
1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...