Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map servlet programmatically instead of using web.xml or annotations

How can I implement this mapping programmatically without web.xml or annotations? The mission is not to use any framework like spring or something else.

<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>
like image 726
Marta Ginosian Avatar asked Aug 30 '16 04:08

Marta Ginosian


People also ask

How do I map a servlet in web xml?

Configuring and Mapping a Servlet This is done using the <servlet> element. Here you give the servlet a name, and writes the class name of the servlet. Second, you map the servlet to a URL or URL pattern. This is done in the <servlet-mapping> element.

Do We Need web xml in Spring boot?

Spring MVC web applications use the web. xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web.

Why is web xml needed?

web. xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method. For example: the doGet() method for HTTP GET requests.


2 Answers

Since Servlet 3.0 you can use ServletContext#addServlet() for this.

servletContext.addServlet("hello", test.HelloServlet.class);

Depending on what you're developing, there are two hooks where you can run this code.

  1. If you're developing a publicly reusable modular web fragment JAR file such as existing frameworks like JSF and Spring MVC, then use a ServletContainerInitializer.

    public class YourFrameworkInitializer implements ServletContainerInitializer {
    
        @Override
        public void onStartup(Set<Class<?>> c, ServletContext servletContext) throws ServletException {
            servletContext.addServlet("hello", test.HelloServlet.class);
        }
    
    }
    
  2. Or, if you're using it as an internally integrated part of your WAR application, then use a ServletContextListener.

    @WebListener
    public class YourFrameworkInitializer implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            event.getServletContext().addServlet("hello", test.HelloServlet.class);
        }
    
        // ...
    }
    

You only need to make sure that your web.xml is compatible with Servlet 3.0 or newer (and thus not Servlet 2.5 or older), otherwise the servletcontainer will run in fallback modus complying the declared version and you will lose all Servlet 3.0 features.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0"
>
    <!-- Config here -->
</web-app>

See also:

  • ServletContainerInitializer vs ServletContextListener
  • @WebServlet annotation with Tomcat 7
  • Design Patterns web based applications
  • Splitting up shared code and web.xml from WAR project to common JAR project
like image 100
BalusC Avatar answered Jan 03 '23 21:01

BalusC


You can use annotations to achieve this using code.

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
        response.getWriter().println("Hello");
    }
}

You can read about annotations here, here and here

like image 28
anacron Avatar answered Jan 03 '23 19:01

anacron