• tomcat服务器用Servlet类查找磁盘文件上的Json信息,如果匹配则在浏览器上显示出该条内容的全部信息


    package com.swift;
    
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    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 FindServlet
     */
    @WebServlet("/find")
    public class FindServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FindServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("d:/student.json"),"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();
            String name=request.getParameter("name");
            for(Student stu:list) {
                if(stu.getName().equals(name)) {
                    Gson gson=new Gson();
                    String json=gson.toJson(stu);
                    response.getWriter().println(json);
                }
            }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

  • 相关阅读:
    Integer和Integer常量池
    Spring中常用的23中设计模式
    GitHub 简单教程
    IDEA 中用好 Lombok,撸码效率至少提升5倍!
    Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案
    ROS常用命令和VIM常用命令
    ROS运行
    VINS-Mono运行
    环境配置相关
    C89标准和C99标准C11标准的区别
  • 原文地址:https://www.cnblogs.com/qingyundian/p/7515127.html
Copyright © 2020-2023  润新知