• 每日学习(个人作业2)


    对于个人作业2的show页面的后台处理

    servlet

    package servlet;
    
    import java.io.IOException;
    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;
    
    import bean.Bean;
    import dao.Dao;
    
    /**
     * Servlet implementation class Servlet
     */
    @WebServlet("/Servlet")
    public class Servlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public Servlet() {
            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
    		request.setCharacterEncoding("utf-8");
            String word=request.getParameter("word");
            Dao dao=new Dao();
            List<Bean> list=dao.findtitle(word);
            Gson gson = new Gson();        
            response.setContentType("text/html;charset=utf-8");
            String json = gson.toJson(list);
            System.out.println(json);
            response.getWriter().write(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);
    	}
    
    }
    

     getmax:

    package servlet;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Map;
    
    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.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import java.util.*;
    
    import dao.Dao;
    
    /**
     * Servlet implementation class getmax
     */
    @WebServlet("/getmax")
    public class getmax extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public getmax() {
            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
    		request.setCharacterEncoding("utf-8");
    		response.setContentType("text/html;charset=utf-8");	
    		Dao dao=new Dao();
            Map<String, Integer>sortMap=dao.getallmax();
            JSONArray json =new JSONArray();
            int k=0;
            for (Map.Entry<String, Integer> entry : sortMap.entrySet()) 
            {
                JSONObject ob=new JSONObject();
                ob.put("name", entry.getKey());
                ob.put("value", entry.getValue());
                if(!(entry.getKey().equals("for")||entry.getKey().equals("and")||entry.getKey().equals("With")||entry.getKey().equals("of")||entry.getKey().equals("in")||entry.getKey().equals("From")||entry.getKey().equals("A")||entry.getKey().equals("to")||entry.getKey().equals("a")||entry.getKey().equals("the")||entry.getKey().equals("by")))
                {
                    json.add(ob);
                    k++;
                }
                if(k==10)
                    break;
            }
            System.out.println(json.toString());
            response.getWriter().write(json.toString());
    	}
    
    	/**
    	 * @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);
    	}
    
    }
    

     数据库的处理:

    public List<Bean> findtitle(String name) {
            String sql="select * from cvpr where title like '%"+name+"%'";
            Connection con=null;
            Statement state=null;
            ResultSet rs=null;
            boolean flag=false;
            try {
    			con=Util.getConnection();
    		} catch (SQLException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            Bean bean=null;
            List<Bean> list=new ArrayList<Bean>();
            try {
                state=con.createStatement();
                rs=state.executeQuery(sql);
                while(rs.next())
                {
                    bean=new Bean();
                    bean.setTitle(rs.getString("title"));
                    bean.setLink(rs.getString("link"));
                    list.add(bean);
                }
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            Util.close(con,state,rs);
            return list;    
        }
    public Map<String,Integer> getallmax()
        {
            String sql="select * from cvpr";
            Map<String, Integer>map=new HashMap<String, Integer>();
            Map<String, Integer>sorted=new HashMap<String, Integer>();
            Connection con=null;
            Statement state=null;
            ResultSet rs=null;
            try {
                con=Util.getConnection();
            } catch (SQLException e3) {
                // TODO Auto-generated catch block
                e3.printStackTrace();
            }
            try {
                state=con.createStatement();
                rs=state.executeQuery(sql);
                while(rs.next())
                {
                    String keywords=rs.getString("keywords");
                    String[] split = keywords.split(",");
                    for(int i=0;i<split.length;i++)
                    {
                        if(map.get(split[i])==null)
                        {
                            map.put(split[i],0);
                        }
                        else
                        {
                            map.replace(split[i], map.get(split[i])+1);
                        }
                    }
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Util.close( con, state,rs);
            sorted = map
                    .entrySet()
                    .stream()
                    .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
                    .collect(
                            Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
                                    LinkedHashMap::new));
            return sorted;
        }
    作者:哦心有
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    jdk1.5线程知识列表
    linux 常用命令记录
    spring batch 编码问题
    maven
    统计文本中每个单词出现的次数
    生成优惠券,并将优惠券存入Mysql
    python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function
    CSRF verification failed. Request aborted. 表单提交方法为POST时的报错
    InsecureRequestWarning: Unverified HTTPS request is being made.解决方法
    SQL Server 打印九九乘法表
  • 原文地址:https://www.cnblogs.com/haobox/p/14907745.html
Copyright © 2020-2023  润新知