![](/img/trans.png)
[英]Eclipse - How do I publish non-Java resources without a web.xml file?
[英]How do I map AJAX to servlet without web.xml
TL; DR:將@WebServlet(“ / Find-Customers”)放在servlet的開頭(通過Tomcat 7部署)不會將servlet映射到host:port / webproject / Find-Customers,即使該servlet在src中夾。
我試圖使用@WebServlet(“ / ...”)調用servlet,這是我過去做過的,但是這次還是出了點問題。 我從來沒有使用過web.xml,而且一切正常。 我在ajaxFxns.js中使用了Ajax POST方法,並在Java中輸入了“ Find-Customers”作為地址和以下內容:
package coreservlets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
@WebServlet("/Find-Customers")
public class ShowCustomers extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
// more code here
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
據我所知,Firebug說“ / Find-Customers”時顯示404 Not Found,這意味着@WebServlet函數未將Servlet正確映射到localhost:8080 / webproject / Find-Customers。 這是目錄結構(刪除了無關的東西):
Webproject
--src
--coreservlets
--ShowCustomers.java
--WebContent
--scripts
--ajaxFxns.js
--index.html
創建coreservlets文件夾時,應該做一些特別的事情,還是在開發人員環境中(我正在使用Eclipse)進行調試? Web.xml的實現也並不是很順利,這就是為什么我要問如何沒有它。 幫助將不勝感激!
可以顯示Ajax代碼嗎? Ajax調用的URL可能有問題。 例如,考慮以下兩種情況:
對於您在index.html和ajaxFxns.js下面發布的ShowCustomers Servlet,Ajax調用可以正常工作
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<script type="text/javascript" src="scripts/ajaxFxns.js"></script>
<title>Ajax post</title>
</head>
<body>
</body>
</html>
ajaxFxns.js:
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "Find-Customers", true);
xhttp.send();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.