Showing posts with label deployment descriptor. Show all posts
Showing posts with label deployment descriptor. Show all posts

Friday, December 5, 2014

Servlet Mapping

Web XML


In the deployment descriptor (web.xml file), typically contained at WebContent/WEB-INF/web.xml, you can modify the servlet mapping:
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xmlns="http://java.sun.com/xml/ns/javaee"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
      id="WebApp_ID" version="3.0">  

      <display-name>HelloWorldServlet</display-name> 
 
      <servlet>  
           <servlet-name>XYZ</servlet-name>  
           <servlet-class>com.swtk.web.servlets.HelloWorld</servlet-class>  
      </servlet>  

      <servlet-mapping>  
           <servlet-name>XYZ</servlet-name>  
           <url-pattern>/hiddenservlets/helloworld.do</url-pattern>  
      </servlet-mapping>  

      <welcome-file-list>  
           <welcome-file>index.html</welcome-file>  
           <welcome-file>index.htm</welcome-file>  
           <welcome-file>index.jsp</welcome-file>  
           <welcome-file>default.html</welcome-file>  
           <welcome-file>default.htm</welcome-file>  
           <welcome-file>default.jsp</welcome-file>  
      </welcome-file-list>  

 </web-app>  


The first part of the deployment descriptor is the creation of the servlet node:
 <servlet>  

      <!-- this is a variable name that could be anything -->
      <servlet-name>XYZ</servlet-name>  

      <!-- this is a fully qualified mapping to the servlet class -->
      <servlet-class>com.swtk.web.servlets.HelloWorld</servlet-class>  

 </servlet>  

The second part of the descriptor contains the servlet-mapping:
 <servlet-mapping>  
      
      <!-- refer to the variable name in the servlet node -->
      <servlet-name>XYZ</servlet-name>  
    
      <!-- define a mapping -->
      <url-pattern>/hiddenservlets/helloworld.do</url-pattern>  

 </servlet-mapping>  

Multiple url-pattern nodes can be defined in the same servlet-mapping block. The use of the .do extension is a convention from earlier days of web development.


Using Annotations

In Servlets 3.0, you can use annotations directly on the servlet to accomplish the same thing.
 package com.swtk.web.servlets;  

 import java.io.IOException;  
 import java.io.PrintWriter;  
 import javax.servlet.ServletException;  
 import javax.servlet.annotation.WebServlet;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse; 
 
 @WebServlet(
    description = "Simple HelloWorld Project", 
    urlPatterns = { 
       "/HelloWorld", 
       "/hiddenservlets/helloworld.do" 
    }
 )  

 public class HelloWorld extends HttpServlet {  

      private static final long     serialVersionUID     = 1L;  

      public HelloWorld() {  
           super();  
      }  

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
           PrintWriter out = response.getWriter();  
           out.print("Hello, World!");  
           out.close();  
      }  
 }