2

I want my SampleServlet to be called first whenever my java web application is accessed in this manner :

http://server:8080/appname/

Is there any way to implement this?

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96

5 Answers5

6

Use a Servlet filter to call your Servlet.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Chry Cheng
  • 3,378
  • 5
  • 47
  • 79
5

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.

Community
  • 1
  • 1
Simon Gibbs
  • 4,737
  • 6
  • 50
  • 80
3

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
  • 31,725
  • 15
  • 104
  • 153
1

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
  • 6,120
  • 9
  • 46
  • 60
  • This is invoked once, during the application's life cycle. The poster asked for something that is accessed once per request. – erickson Mar 21 '09 at 21:44
  • 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 Mar 21 '09 at 23:08
  • 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 Mar 22 '09 at 08:14
  • 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. – chesterbr Nov 12 '09 at 16:58
0

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.

Community
  • 1
  • 1