Wednesday May 21, 2008
Embeddable GlassFish in Action - Servlet in a Maven project
Kohsuke
announced the embedability
of GlassFish v3 - this is really cool! Now you can run
GlassFish inside an existing JVM, without the need to start it
externally. The API javadocs are available here.
This blog explains how to host a Servlet using these APIs and write a
simple Maven test to invoke the Servlet - all within the same VM.
The blog creates a Maven project using NetBeans but Maven CLI can be
used as well.
In the NetBeans IDE,
if Maven plugin is not already installed, then install it using
"Tools", "Plugins","Available Plugins".




| mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=org.glassfish.embedded.samples -DartifactId=webtier |
| <repositories> <repository> <id>glassfish-repository</id> <name>Java.net Repository for Glassfish</name> <url>http://download.java.net/maven/glassfish</url> </repository> <repository> <id>download.java.net</id> <name>Java.net Maven Repository</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> |
| <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> |
| <dependency> <groupId>org.glassfish.distributions</groupId> <artifactId>web-all</artifactId> <version>10.0-build-20080430</version> </dependency> <dependency> <groupId>org.glassfish.embedded</groupId> <artifactId>gf-embedded-api</artifactId> <version>1.0-alpha-4</version> </dependency> |

| package
org.glassfish.embedded.samples.webtier; 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; /** * @author Arun Gupta */ public class SimpleServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Wow, I'm embedded!"); } } |




| <?xml
version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <servlet> <servlet-name>SimpleServlet</servlet-name> <servlet-class>org.glassfish.embedded.samples.webtier.SimpleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SimpleServlet</servlet-name> <url-pattern>/SimpleServlet</url-pattern> </servlet-mapping> </web-app> |

|
private final String NAME = "AppTest"; public void testServlet() throws Exception { int port = 9999; GlassFish glassfish = newGlassFish(port); URL url = new URL("http://localhost:" + port + "/" + NAME + "/SimpleServlet"); BufferedReader br = new BufferedReader( new InputStreamReader( url.openConnection().getInputStream())); assertEquals("Wow, I'm embedded!", br.readLine()); glassfish.stop(); } private GlassFish newGlassFish(int port) throws Exception { GlassFish glassfish = new GlassFish(port); ScatteredWar war = new ScatteredWar(NAME, new File("src/main/resources"), new File("src/main/resources/WEB-INF/web.xml"), Collections.singleton(new File("target/classes").toURI().toURL())); glassfish.deploy(war); System.out.println("Ready ..."); return glassfish; } |





Posted by Arun Gupta in web2.0 | Comments[16]
|
|
|
|
|
Today's Page Hits: 1782
Total # blog entries: 1002