[Demo] Deploying Using Web Fragments (web-fragment.xml) Using Eclipse Feature

Web fragments are xml files that will have part of the configurations of a web.xml. There can be many web fragments, and when the application is deployed container will combine all the fragments and will treat it like a single web.xml. Similar to annotations, now developers can write web fragments for their modules and application assemblers would not have to add them to the web.xml file. We can override annotation behavior with a web fragment or web.xml. Container will first process web.xml and then based on any order defined (if any) process web fragments and finally will process annotations for any metadata that is not already defined in web fragment or web.xml. You may also specify the order of invocation of web fragments from within the web fragment.

First create a Dynamic Web Project using eclipse and name it DynaWebProjWithFrag. Remember to select the option for generating web.xml.

 

Now create a new Web Fragment Project using eclipse. Give the name as MyWebFrag. Also select the option to ‘Add Project To a Dynamic Web Project’ and select DynaWebProjWithFrag.

 

In the MyWebFrag fragment project, create a simple servlet called FragServlet with a url pattern "/FragServlet" and only a doGet method that will print “MyWebFrag”.

@WebServlet("/FragServlet")

public class FragServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

 

  protected void doGet(HttpServletRequest request, HttpServletResponse response)

      throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    out.println("FragServlet");

  }

}

 

Now add FragServlet as a welcome-file in the web fragment project’s web-fragment.xml. The web-fragment.xml available under the META-INF project, or you need to create one as below here.

<?xml version="1.0" encoding="UTF-8"?>

<web-fragment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:webfragment="http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"

                xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-fragment_3_1.xsd"

                id="WebFragment_ID" version="3.1">

                <display-name>MyWebFrag</display-name>

                <name>MyWebFrag</name>

                <welcome-file-list>

                                <welcome-file>FragServlet</welcome-file>

                </welcome-file-list>

</web-fragment>

 

Now right click and run the Dynamic Web Project DynaWebProjWithFrag on the server using the URL as http://localhost:8080/DynaWebProjWithFrag/.

This will print FragServlet.

When we run the original dynamic web project without specifying any file, it will look for every file declared as welcome files. All the files from web.xml and web-fragment.xml were merged, with the ones in web.xml coming before the ones in web-fragment.xml. Since none of the files with the names mentioned as welcome files in web.xml was available, it executed the matching one configured in the web-fragment.xml of the fragment project.

Now try creating a new file index.html as below (assuming this entry is there in the web.xml welcome file list).

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Welcome File</title>

</head>

<body>

<h1>Welcome File</h1>

</body>

</html>

 

Now if you again run the Dynamic Web Project DynaWebProjWithFrag on the server using the URL as http://localhost:8080/DynaWebProjWithFrag/.

This will print Welcome File.

 

We will do just one more thing. We will verify the location and structure of the fragment jar inside the dynamic web project, when exported as a standalone deployable jar.

Right click on the project Dynamic Web Project DynaWebProjWithFrag, select Export > War File, provide a Destination and save the war file.

If you explore the war file, then you can see that the web fragment jar (MyWebFrag.jar) is currently inside the Dynamic Web Project DynaWebProjWithFrag in the location WEB-INF\lib\.

 

If you further explore the web fragment jar (MyWebFrag.jar), you can see that the web-fragment.xml is inside META-INF\ folder of the web fragment jar (MyWebFrag.jar).

 

TO-DO

Now since you have explored the structure of the created dynamic web project war and the fragment jar, do this demo without using the eclipse feature for creating the Web Fragment. At the very least, create a standalone fragment project, create jar and then put it inside WEB-INF lib of the dynamic web project and then execute or export it.

 

Development and Execution Environment

Eclipse Luna Java EE IDE for Web Developers and Apache Tomcat 8.0.18, using Servlet spec 3.1.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)