SE452: Configuring Web Apps (more web.xml) [22/24] Previous pageContentsNext page

The servlet and a servlet-mapping element are important sections in the deployment descriptor (web.xml).

servlet element: the servlet definition

servlet-mapping element: servlet mapping

An example webapp for a simple servlet:


        
<web-app>
  <servlet>
   <servlet-name>hello</servlet-name>
   <servlet-class>HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>hello</servlet-name>
   <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

    
        

A servlet can be invoked with the URL specified in the servlet mapping:

http://localhost:8080 protocol, host, and port

/se452,/myapps the context prefix

/hello the servlet url

Sometimes we want to initialize a servlet when it is first loaded by the servlet engine.

Initialization parameters are usually handled in the init() method.


        void init()
        

Override this one to initialize a servlet


        void init( ServletConfig config)
        

Do not override this one, if you do override, you must call super.init(ServletConfig config) You can access the ServletConfig's methods through the servlet itself, since the interface is implemented by GenericServlet

Previous pageContentsNext page