Engineering Full Stack Apps with Java and JavaScript
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).
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.
Eclipse Luna Java EE IDE for Web Developers and Apache Tomcat 8.0.18, using Servlet spec 3.1.