Jboss EAR Deployement Servlet JSP Application

Jboss EAR Deployement Servlet JSP Application

In this demo we will create Servlet JSP application and create EAR file using Ant build file, finally deploy on JBoss server. Please follow steps below:

  • Create dynamic web project name: EarDeploymentJbossServlet
  • Final project structure:

Jboss EAR Deployement Servlet JSP Application

  • web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>EarDeploymentJBossServer</display-name>

	<servlet>
		<servlet-name>HelloWorld</servlet-name>
		<servlet-class>com.javahonk.HelloWorld</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/servlet/HelloWorld</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>
  • index.jsp:
<%@page language="java" %>
<html>
<head>
<title>Jboss EAR Deployment Tutorial</title>
</head>
<body >
<p align="center"><font size="2" color="red">Jboss EAR Deployement with Servlet and JSP </font></p>
<p align="center"><font color="#000080" size="4"><a href="servlet/HelloWorld">Click here to</a> execute Servlet.</font></p>
</body>

</html>
  • application.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<application>
	<display-name>Jboss Ear Deployment</display-name>
	<module>
		<web>
			<web-uri>EarDeploymentJbossServlet.war</web-uri>
			<context-root>/EarDeploymentJbossServlet</context-root>
		</web>
	</module>
</application>
  • HelloWorld.java:
package com.javahonk;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request, HttpServletResponse response) throws  IOException, ServletException{
		  response.setContentType("text/html");
		  PrintWriter out = response.getWriter();
		  out.println("<html>");
		  out.println("<head>");
		  out.println("<title>Hello World Servlet!</title>");
		  out.println("</head>");
		  out.println("<body>");
		  out.println("<p align=\"center\"><font size=\"5\" color=\"#000080\">Test Successful!!!</font></p>");
		  out.println("<p align=\"center\"><a href=\"javascript:history.back()\">Go back to Home</a></p>");
		  out.println("</body>");
		  out.println("</html>");
		   }

}
  • To create EAR file to deploy on JBoss server use this build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Jboss 5.0 Ear Deployment with Servlet and JSP" default="all" basedir=".">

	<property name="dirs.base" value="${basedir}" />
	<property name="src" value="${dirs.base}/src" />
	<property name="web" value="${dirs.base}/WebContent" />
	<property name="classdir" value="${web}/WEB-INF/classes" />
	<property name="deploymentdescription" value="${dirs.base}/build/deploymentdescriptors" />
	<property name="dir.deploy.folder" value="C:\JavaHonk\jboss-6.1.0.Final\server\default\deploy" />

	<property name="warFile" value="EarDeploymentJbossServlet.war" />
	<property name="earFile" value="EarDeploymentJbossServlet.ear" />

	<property name="earDir" value="${dirs.base}/build/ear" />
	<property name="warDir" value="${dirs.base}/build/war" />

	<target name="clean" description="Remove all generated files.">
		<delete dir="${warDir}/WEB-INF" />
		<delete dir="${warDir}/WEB-INF/classes" />
		<delete file="${earDir}/${warFile}" />
		<delete file="${dirs.base}/${earFile}" />
		<echo message="Cleaned" />
	</target>

	<target name="clean_folder" description="Remove all generated files.">
		<delete dir="${warDir}/WEB-INF" />
		<delete dir="${warDir}/WEB-INF/classes" />
		<delete file="${earDir}/${warFile}" />
		<delete file="${dirs.base}/${earFile}" />
		<echo message="Cleaned" />
	</target>

	<!-- Main target -->
	<target name="all" depends="clean,buildWar,buildEar,deploy,clean_folder" />

	<!-- Create the War File -->
	<target name="buildWar">
		<fileset dir="${classdir}" includes="**/*.class" />
		<fileset dir="${web}" includes="web.xml" />
		<fileset dir="${web}" includes="**/*.*" />
		<!-- Create war file and place in ear directory -->
		<jar jarfile="${earDir}/${warFile}" basedir="${web}" />
	</target>

	<!-- Create the Ear File -->
	<target name="buildEar">
		<fileset dir="${deploymentdescription}" includes="application.xml" />
		<!-- Create ear file and place in ear directory -->
		<jar jarfile="${dirs.base}/${earFile}" basedir="${earDir}" />
	</target>

	<target name="deploy">
		<copy todir="${dir.deploy.folder}" overwrite="yes">
			<fileset file="EarDeploymentJbossServlet.ear" />
		</copy>
		<echo message="deployed" />
	</target>

</project>
  • To create EAR file : Right click build.xml –> Run As –> Ant Build…

Jboss EAR Deployement Servlet JSP Application

  • This will create EAR file in “C:\JavaHonk\jboss-6.1.0.Final\server\default\deploy” folder. If your JBoss installation location is on different location then copy EAR file here and paste to JBoss_Home\\server\default\deploy folder and start JBoss server. Now open your browser and type below URL you will page as below:

Jboss EAR Deployement Servlet JSP Application

  • For test you click here link to send request to servlet and you will see response as below:

Jboss EAR Deployement Servlet JSP Application

That’s it for Jboss EAR Deployement Servlet JSP Application

For more information JBoss and EAR deployment please read this tutorial

download  Download Project: EarDeploymentJBossServer

Leave a Reply

Your email address will not be published. Required fields are marked *