简体   繁体   English

当我调用我的servlet时,Tomcat给出了404

[英]Tomcat gives 404 when i call my servlet

I can't access to my servlet i call with a form. 我无法使用表单访问我的servlet。 I checked the arborescence, web.xml and the form, but i can't see any problem. 我检查了arborescence,web.xml和表单,但我看不出任何问题。 I use Eclipse with a "web dynamic project". 我使用Eclipse和“web动态项目”。

There is my arborescence : 有我的树枝:

在此输入图像描述

My web.xml : 我的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>

My form (i tried the complete url, but it didn't works) : 我的表单(我尝试了完整的网址,但它没有用):

<form action="/urlCommandeServ" method="post">

And my servlet : 而我的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);

I didn't have any error in eclipse, and the log folder in tomcat is empty. 我在eclipse中没有任何错误,tomcat中的日志文件夹是空的。 Could you help me ? 你可以帮帮我吗 ?

EDIT: 编辑:

There is my error : 有我的错误:

错误404

I agree for responses about my error on response.sendRedirect, but this is not the real subject of my error :) Even if i erase all of my code on doPost, i have this error, instead of a white page. 我同意关于我在response.sendRedirect上的错误的回复,但这不是我的错误的真正主题:)即使我删除了doPost上的所有代码,我有这个错误,而不是白页。

You need to include .jsp when performing the redirect. 执行重定向时,您需要包含.jsp

response.sendRedirect("CreerCommande.jsp?cat=" + cat+"&prix="+prix);

instead of: 代替:

response.sendRedirect("CreerCommande?cat=" + cat+"&prix="+prix);

Also add your contextpath to the url in the form. 还要将您的contextpath添加到表单中的url。

<form action="<%=request.getContextPath()%>/urlCommandeServ" method="post">

Change your <form> html to <form> html更改为

<form action="urlCommandeServ" method="post">

When you're posting to /urlCommandeServ you're asking Tomcat (or your web server) to look for a web application named urlCommandeServ which isn't there and hence you get a 404. 当您发布到/urlCommandeServ您要求Tomcat(或您的Web服务器)查找名为urlCommandeServ的Web应用程序,该应用程序不存在,因此您将获得404。

Why don't you use RequestDispatcher? 你为什么不使用RequestDispatcher?

RequestDispatcher dispatcher = request.getRequestDispatcher("yourPage.jsp");
if (dispatcher != null){
    dispatcher.forward(request, response);
}

See: SO Link 见: SO Link

"If this code works in your J2SE it means you need to have a JAR file somewhere containing com.mysql.jdbc.Driver class (so called JDBC driver). This JAR needs to be visible in Tomcat. So, I would suggest to place mysql-jdbc.jar at physical location to /WEB-INF/lib directory of your project." “如果这个代码在你的J2SE中工作,那就意味着你需要在某个地方包含一个包含com.mysql.jdbc.Driver类的JAR文件(所谓的JDBC驱动程序)。这个JAR需要在Tomcat中可见。所以,我建议放置物理位置的mysql-jdbc.jar到项目的/ WEB-INF / lib目录。“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Servlet提供404(Eclipse + Tomcat) - Servlet gives 404 (Eclipse + Tomcat) 自定义servlet在Tomcat 6上提供404,在7上运行良好 - Custom servlet gives 404 on Tomcat 6, works fine on 7 Servlet给出404而不是加载适当的页面。 启动Tomcat时控制台显示严重错误 - Servlet gives 404 instead of loading appropriate page. Console says SEVERE error when launching Tomcat 我的基于Spring的Restful Web服务在部署到独立的tomcat而不是Eclipse的Tomcat时给出404 - My Spring based Restful webservice gives 404 when deploy to standalone tomcat instead of Eclipse's Tomcat 重定向到Servlet时的Java Servlet和Apache Tomcat 404 - Java Servlet and Apache Tomcat 404 when redirecting to Servlet 从Tomcat运行servlet时发生ClassNotFoundException(内部类) - ClassNotFoundException (inner class) when I run my servlet from Tomcat java servlet上的Tomcat 404 - Tomcat 404 on java servlet Spring Boot - 当我调用API时,它会给出404错误 - Spring Boot - When I call API, its gives 404 error 尝试调用servlet时遇到ERROR 404 - ERROR 404 encountered when attempting to call a servlet Tomcat正在运行,但是在尝试访问我的HTML时给出了404,但安装后却显示了Tomcat的默认页面 - Tomcat running but gives 404 when trying to access my html, but instead shows tomcat's default page after installation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM