8

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Marta Ginosian
  • 117
  • 1
  • 8
  • If you don't have a framework, there's no such thing as a "view resolver", since it's the frameworks that provide such a feature. – Andreas Aug 30 '16 at 05:08
  • You could create a servlet that handles the mapping and invokes other servlets. That is what spring does. The servlet is the place where the container interfaces with the application so practically you will need at least one servlet. – BevynQ Aug 30 '16 at 05:08
  • @Marta, I understand you are trying to learn. But, what exactly are you trying to learn? If you are learning Servlets, then you should learn about the servlet-mapping in web.xml and to do the mapping using Annotations. – anacron Aug 30 '16 at 07:00

3 Answers3

9

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
6

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

anacron
  • 6,443
  • 2
  • 26
  • 31
1

If you are using tomcat 7 or above you can done this by annotation

@WebServlet("/hello") 
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
  • Nice thank you, option one which I will try. But as I'm just learning I would really like to go the hard way and do the mapping without annotations. So if there is any way please drop a hint. – Marta Ginosian Aug 30 '16 at 04:58