General process of the system:
index.jsp input the station name (click "show site information", jump to list.jsp to read the table);
Background obtains id through station name; getIdbyname(String name)
The id is fed back to dijkstra(int vs,int vf) to find the shortest path and the vertex on the path (read the document to get the Graph);
dijkstra is still a number, so getNamebyid(int id) is also needed; use list < string > to return the site name on the shortest path
Finally, feedback to succeeded.jsp
Source code:
servlet layer:
busServletimport 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 entity.Bus; import service.busService; /** * Servlet implementation class busServlet */ @WebServlet("/busServlet") public class busServlet extends HttpServlet { private static final long serialVersionUID = 1L; busService bus=new busService(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if(method.equals("dijkstra")) { dijkstra(req,resp); }else if(method.equals("list")) { list(req,resp); }else if(method.equals("getIdbyname")) { getIdbyname(req,resp); } } private void getIdbyname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /** * Get id by name * Used to determine whether the input station name has inventory in the table */ req.setCharacterEncoding("utf-8"); String s = req.getParameter("start"); String f = req.getParameter("final"); int k1=bus.getIdbyname(s); int k2=bus.getIdbyname(f); if(k1!=-1&&k2!=-1) {//And operation, both sides are true at the same time req.setAttribute("message", "query was successful");//setAttribute Method is used to save content in an object and transfer it to jsp in req.getRequestDispatcher("busServlet?method=dijkstra").forward(req,resp);//getRequestDispatcher Method to go to the next page }else if(k1==-1||k2==-1){//Or operation, one on both sides is true req.setAttribute("message", "Station name is wrong, please make sure it is entered correctly"); req.getRequestDispatcher("index.jsp").forward(req,resp); } } private void list(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8"); List<Bus> buses = bus.readExcelList(); req.setAttribute("buses", buses); req.getRequestDispatcher("list.jsp").forward(req,resp); } private void dijkstra(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String s = req.getParameter("start"); String f = req.getParameter("final"); int vs=bus.getIdbyname(s); int vf=bus.getIdbyname(f); List<String> stations=bus.dijkstra(vs, vf); req.setAttribute("stations",stations);//setAttribute Method is used to save content in an object and transfer it to jsp in req.getRequestDispatcher("succeed.jsp").forward(req,resp);//getRequestDispatcher Method to go to the next page } }