facebook

how spring links to servlet?

💡
Our Forums Have Moved

For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #252682 Reply

    amir55
    Participant

    hi all

    just simply what to add in the web.xml to get the serlvet instanitiated such as listenr and dispacher

    suppose my servlet is simpleServlet.java

    thnaks

    #252699

    Riyad Kalla
    Member

    Moving to OT > Soft Dev

    #252722

    Robert Varga
    Participant

    @amir55 wrote:

    hi all

    just simply what to add in the web.xml to get the serlvet instanitiated such as listenr and dispacher

    suppose my servlet is simpleServlet.java

    thnaks

    I use the following stuff (in Spring 1.1) for an application with Velocity as view technology:

    
        <context-param>
            <param-name>defaultHtmlEscape</param-name>
            <param-value>true</param-value>
        </context-param>
    
    
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>
                org.springframework.web.filter.CharacterEncodingFilter
            </filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
    
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext*.xml</param-value>
        </context-param>
    
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    

    You also need a placeholder index.html in the document root directory (which will not be used at all, it only needs to exist), but /index.html will be redirected according to Spring MVC controller resolution.

    Best regards,

    Robert

    #252875

    amir55
    Participant

    thanks Robert but I could not get it to work. Look at my simple program quotes
    I sent u a private message to send u my zipped program

    hi all

    // MyServlet.java has this line
    ApplicationContext ac = new FileSystemXmlApplicationContext(“src/applicationContext.xml”);

    // web.xml I have jsut regester to tthe servlet

    <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>tests.MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>

    //applicationContext.xm
    is blank

    so the question is what to add from code to avoid many errors

    many thanks to al

    #252876

    Robert Varga
    Participant

    Hi Amir,

    the problem is that you want to use your own servlet which tries to load the context file from the src directory as a filesystem resource.

    I recommend that you should use Spring’s own servlet and ContextListener, which provides a great deal of other features.

    However, if you want to use your own servlet, then you don’t need the ContextListener, but you need at least the following two corrections (and the third is useful, too):

    1. Instead of FileSystemXMLApplicationContext, use a ClassPathXMLApplicationContext.
    2. Drop the src/ from the beginning of the constructor parameter. The correct value is simply “applicationContext.xml”, if you have it originally in the source directory.
    3. Don’t forget to close the application context in the destroy method of the servlet.

    However, if you want to choose this approach, then you should be aware of the following:
    – you should have this single servlet
    – it should be stateless except for the context
    – you should have only one instances of this servlet (specifiable in the web.xml)
    – you might have integration problems with other parts of the Spring infrastructure or with examples which relies on the application context maintained in a particular place in the servlet context AND that they expect the application context to be of the type org.springframework.web.context.WebApplicationContext

    Best regards,

    Robert

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: how spring links to servlet?

You must be logged in to post in the forum log in