2024/04/10(2024春季)
学习时长:2小时
代码行数:200+行
博客数量:1篇
在昨天的基础上,我们开始通过前端输入出发地和目的地的数据来调用之前设计的计算最短路径的算法计算两地之间的最短路线包括换乘信息。最后再将信息返回到javaweb前端页面。我们就可以通过前端来查询最短路径了。
package com.example.demo; import java.io.*; import jakarta.servlet.ServletException; import jakarta.servlet.http.*; import jakarta.servlet.annotation.*; @WebServlet(name = "helloServlet", value = "/function1") public class Function1 extends HttpServlet { private String message; public void init() { message = "Hello World!"; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { String startstation=request.getParameter("startplace"); String endstation=request.getParameter("endplace"); GetInfo getInfo = GetInfo.getGetInfo(); if (startstation.isEmpty()||endstation.isEmpty()) { request.setAttribute("start",startstation); request.setAttribute("end",endstation); message="填写站点不能为空"; request.setAttribute("message",message); // 转发给另一个Servlet来处理 try { request.getRequestDispatcher("index.jsp").forward(request,response); } catch (ServletException e) { throw new RuntimeException(e); } return; } boolean isStart=false; boolean isEnd=false; for(Station s :getInfo.stations){ System.out.println(s.name); if (startstation.equals(s.name)) isStart = true; if (endstation.equals(s.name)) isEnd = true; if (isStart && isEnd) break; } if(!isStart){ message="起点站不存在"; request.setAttribute("start",startstation); request.setAttribute("end",endstation); //设置请求上属性的参数 request.setAttribute("message",message); // 转发给另一个Servlet来处理 try { request.getRequestDispatcher("index.jsp").forward(request,response); } catch (ServletException e) { throw new RuntimeException(e); } return; } if(!isEnd){ message="终点站不存在"; request.setAttribute("start",startstation); request.setAttribute("end",endstation); //设置请求上属性的参数 request.setAttribute("message",message); // 转发给另一个Servlet来处理 try { request.getRequestDispatcher("index.jsp").forward(request,response); } catch (ServletException e) { throw new RuntimeException(e); } return; } String result=new GetShortestPath().getShortPath(startstation,endstation); //设置请求上属性的参数 request.setAttribute("result",result); request.setAttribute("start",startstation); request.setAttribute("end",endstation); // 转发给另一个Servlet来处理 try { request.getRequestDispatcher("index.jsp").forward(request,response); } catch (ServletException e) { throw new RuntimeException(e); } } public void destroy() { } }
package com.example.demo; import java.util.Scanner; import static java.lang.System.exit; public class GetShortestPath { public String getShortPath(String start,String end) { Result result = Dijkstra.calculate(new Station(start), new Station(end)); String str = "共经过" + (result.getPassStations().size()+1) + "站"+"\n"; str += "所在线路:" +"\n"+ result.getPassStations().get(0).line + " " + result.getStartStation().name + "->"; String atLineName = result.getPassStations().get(0).line; Boolean isTransfer = false; for (int i = 0; i < result.getPassStations().size(); i++) { if (!atLineName.equals(result.getPassStations().get(i).line)) { isTransfer = true; atLineName = result.getPassStations().get(i).line; } else isTransfer = false; if (result.getPassStations().get(i).name.equals("")) str += "总站->"; else if (isTransfer) str += "换乘到" + atLineName + "线路 " + result.getPassStations().get(i).name + "->"; else str += result.getPassStations().get(i).name + "->"; } str = str.substring(0, str.length() - 2); Dijkstra.reset(); return str; } public static String getLineName(Station station) { for (SubwayLine s : GetInfo.lines) { for (Station st : s.stations) { if (st.name.equals(station.name)) return st.line; } } return ""; } }