[英]Tomcat gives 404 when i call my servlet
我無法使用表單訪問我的servlet。 我檢查了arborescence,web.xml和表單,但我看不出任何問題。 我使用Eclipse和“web動態項目”。
有我的樹枝:
我的web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Z-ProjetJ2EE</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<servlet-name>CommandeServlet</servlet-name>
<servlet-class>controleur.CommandeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CommandeServlet</servlet-name>
<url-pattern>/urlCommandeServ</url-pattern>
</servlet-mapping>
</web-app>
我的表單(我嘗試了完整的網址,但它沒有用):
<form action="/urlCommandeServ" method="post">
而我的servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String cat = request.getParameter("categorie");
Float prix = Float.parseFloat(request.getParameter("prix"));
response.sendRedirect("CreerCommande?cat=" + cat+"&prix="+prix);
我在eclipse中沒有任何錯誤,tomcat中的日志文件夾是空的。 你可以幫幫我嗎 ?
編輯:
有我的錯誤:
我同意關於我在response.sendRedirect上的錯誤的回復,但這不是我的錯誤的真正主題:)即使我刪除了doPost上的所有代碼,我有這個錯誤,而不是白頁。
執行重定向時,您需要包含.jsp
。
response.sendRedirect("CreerCommande.jsp?cat=" + cat+"&prix="+prix);
代替:
response.sendRedirect("CreerCommande?cat=" + cat+"&prix="+prix);
還要將您的contextpath添加到表單中的url。
<form action="<%=request.getContextPath()%>/urlCommandeServ" method="post">
將<form>
html更改為
<form action="urlCommandeServ" method="post">
當您發布到/urlCommandeServ
您要求Tomcat(或您的Web服務器)查找名為urlCommandeServ
的Web應用程序,該應用程序不存在,因此您將獲得404。
你為什么不使用RequestDispatcher?
RequestDispatcher dispatcher = request.getRequestDispatcher("yourPage.jsp");
if (dispatcher != null){
dispatcher.forward(request, response);
}
見: SO Link
“如果這個代碼在你的J2SE中工作,那就意味着你需要在某個地方包含一個包含com.mysql.jdbc.Driver類的JAR文件(所謂的JDBC驅動程序)。這個JAR需要在Tomcat中可見。所以,我建議放置物理位置的mysql-jdbc.jar到項目的/ WEB-INF / lib目錄。“
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.