2024/5/23

import javax.servlet.*;
2import javax.servlet.http.*;
3import java.io.IOException;
4
5public class SubwayServlet extends HttpServlet {
6    
7    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
8        processRequest(request, response);
9    }
10    
11    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
12        processRequest(request, response);
13    }
14    
15    private void processRequest(HttpServletRequest request, HttpServletResponse response)
16            throws ServletException, IOException {
17        // 获取用户输入
18        String operation = request.getParameter("operation");
19        String lineName = request.getParameter("lineName");
20        String startStation = request.getParameter("startStation");
21        String endStation = request.getParameter("endStation");
22
23        // 初始化或读取地铁线路数据,这里简化处理,实际应用中可能需要数据库或文件读取
24        SubwayMessage("Subway.txt"); // 假设这是读取数据的方法
25
26        response.setContentType("text/html;charset=UTF-8");
27        PrintWriter out = response.getWriter();
28
29        try {
30            if ("1".equals(operation)) { // 查询线路信息
31                SearchLine(lineName);
32                out.println("<h1>线路信息</h1>");
33                // 注意:此处需转换SearchLine输出为HTML格式输出
34            } else if ("2".equals(operation)) { // 查询最短路径
35                if (stationHashMap.containsKey(startStation)) {
36                    SearchShortPath(startStation, endStation);
37                    PrintMessage(startStation, endStation);
38                    out.println("<h1>最短路径</h1>");
39                    // 同样,PrintMessage的输出需要转换为HTML格式
40                } else {
41                    out.println("<h1>不存在该起始站</h1>");
42                }
43            }
44        } finally {
45            out.close();
46        }
47    }
48}

步骤 2: 配置web.xml

WEB-INF/web.xml中配置Servlet:

Xml
解释
 
 
1<web-app>
2    <servlet>
3        <servlet-name>SubwayServlet</servlet-name>
4        <servlet-class>your.package.SubwayServlet</servlet-class>
5    </servlet>
6    <servlet-mapping>
7        <servlet-name>SubwayServlet</servlet-name>
8        <url-pattern>/subway</url-pattern>
9    </servlet-mapping>
10</web-app>

步骤 3: 创建HTML表单

创建一个简单的HTML页面index.html,供用户输入查询条件:

Html
解释
 
 
1<!DOCTYPE html>
2<html lang="en">
3<head>
4    <meta charset="UTF-8">
5    <title>地铁查询系统</title>
6</head>
7<body>
8    <h1>地铁查询系统</h1>
9    <form action="subway" method="post">
10        <input type="hidden" name="operation" value="1">
11        <label>查询线路名:<input type="text" name="lineName"></label><br>
12        <input type="submit" value="查询线路信息">
13    </form>
14    <hr>
15    <form action="subway" method="post">
16        <input type="hidden" name="operation" value="2">
17        <label>起始站:<input type="text" name="startStation"></label><br>
18        <label>终点站:<input type="text" name="endStation"></label><br>
19        <input type="submit" value="查询最短路径">
20    </form>
21</body>
22</html>

注意事项

  • 上述代码仅为示例,实际部署时需要考虑异常处理、数据安全性、以及如何将控制台输出转换为适合Web页面展示的HTML格式等。
  • 为了更好地管理用户界面和后端逻辑,可以考虑使用MVC框架(如Spring MVC)或前端JavaScript框架(如React、Vue.js)来构建更现代化的Web应用。
  • 对于复杂的数据处理,可能还需要数据库集成来持久化数据和提高效率。
 
 

package SubwayResult;

import java.util.ArrayList;

public class Line { String LineName;/记录当前线路名/ ArrayList Station = new ArrayList<>();/记录该线路中所有的站点名/

解释
 
 
1public String getLineName() {
2    return LineName;
3}
4
5public void setLineName(String lineName) {
6    LineName = lineName;
7}
8
9public ArrayList<String> getStation() {
10    return Station;
11}
12
13public void setStation(ArrayList<String> station) {
14    Station = station;
15}

}

posted @ 2024-05-23 22:45  几条小鱼  阅读(9)  评论(0)    收藏  举报