结对作业开发进度(第八天)
第八天
对servlet中的各项信息与方法进行绑定
1 package com.web.servlet; 2 import com.alibaba.fastjson.JSON; 3 import com.pojo.*; 4 import com.service.ditieService; 5 import com.service.impl.ditieimpl; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import java.io.BufferedReader; 11 import java.io.IOException; 12 import java.util.ArrayList; 13 import java.util.List; 14 @WebServlet("/ditie/*") 15 public class ditie extends servlet{ 16 private ditieService ditieServices=new ditieimpl(); 17 public void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 //1. 调用service查询 19 List<ditiepojo> ditiepojos = ditieServices.selectAll(); 20 //2. 转为JSON 21 String jsonString = JSON.toJSONString(ditiepojos); 22 //3. 写数据 23 response.setContentType("text/json;charset=utf-8"); 24 response.getWriter().write(jsonString); 25 } 26 public void all1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 //1. 调用service查询 28 List<ditiepojo> ditiepojos = ditieServices.selectAll(); 29 30 request.setAttribute("list",ditiepojos); 31 request.getRequestDispatcher("/cha.jsp").forward(request,response); 32 } 33 public void ceshi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 request.setCharacterEncoding("utf-8"); 35 /* String qdian = request.getParameter("qidian"); 36 String zdain = request.getParameter("zdain");;*/ 37 38 request.setCharacterEncoding("utf-8"); 39 BufferedReader br = request.getReader(); 40 String params = br.readLine();//json字符串 41 lix lix = JSON.parseObject(params, lix.class); 42 String qidian = lix.getQidian(); 43 String zdain1 = lix.getZdain(); 44 zhunbei zhunbei=new zhunbei(); 45 String zuiduan = zhunbei.zuiduan(qidian, zdain1); 46 String[] s = zuiduan.split(" "); 47 int b=s.length; 48 List<zhandian1>zhandian1s=new ArrayList<zhandian1>(); 49 for(int i=0;i<b;i++) 50 { 51 zhandian1 zhandian1=new zhandian1(); 52 zhandian1.setZhandian1(s[i]); 53 zhandian1s.add(zhandian1); 54 } 55 String jsonString = JSON.toJSONString(zhandian1s); 56 //3. 写数据 57 response.setContentType("text/json;charset=utf-8"); 58 response.getWriter().write(jsonString); 59 60 /* request.setAttribute("jl",zuiduan); 61 request.getRequestDispatcher("/ceshi.jsp").forward(request,response);*/ 62 } 63 public void tiaocha1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 64 request.setCharacterEncoding("utf-8"); 65 BufferedReader br = request.getReader(); 66 String params = br.readLine();//json字符串 67 ditiepojo ditiepojo = JSON.parseObject(params, ditiepojo.class); 68 List<com.pojo.ditiepojo> tiaocha = ditieServices.tiaocha(ditiepojo); 69 String s = JSON.toJSONString(tiaocha); 70 //System.out.println(s); 71 response.setContentType("text/json;charset=utf-8"); 72 response.getWriter().write(s); 73 } 74 public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 75 //1. 接收 当前页码 和 每页展示条数 url?currentPage=1&pageSize=5 76 String _currentPage = request.getParameter("currentPage"); 77 String _pageSize = request.getParameter("pageSize"); 78 int currentPage = Integer.parseInt(_currentPage); 79 int pageSize = Integer.parseInt(_pageSize); 80 // 获取查询条件对象 81 BufferedReader br = request.getReader(); 82 String params = br.readLine();//json字符串 83 //转为 Brand 84 ditiepojo ditiepojo = JSON.parseObject(params, ditiepojo.class); 85 //2. 调用service查询 86 PageBean<ditiepojo> pageBean = ditieServices.selectByPageAndCondition(currentPage,pageSize,ditiepojo); 87 //2. 转为JSON 88 String jsonString = JSON.toJSONString(pageBean); 89 //3. 写数据 90 response.setContentType("text/json;charset=utf-8"); 91 response.getWriter().write(jsonString); 92 } 93 public void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 94 /* List<zhandian> select = ditieServices.select(); 95 *//* for(zhandian u:select) 96 { 97 String[] s = u.getZhandian1().split(" "); 98 int b=s.length; 99 for(int i=0;i<b;i++) 100 { 101 ditiepojo ditiepojo=new ditiepojo(null,null,null,null); 102 ditiepojo.setXianlu(u.getXianlu()); 103 ditiepojo.setZhandian(s[i]); 104 ditieServices.add(ditiepojo); 105 } 106 }*/ 107 List<ditiepojo> ditiepojos = ditieServices.selectAll(); 108 String zhandain1[]=new String[100000]; 109 for(ditiepojo u:ditiepojos) 110 { 111 List<ditiepojo> qzcah = ditieServices.qzcah(u.getZhandian()); 112 int b=qzcah.size(); 113 if(b>1) 114 { 115 for(int i=0;i<b;i=i+1) 116 { 117 for(int j=0;j<=b-1;j++) 118 {if(i!=j) 119 { 120 huanc huanc=new huanc(null,null,null); 121 huanc.setXianlu1(qzcah.get(i).getXianlu()); 122 huanc.setGzhandain(qzcah.get(i).getZhandian()); 123 huanc.setXianlu2(qzcah.get(j).getXianlu()); 124 ditieServices.add1(huanc); 125 } 126 127 } 128 129 } 130 } 131 } 132 133 } 134 public void zxhc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 135 request.setCharacterEncoding("utf-8"); 136 BufferedReader br = request.getReader(); 137 String params = br.readLine();//json字符串 138 lix lix = JSON.parseObject(params, lix.class); 139 String qidian = lix.getQidian(); 140 String zdain1 = lix.getZdain(); 141 zhunbei zhunbei=new zhunbei(); 142 String zuiduan = zhunbei.zuiduan(qidian, zdain1); 143 String zxlu = zhunbei.zxlu(); 144 String[] s = zxlu.split(" "); 145 int b=s.length;List<lu> lus=new ArrayList<>(); 146 for(int i=0;i<b;i++) 147 { 148 lu lu=new lu(); 149 lu.setLu(s[i]);lus.add(lu); 150 151 } 152 String jsonString = JSON.toJSONString(lus); 153 //3. 写数据 154 response.setContentType("text/json;charset=utf-8"); 155 response.getWriter().write(jsonString); 156 } 157 }
1 package com.web.servlet; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 public class Line { 6 public String getName() { 7 return name; 8 } 9 public void setName(String name) { 10 this.name = name; 11 } 12 public List<String> getStations() { 13 return stations; 14 } 15 public void setStations(List<String> line) { 16 this.stations = line; 17 } 18 private String name; 19 private List<String> stations = new ArrayList<String>();//����·�ϵ�����վ�� 20 }
1 package com.web.servlet; 2 3 import java.util.*; 4 5 public class Map { 6 public int[][] getSubwayline() { 7 return subwayline; 8 } 9 public void setSubwayline(int[][] subwayline) { 10 this.subwayline = subwayline; 11 } 12 public List<String> getStations() { 13 return stations; 14 } 15 public void setStations(List<String> stations) { 16 this.stations = stations; 17 } 18 public int[][] getPath() { 19 return path; 20 } 21 public void setPath(int[][] path) { 22 this.path = path; 23 } 24 private int[][] subwayline; 25 private static List<String> stations; 26 private int[][] path; 27 28 29 public int getIndex(String station) {// 30 return stations.indexOf(station); 31 } 32 33 public String getName(int index){ 34 return stations.get(index); 35 } 36 37 public Map(List<String> stations) {//��ʼ��������ͼ 38 this.stations = stations; 39 this.subwayline = new int[stations.size()][stations.size()]; 40 this.path = new int[stations.size()][stations.size()]; 41 for(int i=0;i<stations.size();i++) { 42 for(int j=0;j<stations.size();j++) { 43 if(i==j) subwayline[i][j] = 0; 44 else { 45 subwayline[i][j] = 999999; 46 subwayline[j][i] = 999999; 47 } 48 } 49 } 50 } 51 52 public void initDis(String start, String end) {//��ʼ�� 53 this.subwayline[getIndex(start)][getIndex(end)] = 1; 54 this.subwayline[getIndex(end)][getIndex(start)] = 1; 55 } 56 57 }
1 package com.web.servlet; 2 3 import java.util.*; 4 5 public class Result { 6 public int[][] getShortestPath() { 7 return ShortestPath; 8 } 9 10 public void setShortestPath(int[][] shortestPath) { 11 ShortestPath = shortestPath; 12 } 13 14 public int[][] getShortestDis() { 15 return ShortestDis; 16 } 17 18 public void setShortestDis(int[][] shortestDis) { 19 ShortestDis = shortestDis; 20 } 21 22 public static int getMax() { 23 return max; 24 } 25 26 public static final int max = 999999; 27 public int[][] ShortestPath; 28 public int[][] ShortestDis; 29 30 public Result(int[][] G) { 31 this.ShortestPath = new int[G.length][G.length]; 32 this.ShortestDis = new int[G.length][G.length]; 33 for(int i=0;i<G.length;i++) { 34 for(int j=0;j<G.length;j++) { 35 this.ShortestPath[i][j]=j; 36 this.ShortestDis[i][j]=G[i][j]; 37 this.ShortestDis[j][i]=G[j][i]; 38 } 39 } 40 41 //Floyd�����㷨 42 for(int k=0;k<G.length;k++) 43 for(int j=0;j<G.length;j++) 44 for(int i=0;i<G.length;i++) { 45 if(this.ShortestDis[i][j]>this.ShortestDis[i][k]+this.ShortestDis[k][j]) { 46 this.ShortestDis[i][j]=this.ShortestDis[i][k]+this.ShortestDis[k][j]; 47 this.ShortestPath[i][j]=this.ShortestPath[i][k]; 48 } 49 } 50 } 51 52 public int getMinDis(int i, int j) { 53 return this.ShortestDis[i][j]; 54 } 55 56 public List<Integer> indexToList(int i, int j){ 57 List<Integer> list = new ArrayList<>(); 58 while(i!=j) { 59 list.add(i); 60 i = this.ShortestPath[i][j]; 61 } 62 list.add(i); 63 return list; 64 } 65 }
1 package com.web.servlet; 2 import javax.servlet.ServletException; 3 import javax.servlet.http.HttpServlet; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 import java.io.IOException; 7 import java.lang.reflect.InvocationTargetException; 8 import java.lang.reflect.Method; 9 public class servlet extends HttpServlet { 10 //根据请求的最后一段路径来进行方法分发 11 @Override 12 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 13 //1. 获取请求路径 14 String uri = req.getRequestURI(); // /brand-case/brand/selectAll 15 16 //2. 获取最后一段路径,方法名 17 int index = uri.lastIndexOf('/'); 18 String methodName = uri.substring(index + 1); // /selectAll? selectAll? 19 20 //2. 执行方法 21 //2.1 获取BrandServlet /UserServlet 字节码对象 Class 22 23 Class<? extends servlet> cls = this.getClass(); 24 //2.2 获取方法 Method对象 25 try { 26 Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); 27 //2.3 执行方法 28 method.invoke(this,req,resp); 29 } catch (NoSuchMethodException e) { 30 e.printStackTrace(); 31 } catch (IllegalAccessException e) { 32 e.printStackTrace(); 33 } catch (InvocationTargetException e) { 34 e.printStackTrace(); 35 } 36 37 38 } 39 }
1 package com.web.servlet; 2 import com.pojo.ditiepojo; 3 import com.pojo.huanc; 4 import com.service.ditieService; 5 import com.service.impl.ditieimpl; 6 import java.util.List; 7 public class sunafa { 8 private ditieService ditieServices=new ditieimpl(); 9 /*public String jl[] =new String[10000];*/ 10 public String lu="";public String lz=""; 11 public String hx(String xianlu1,String xianlu2) 12 { 13 List<huanc> hcha = ditieServices.hcha(xianlu1); 14 int size = hcha.size(); 15 for(int i=0;i<size;i++) 16 { 17 System.out.println(hcha.get(i).getXianlu2()); 18 if(hcha.get(i).getXianlu2().equals(xianlu2)) 19 { 20 lu=lu+xianlu1+" "+hcha.get(i).getXianlu2()+";"; 21 continue; 22 // return 1; 23 } 24 else{ 25 lu=lu+hcha.get(i).getXianlu1()+" "; 26 String[] s = lu.split(" "); 27 // System.out.println(s[0]); 28 if(hcha.get(i).getXianlu2().equals(s[0])) 29 { 30 continue; 31 } 32 hx(hcha.get(i).getXianlu2(),xianlu2); 33 } 34 } 35 return lu; 36 } 37 38 }