Search code examples
jspservletsjakarta-eeservlet-2.5

Web application in other web application


I need to create a web application (servlets/jsp) as a JAR file which can be added in another web application in WEB-INF/lib folder. And it should be made available by making an entry in web.xml file.

Can this be done?


Solution

  • Of course you can do that:

    1. Create Servlet classes / JSP files. Package them all in a .jar file. Don't include web.xml or any other files.

    2. Import the jar in your web application classpath.

    3. Configure Servlet classes and JSP files in your web.xml as below:

    For JSP:

    <servlet>
        <servlet-name>MyJSPFile</servlet-name>
        <jsp-file>/path/to/jsp/MyJSP.jsp</jsp-file>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>MyJSPFile</servlet-name>
        <url-pattern>/MyJSP.jsp</url-pattern>
    </servlet-mapping>
    

    For Servlet:

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>my.servlet.classpath.MyServlet</servlet-class>
    </servlet>
     
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet.do</url-pattern>
    </servlet-mapping>