Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

enter image description here

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 :

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.

like image 958
toshiro92 Avatar asked Apr 26 '13 20:04

toshiro92


People also ask

How do I fix error 404 in Tomcat?

This error indicates that the server could not find the desired resource. This resource can be any file such as JSP, HTML, or image resource. Usually, the resource is present, but it is referenced incorrectly. In most cases, you can fix this by correcting the URL.

How do I fix HTTP Status 404 not found?

The simplest and easiest way to fix your 404 error code is to redirect the page to another one. You can perform this task using a 301 redirect. What's 301, you may ask? It's a redirect response code that signals a browser that the content has been transferred to another URL.

What should be done in Tomcat error 404 the origin server did not find current representation for the target resource or is not willing to disclose?

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. This error means the server could not find the requested resource (JSP, HTML, images…) and returns HTTP status code 404. Most of the time, you can fix this error by correcting the URL.


2 Answers

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">
like image 76
Kevin Bowersox Avatar answered Oct 07 '22 19:10

Kevin Bowersox


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.

like image 34
Ravi K Thapliyal Avatar answered Oct 07 '22 21:10

Ravi K Thapliyal