课堂小项目(5)
作者:互联网
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