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

2017-09-14 08:06:52来源:cnblogs.com作者:Advancing-Swift人点击

分享
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);    }}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台