Java 我们希望根据通过URL传递的id号提供服务。 我们可以在数据库中存储一个表,指示id号和文档之间的对应关系。或者我们可以将这些数据存储到CSV文件中。不管怎样,对于这样一个相对较小的表,我都会在Servlet启动时将整个表加载到内存中。您可以将代码放在init()方法中,以便将数据加载到应用程序中

Java 我们希望根据通过URL传递的id号提供服务。 我们可以在数据库中存储一个表,指示id号和文档之间的对应关系。或者我们可以将这些数据存储到CSV文件中。不管怎样,对于这样一个相对较小的表,我都会在Servlet启动时将整个表加载到内存中。您可以将代码放在init()方法中,以便将数据加载到应用程序中,java,http,url,servlets,Java,Http,Url,Servlets,然后我们有这样的东西: static HashMap<Integer, String> fileMap; public void init() { fileMap = new HashMap<> (); fileMap.put(1,"thing1.xml"); fileMap.put(2,"thing2.xml"); fileMap.put(3,"Otherthingy.xml"); } 答案可能取决于您想对URI中的信息做什么。编写RE

然后我们有这样的东西:

static HashMap<Integer, String> fileMap;
public void init() {
    fileMap = new HashMap<> ();
    fileMap.put(1,"thing1.xml");
    fileMap.put(2,"thing2.xml");
    fileMap.put(3,"Otherthingy.xml");
}

答案可能取决于您想对URI中的信息做什么。编写REST样式的应用程序时,URI是将信息传递给应用程序的主要方式之一。因此,如果您想重新考虑您对附加包的厌恶,因为RESTAPI非常受欢迎,这是有充分理由的

坚持使用普通的servlet,假设我们有50个xml文档,我们希望根据通过URL传递的id号提供服务。 我们可以在数据库中存储一个表,指示id号和文档之间的对应关系。或者我们可以将这些数据存储到CSV文件中。不管怎样,对于这样一个相对较小的表,我都会在Servlet启动时将整个表加载到内存中。您可以将代码放在init()方法中,以便将数据加载到应用程序中

然后我们有这样的东西:

static HashMap<Integer, String> fileMap;
public void init() {
    fileMap = new HashMap<> ();
    fileMap.put(1,"thing1.xml");
    fileMap.put(2,"thing2.xml");
    fileMap.put(3,"Otherthingy.xml");
}

我忘了指定,但我在这里只处理一个servlet。为什么它必须是一个servlet?您希望执行的操作本质上可以成为它们自己的servlet。我忘了指定,但我在这里只处理一个servlet。为什么它必须是一个servlet?您希望执行的操作本质上可以成为它们自己的servlet。
Map<String,String> myActions = new HashMap<String,String>();
myActions.put("/1","one.xml");
.....
String uri = request.getRequestURI ();

if (myActions.containsKey(uri)) {
   String value = myActions.get(uri);
   do something with value
} else {
   throw error message...
}
public interface Action {
    String execute(HttpServletRequest req, HttpServletResponse res) throws ServletException,
            IOException;
}
<servlet>
    <servlet-name>processUserXml</servlet-name>
    <servlet-class>com.example.server.ProcessUserXml</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>processUserXml</servlet-name>
    <url-pattern>/processuser</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>processItemXml</servlet-name>
    <servlet-class>com.example.server.ProcessItemXml</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>processItemXml</servlet-name>
    <url-pattern>/processitem</url-pattern>
</servlet-mapping>
static HashMap<Integer, String> fileMap;
public void init() {
    fileMap = new HashMap<> ();
    fileMap.put(1,"thing1.xml");
    fileMap.put(2,"thing2.xml");
    fileMap.put(3,"Otherthingy.xml");
}
public void doGet (HttpServletRequest request, HttpServletResponse response) 
                               throws IOException  {
        String uri = request.getRequestURI();
        int slash = uri.indexOf("/");
        if(slash+1 == uri.length())
            //throw an error

        String idString = request.getRequestURI().substring(slash+1);
        int id = Integer.parseInt(idString);
        //should put this in a try catch block and throw an error if it is not a number.
        String requestedFile = fileMap.get(id);
        if(requestedFile == null)
            //throw some error.
        // Do whatever you need to do with the file.
}