简体   繁体   中英

Tomcat gives 404 when i call my servlet

I can't access to my servlet i call with a form. I checked the arborescence, web.xml and the form, but i can't see any problem. I use Eclipse with a "web dynamic project".

There is my arborescence :

在此输入图像描述

My 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 :

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. 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.

You need to include .jsp when performing the redirect.

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.

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

Change your <form> html to

<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.

Why don't you use RequestDispatcher?

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

See: 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM