Servlets 如何为Javaservlet获取正确的URL?
我正在从事一个基于web的项目,在该项目中,我必须通过以下请求将数据从JavaScript传递到Java的Servlet:Servlets 如何为Javaservlet获取正确的URL?,servlets,http-status-code-404,url-pattern,Servlets,Http Status Code 404,Url Pattern,我正在从事一个基于web的项目,在该项目中,我必须通过以下请求将数据从JavaScript传递到Java的Servlet: var xhr = new XMLHttpRequest(); xhr.open('GET', 'DCCServlet?command=' + encodeURIComponent(command), true); xhr.send(null); 以下是Servlet的代码: 包装网 import javax.servlet.ServletException; impor
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>
我猜我的URL搞错了,但我真的不知道。如果是这样的话,正确的URL应该是什么?如果不是,可能是什么问题?在xml中,我认为您已经将servlet映射到了“/DCC”,但是您在GET中请求“/DCCServlet”您给了它一个
/DCC
的URL模式,但是您在/DCCServlet
上调用它,你到底想要什么?一定是这样的。我的错,我还是个新手。谢谢
GET http://localhost:8080/PiRail/DCCServlet?command=func%2044%204 404 (Not Found)