Sunday 18 October 2015

Jetty: Hello World application

Step 1: Download latest stable release of Jetty from following location.

I am going to use Jetty 9. Extract the downloaded zip file. Following are the directories inside Jetty distribution.

Directory
Description
bin
Contains shell script ‘jetty.sh’ to run Jetty on Unix systems.
demo-base
A Jetty base directory to run a Jetty server with demonstration webapps
etc
Contains Jetty XML configuration files
lib
All the JAR files necessary to run Jetty
logs
Directory for request logs
modules
Directory of module definitions
resources
Directory containing additional resources for classpath, activated via configuration. It contains log4j.properties file.
webapps
Directory containing webapps that run under the default configuration of Jetty

Following are the files under Jetty distribution.
File
Description
license-eplv10-aslv20.html
Licence file for Jetty
notice.html
License information and exceptions
README.TXT
Useful getting started information
start.ini
File containing the arguments that are added to the effective command line (modules, properties and XML configuration files)
start.jar
Jar that invokes Jetty
VERSION.txt
Contains Release information

Set ‘JETTY_HOME’ directory. I set JETTY_HOME like below.
export JETTY_HOME=/Users/harikrishna_gurram/Jetty/jetty-distribution-9.3.3.v20150827


Step 2: Start the Jetty server. Run following command to start Jetty server.

