如何为 Java Servlet 获取正确的 URL?
How to get correct URL for Java Servlet?
我正在开发一个基于 Web 的项目,我必须通过以下请求将数据从 JavaScript 传递到 Java s Servlet:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'DCCServlet?command=' + encodeURIComponent(command), true);
xhr.send(null);
这是 Servlet 的代码:
包裹网;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DCCServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException{
String command = request.getParameter("command");
System.out.println(command);
//Send string where it is needed
Startup.MC.sendCommand(command);
}
}
这里是 XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>PiRail</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
web.Startup
</listener-class>
</listener>
<servlet>
<servlet-name>DCCServlet</servlet-name>
<servlet-class>web.DCCServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DCCServlet</servlet-name>
<url-pattern>/DCC</url-pattern>
</servlet-mapping>
</web-app>
据我所知,servlet 没有问题,尽管我可能是错的。
无论出于何种原因,每次我执行发出请求的操作时,我都会在 Opera 控制台中收到 404 错误。
GET http://localhost:8080/PiRail/DCCServlet?command=func%2044%204 404 (Not Found)
我猜我弄错了 URL 但我真的不知道。如果是这样,正确的 URL 应该是什么?如果不是,可能是什么问题?
在 xml 中,我认为您已将 servlet 映射到“/DCC”,但您在 GET
中请求“/DCCServlet”
我正在开发一个基于 Web 的项目,我必须通过以下请求将数据从 JavaScript 传递到 Java s Servlet:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'DCCServlet?command=' + encodeURIComponent(command), true);
xhr.send(null);
这是 Servlet 的代码: 包裹网;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DCCServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException{
String command = request.getParameter("command");
System.out.println(command);
//Send string where it is needed
Startup.MC.sendCommand(command);
}
}
这里是 XML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>PiRail</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
web.Startup
</listener-class>
</listener>
<servlet>
<servlet-name>DCCServlet</servlet-name>
<servlet-class>web.DCCServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DCCServlet</servlet-name>
<url-pattern>/DCC</url-pattern>
</servlet-mapping>
</web-app>
据我所知,servlet 没有问题,尽管我可能是错的。 无论出于何种原因,每次我执行发出请求的操作时,我都会在 Opera 控制台中收到 404 错误。
GET http://localhost:8080/PiRail/DCCServlet?command=func%2044%204 404 (Not Found)
我猜我弄错了 URL 但我真的不知道。如果是这样,正确的 URL 应该是什么?如果不是,可能是什么问题?
在 xml 中,我认为您已将 servlet 映射到“/DCC”,但您在 GET
中请求“/DCCServlet”