0

i want to write a simple servlet in my maven project but i'm getting this error the requested url (/src/main/java/web.servlets.FunctionalTestServlet) was not found on this server.

here's my code :

Servlet: located in src/main/java package web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FunctionalTestServlet extends HttpServlet
  {

public void doGet(HttpServletRequest request,HttpServletResponse response)throws  
    ServletException, IOException {

     PrintWriter out = response.getWriter();
     out.println( "SimpleServlet Executed" );
     out.flush();
     out.close();
    }


public void doPost(HttpServletRequest request,  HttpServletResponse response)
          throws IOException, ServletException{
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();  
          String fileName = request.getParameter("testfile");
           out.println("<b><font color='blue'>The File name is 
                     :</font></b>" 
          + "<b>"+ fileName +"</b>" + "<br>");
        }
          }

jsp page:under src/main/webapp

 <html:file properties="tonFichier" name="tonForm"/>


  <form action="/src/main/java/web.servlets.FunctionalTestServlet"   
  enctype="multipart/form-data" method="post">
    <p>
   Type some text (if you like):<br>
   <input type="text" name="textline" size="30">
   </p>
   <p>
    Please specify a Test , or a set of tests:<br>
   <input type="file" name="testfile" size="40" >
   </p>
   <div>
  <input type="submit" value="Execute Test">

  </div>
 </form>
 </body>
 </html>

Here's my web.xml : under /WebTestAutomatisation/src/main/webapp/WEB-INF/web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>FunctionalTestServlet</servlet-name>
 <servlet-class>web.servlets.FunctionalTestServlet.java</servlet-class>
</servlet> 
<servlet-mapping>
<servlet-name>FunctionalTestServlet</servlet-name>
 <url-pattern>/*</url-pattern>
</servlet-mapping>

  </web-app>
Amira Manai
  • 2,599
  • 8
  • 40
  • 60

2 Answers2

1

I realize that this is kind of an old thread, but I've been struggling with a very similar problem and just found the solution to it. I thought I'd share...

I am relatively new to Maven and have been struggling with a very similar problem using maven 3.1, tomcat 7.0.37, java 1.7, and Eclipse Juno. I would create a maven project using maven-archetype-webapp, and then convert the project to faceted form using the Dynamic Web Module, setting my content directory to /src/main/webapp.

I would create my servlet class and make sure to include servlet-api.jar to the java build path. I also updated my pom.xml with a dependency to javax.servlet-api with a scope of 'provided':

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1-b05</version>
    <scope>provided</scope>
</dependency>

I included the project to the tomcat server, and when visiting the servlet url, I would get a 404 error - The requested resource is not available.

I struggled with this for like two days and just this morning discovered the solution. The part that I was missing was to do a 'maven install'. Since I'm using Eclipse, that involved right clicking on the project, selecting 'Run As', and then 'Maven Install'. After that, I stopped/restarted my local tomcat server, and I was then able to successfully get the servlet to work.

Something else that I noticed is that when looking at the libraries tab in the java build path, the JRE system library that was configured out of the box was J2SE-1.5. I changed mine to JavaSE-1.7 (jdk1.7.0_13).

What ended up helping me the most on this problem was this video:

http://www.youtube.com/watch?v=8Waf4jBbino

I hope this helps someone!

Stephen Spalding
  • 231
  • 4
  • 11
0
<servlet-name>FunctionalTestServlet</servlet-name>
 <servlet-class>web.servlets.FunctionalTestServlet.java</servlet-class>
</servlet>

You need to put the class name there. Drop the ".java" at the end.

<servlet-mapping>
<servlet-name>FunctionalTestServlet</servlet-name>
 <url-pattern>/*</url-pattern>
</servlet-mapping>

And with this mapping, your servlet will get all requests. So the URL can be '/', no need for '/src/main/java/web.servlets.FunctionalTestServlet'. You probably want a pattern of /FunctionalTest.

Also, the web server usually does not even have access to the source files, just the compiled class files. The URL path is mapped in web.xml and has nothing at all to do with package names or class names.

Thilo
  • 257,207
  • 101
  • 511
  • 656