$ cd $JETTY_HOME
$ java -jar start.jar 
2015-10-09 15:57:57.467:INFO::main: Logging initialized @1116ms
2015-10-09 15:57:57.519:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended.  See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html
2015-10-09 15:57:57.666:INFO:oejs.Server:main: jetty-9.3.3.v20150827
2015-10-09 15:57:57.685:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///Users/harikrishna_gurram/study1/Jetty/jetty-distribution-9.3.3.v20150827/webapps/] at interval 1
2015-10-09 15:57:57.711:INFO:oejs.ServerConnector:main: Started ServerConnector@38082d64{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2015-10-09 15:57:57.712:INFO:oejs.Server:main: Started @1361ms

Open browser and hit the url  ‘http://localhost:8080’. You will get a warning message like below.
It is not recommended to run Jetty from the $JETTY_HOME directory, Next step shows you, how to create a Jetty Base.


Step 3: Go to ‘demo-base’ directory and run start.jar fie.

$ cd $JETTY_HOME/demo-base/
$JETTY_HOME/start.jar
2015-10-09 16:16:44.123:INFO::main: Logging initialized @573ms
2015-10-09 16:16:44.390:WARN::main: demo test-realm is deployed. DO NOT USE IN PRODUCTION!
2015-10-09 16:16:44.393:INFO:oejs.Server:main: jetty-9.3.3.v20150827
2015-10-09 16:16:44.412:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///Users/harikrishna_gurram/study1/Jetty/jetty-distribution-9.3.3.v20150827/demo-base/webapps/] at interval 1
2015-10-09 16:16:44.683:WARN::main: async-rest webapp is deployed. DO NOT USE IN PRODUCTION!
2015-10-09 16:16:44.802:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@7cd62f43{/async-rest,[file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-async-rest.war-_async-rest-any-3675104970200210444.dir/webapp/, jar:file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-async-rest.war-_async-rest-any-3675104970200210444.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.3.3.v20150827.jar!/META-INF/resources],AVAILABLE}{/async-rest.war}
2015-10-09 16:16:44.877:WARN::main: test-spec webapp is deployed. DO NOT USE IN PRODUCTION!
2015-10-09 16:16:44.947:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@221af3c0{/test-spec,[file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-test-spec.war-_test-spec-any-798426417351721271.dir/webapp/, jar:file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-test-spec.war-_test-spec-any-798426417351721271.dir/webapp/WEB-INF/lib/test-web-fragment-9.3.3.v20150827.jar!/META-INF/resources],AVAILABLE}{/test-spec.war}
2015-10-09 16:16:45.020:WARN::main: test-jndi webapp is deployed. DO NOT USE IN PRODUCTION!
2015-10-09 16:16:45.070:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@a4102b8{/test-jndi,file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-test-jndi.war-_test-jndi-any-3293354661349889117.dir/webapp/,AVAILABLE}{/test-jndi.war}
2015-10-09 16:16:45.123:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@6dbb137d{/,file:///Users/harikrishna_gurram/study1/Jetty/jetty-distribution-9.3.3.v20150827/demo-base/webapps/ROOT/,AVAILABLE}{/ROOT}
2015-10-09 16:16:45.283:WARN::main: test webapp is deployed. DO NOT USE IN PRODUCTION!
2015-10-09 16:16:45.560:INFO:oejsh.ManagedAttributeListener:main: update PushFilter null->org.eclipse.jetty.servlets.PushCacheFilter@1349883 on o.e.j.w.WebAppContext@31368b99{/test,file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-test.war-_test-any-6626470769370355711.dir/webapp/,STARTING}{/test.war}
2015-10-09 16:16:45.561:INFO:oejsh.ManagedAttributeListener:main: update QoSFilter null->org.eclipse.jetty.servlets.QoSFilter@3ba987b8 on o.e.j.w.WebAppContext@31368b99{/test,file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-test.war-_test-any-6626470769370355711.dir/webapp/,STARTING}{/test.war}
2015-10-09 16:16:45.583:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@31368b99{/test,file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-test.war-_test-any-6626470769370355711.dir/webapp/,AVAILABLE}{/test.war}
2015-10-09 16:16:45.618:WARN::main: test-jaas webapp is deployed. DO NOT USE IN PRODUCTION!
2015-10-09 16:16:45.637:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@303cf2ba{/test-jaas,file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-test-jaas.war-_test-jaas-any-8669755857195911896.dir/webapp/,AVAILABLE}{/test-jaas.war}
2015-10-09 16:16:45.639:INFO:oejsh.ContextHandler:main: Started o.e.j.s.h.MovedContextHandler@7a5ceedd{/oldContextPath,null,AVAILABLE}
2015-10-09 16:16:45.767:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@4201c465{/proxy,file:///private/var/folders/1x/9q4yr4k15csf564llfcz3c1h7n6wlv/T/jetty-0.0.0.0-8080-xref-proxy.war-_xref-proxy-any-4279251769580499072.dir/webapp/,AVAILABLE}{/xref-proxy.war}
2015-10-09 16:16:45.780:INFO:oejs.ServerConnector:main: Started ServerConnector@7b50df34{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2015-10-09 16:16:45.785:INFO:oejus.SslContextFactory:main: x509=X509@19b843ba(jetty,h=[jetty.eclipse.org],w=[]) for SslContextFactory@64ec96c6(file:///Users/harikrishna_gurram/study1/Jetty/jetty-distribution-9.3.3.v20150827/demo-base/etc/keystore,file:///Users/harikrishna_gurram/study1/Jetty/jetty-distribution-9.3.3.v20150827/demo-base/etc/keystore)
2015-10-09 16:16:45.785:INFO:oejus.SslContextFactory:main: x509=X509@77659b30(mykey,h=[],w=[]) for SslContextFactory@64ec96c6(file:///Users/harikrishna_gurram/study1/Jetty/jetty-distribution-9.3.3.v20150827/demo-base/etc/keystore,file:///Users/harikrishna_gurram/study1/Jetty/jetty-distribution-9.3.3.v20150827/demo-base/etc/keystore)
2015-10-09 16:16:45.792:INFO:oejs.ServerConnector:main: Started ServerConnector@6b0d80ed{SSL,[ssl, http/1.1]}{0.0.0.0:8443}
2015-10-09 16:16:45.792:INFO:oejs.Server:main: Started @2242ms


Open browser and hit the url ‘http://localhost:8080/’. You will get following screen.
Step 4: Create new dynamic web project ‘hello_world‘ in Eclipse. Complete project structure looks like below.
Create ‘index.jsp’ file. Right click on the project -> New -> JSP File.

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <h1>Hello World</h1>
</body>
</html>


Export hello_world application into a war file.

Right click on project -> Export -> WAR file.

Copy the war file to ‘$JETTY_HOME/demo-base/webapps’, and hit the url ‘http://localhost:8080/hello_world/’. You will get following screen.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment