ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

课堂小项目(5)

2022-06-13 11:32:23  阅读:186  来源: 互联网

标签:name 项目 request new import 课堂 response String


Servlet包中的内容:


 

package Servlet;

import Dao.DB;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

import pojo.Lunwen;
import pojo.Word;

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 java.io.IOException;
import java.util.*;

@WebServlet("/PaperServlet")
public class PaperServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public PaperServlet() {
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
String method = request.getParameter("method");
if(method.equals("Paper")) {
Paper(request, response);
}else if(method.equals("Search")){
Search(request, response);
}
}

private void Search(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// TODO Auto-generated method stub
String name=request.getParameter("name");
DB db = new DB();
List<Lunwen> list = db.listAllWord(name);
request.setAttribute("list",list);
request.getRequestDispatcher("paper.jsp").forward(request, response);
}

private void Paper(HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
DB db = new DB();
List<Lunwen> paperlist = db.Findlunwen();
List<Word> wordlist = new ArrayList<Word>();
String[] names = new String[100000];
int count = 0;
for(Lunwen paper:paperlist) {
String name = paper.getTitle();
String[] namestemp = name.split("_");
for(int i = 0; i < namestemp.length; i++) {

names[count++] = namestemp[i];
}
//names = (String[]) ArrayUtils.addAll(namestemp,names);
}

System.out.println(count);
HashMap<String,Integer> name_value = new HashMap<>();
for(String name:names) {

if(name_value.containsKey(name)){
name_value.put(name,name_value.get(name)+1);
}else{
name_value.put(name,1);
}
}

List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(name_value.entrySet()); //转换为list

list.sort(new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});

for (int i = 1; i <= 20; i++) {
Word word = new Word();

word.setName( list.get(i).getKey());
word.setValue(list.get(i).getValue());
wordlist.add(word);

System.out.println(list.get(i).getKey() + ": " + list.get(i).getValue());
}


/*for(String name:name_value.keySet()) {
Word word = new Word();
if(name!=null&&(name_value.get(name)>0)&&(name.length()>2)) {
word.setName(name);
word.setValue(name_value.get(name));
wordlist.add(word);
}
}*/
response.setContentType("text/html;charset=utf-8");
/*Gson gson = new Gson();
String json = gson.toJson(wordlist);*/
System.out.println(JSONObject.toJSON(wordlist).toString());
response.getWriter().write(JSONObject.toJSON(wordlist).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);
}

}

package Servlet;

import pojo.Lunwen;

import Dao.DB;
import com.alibaba.fastjson.JSONObject;
import pojo.Vo2;

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/searchServlet")
public class searchServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int month = Integer.parseInt(request.getParameter("month"));
List<Lunwen> infs = new ArrayList<>();
if(month == 1) {
System.out.println(month);
DB db = new DB();
infs = db.Findlunwen();

}else if(month == 2) {

System.out.println("+++++++");
String Title = request.getParameter("ltitle");
String keywords = request.getParameter("lkey");

String sql = "select * from lunwen where 1=1 ";

if (!Title.equals("")) {
sql = sql + "and title like" + "\"%" + Title + "%\"";

}/*else if(!name.equals("")){

sql=sql+"and name=" + "\"%" +name + "%\"";
}*/ else if (!keywords.equals("")) {
sql = sql + "and keywords like" + "\"%" + keywords + "%\"";

}

System.out.println(sql);
DB db = new DB();
infs = db.FindLunwenAll(sql);

}
response.setContentType("text/html;charset=utf-8");
Vo2 vo = new Vo2();
vo.setCode(0);
vo.setMsg("success");
vo.setCount(infs.size());
vo.setData(infs);

System.out.println(JSONObject.toJSON(vo).toString());
response.getWriter().write(JSONObject.toJSON(vo).toString());


}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

this.doGet(request,response);

}
}


标签:name,项目,request,new,import,课堂,response,String
来源: https://www.cnblogs.com/wlqyyds/p/16370217.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有