0

I've a JSF 2.0 web application running in tomcat 7.0.29 and tried to use the Servlet 3.0 annotation but the servlet didn't work as i can't see the log written in its init() method.

i ve read many answers for the same problem but still not seccessful.

This how my files look:

The web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="false"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
  <display-name>GestionCongesFeki</display-name>
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>


<!--   <servlet> -->
<!--     <servlet-name>AjaxRelaisServlet</servlet-name> -->
<!--     <servlet-class>utils.AjaxRelaisServlet</servlet-class> -->
<!--     <load-on-startup>2</load-on-startup> -->
<!-- </servlet> -->
<!-- <servlet-mapping> -->
<!--     <servlet-name>AjaxRelaisServlet</servlet-name> -->
<!--     <url-pattern>/AjaxRelaisServlet/*</url-pattern> -->
<!-- </servlet-mapping> -->

</web-app>

the pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>esprit.pfe2013</groupId>
  <artifactId>GestionCongesFeki</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>GestionCongesFeki Maven Webapp</name>
  <url>http://maven.apache.org</url>

<repositories>
        <repository>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>       
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <name>Maven Plugin Repository</name>
            <url>http://repo1.maven.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>

<!--   <repositories> -->
<!-- <repository> -->
<!--            <id>central</id> -->
<!--            <name>Central Repository</name> -->
<!--            <url>http://repo.maven.apache.org/maven2</url> -->
<!--            <layout>default</layout> -->
<!--            <snapshots> -->
<!--                <enabled>false</enabled> -->
<!--            </snapshots> -->
<!--        </repository> -->


<!--    </repositories> -->

    <dependencies>

         <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>3.0.1</version>        
         </dependency>



        <!-- PrimeFaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.4.1</version>
        </dependency>


        <!-- JSF 2 -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.11</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>




        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.13</version>
        </dependency>


        <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>
</dependency>






    </dependencies>
    <build>
    <finalName>GestionCongesFeki</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>  

</project>

and the servlet :

package utils;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AjaxRelaisServlet
 */
@WebServlet("/AjaxRelaisServlet") 
public class AjaxRelaisServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjaxRelaisServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        System.out.println("This is AjaxRelaisServlet initialisaton !!!");
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Actually the servlet was working fine when i was declaring it in the web.xml(the commented code) but not when i used the annotation. By the way , i would like to know if i can get rid of the web.xml when this problem will be resolved, but i don't know how the JSF servlet will be declared without it.

Thanks for help!

kolossus
  • 20,559
  • 3
  • 52
  • 104
Bardelman
  • 2,176
  • 7
  • 43
  • 70
  • Where re u BalusC ! i know u re an expert on this !!! :) – Bardelman Mar 29 '13 at 09:05
  • 1
    He can't hear you. Muahahahahahhahahahhahah >:D – kolossus Mar 29 '13 at 14:32
  • u look too happy for this andu laugh instead of trying to help me :D – Bardelman Mar 29 '13 at 14:38
  • Sorry, couldn't help myself XD. Try this init annotation instead: `@WebServlet(name="AjaxRelaisServlet",urlPatterns={"/AjaxRelaisServlet"})`. While the doc doesn't explicitly say so, I'd imagine the `name` attr is necessary for dispatch. You couldn't configure the same servlet in a web.xml without a ``. As for replacing web.xml, you might want to slow down a bit there. It still has it's uses, take for example: You currently can't specify filter ordering using annotations alone. – kolossus Mar 29 '13 at 15:01
  • well,thank you kolossus, i ve tried it but i was actually expecting the init() method to be executed when the application starts. When working with annotations init() works only when i call other methods doPost() or doPost()..means that instanciating the servlet by the application became as in a "lazy mode" whatever the @WebServlet annotation contains. – Bardelman Mar 29 '13 at 15:51
  • As i used the init() only to test if the servlet work so it s ok for me. it would a problem if i need the init() to work from the application start. By the way, i remember the servlet coudn't being loaded when starting the application also when using the web.xml once in the past. – Bardelman Mar 29 '13 at 15:56
  • If the lazy-loading is the problem, add `loadOnStartup=1` to the `@WebServlet` init params to cause the servlet to be loaded when the container loads the webapp. So to be clear, what was the original problem? Servlet wasn't loading at all or wasn't loading on startup? – kolossus Mar 29 '13 at 16:13
  • Thanks ! it works now !!. Honnestly, i had to read more about the Servlet 3.0 API.. – Bardelman Mar 29 '13 at 16:22
  • You're welcome :). I want to post an answer, what exactly was the original problem? – kolossus Mar 29 '13 at 16:27
  • Yes the problem actually was that the Servlet wasn't loading on startup. as i was testing if the servlet is loaded only with the init() i thought it doesn't.. – Bardelman Mar 29 '13 at 16:28
  • if this answer solved your issue, please accept by clicking on the green checkmark next to it. Cheers – kolossus Mar 29 '13 at 17:06

1 Answers1

6

Add the loadOnStartup=1 attribute to your @WebServlet annotation to cause the servlet to load on app startup. This attribute is analogous to the <load-on-startup/> element you'll find in the web.xml.

    @WebServlet(name="AjaxRelaisServlet",loadOnStartup=1,urlPatterns={"/AjaxRelaisServlet"})

When you have a hammer, every problem looks like a nail.

On the issue of replacing the web.xml entirely, you should be aware that there are some things that the annotations based model currently cannot achieve. Filter ordering, for example, cannot be achieved with annotations only (AFAIK). Also, you should realise thata concrete configuration document is self-documenting. Maintainers of your system can take one look at the web.xml and figure out what's going on in your app, config wise. Contrast that with an annotations-only architecture. It'll be painstaking to figure out the wiring of all the pieces of the system.

kolossus
  • 20,559
  • 3
  • 52
  • 104