views:

649

answers:

5

Hi,

I wand my SampleServlet to called first whenever my java web application is accessed in this manner : http://server:8080/appname/

Is there any way to implement this?

Thanks, Veera.

+2  A: 

Not sure what you mean but you need to map your servlet to "/"

<servlet-mapping>
    <servlet-name>SampleServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
cherouvim
+1  A: 

Not sure what is your aim, but web application initialization can be achieved by ServletContextListener:

public class AppListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        // place your code here
    }

    public void contextDestroyed(ServletContextEvent event) {
    }
}

and later in web.xml:

<web-app>
<listener>
    <listener-class>
        package.AppListener
    </listener-class>
</listener>
...
</web-app>
asalamon74
This is invoked once, during the application's life cycle. The poster asked for something that is accessed once per request.
erickson
imho, the question isn't clear that you can vote it down as off-topic. I interpreted the question to mean "when the user turns up" and to mean displaying a servlet generated page rather than running code. Its a bad question, and a helpful answer whether they match is undecipherable.
Simon Gibbs
erickson: Yes, I know it's invoked once and not once per request. The question was not clear, that's why I started my answer as "Not sure what is your aim".
asalamon74
In fact, I understood the question as once-when-the-application-starts (which was what I was looking for), and now I see it has double interpretation. So the answer is valid, IMHO.
chester
+5  A: 

I think a filter would be more appropriate. See http://java.sun.com/products/servlet/Filters.html.

Chry Cheng
+2  A: 

If you want to make a servlet your homepage then this worked for me on http://feelitlive.com/

<welcome-file-list>
 <welcome-file>homepage</welcome-file>
</welcome-file-list>

...

<servlet>
 <description>Shows stuff on the homepage</description>
 <display-name>Homepage Servlet</display-name>
 <servlet-name>HomepageServlet</servlet-name>  
 <servlet-class>com.cantorva.gigcalendar.servlets.HomepageServlet</servlet-class>
</servlet>

...

<servlet-mapping>
 <servlet-name>HomepageServlet</servlet-name>
 <url-pattern>/homepage</url-pattern>
</servlet-mapping>

That means that that users arriving at your application via the URL you specified will be welcomed by your servlet. It also creates an alias for the homepage at "/homepage" but you don't have to use that.

If you want to run some code on start-up then asalamon74's answer looks right.

Simon Gibbs
A: 

If you want to run code on start-up indeed asalamon74's answer should be fine. If you have a legacy situation and you must use a servlet, the parameter load-on-startup can do the trick for you:

<servlet>
    <servlet-name>SampleServlet</servlet-name>
    <display-name>SampleServlet</display-name>
    <description>Sample Servlet</description>
    <servlet-class>...</servlet-class>
    <init-param>...</init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

The load-on-startup tag specifies that the servlet should be loaded automatically when the web application is started; the number value just gives a loading order to those loading on startup. If no value is specified, the servlet will be loaded when the container decides it needs to be loaded - typically on it's first access.

Pietro Polsinelli