Here you can find the example Java Servlets from my distributed computing class. The project is built using Maven.
This small servlet accepts an incoming HTTP request and sends back a Hello World
message in text/plain
format.
If your application is running in GlassFish, you can access this servlet as http://localhost:8080/myServlets/HelloWorld.
This servlet is similar to our MinHTTPServer.java example in the Java sockets example set. It accepts an incoming HTTP request and dynamically creates a text/html
formatted response (i.e., a web page). This web page contains all the request's data, such as the client address, all request headers, cookies, and dynamic query parameters (such as form data).
If your application is running in GlassFish, you can access this servlet as http://localhost:8080/myServlets/RequestData.
This servlet shows how sessions can be handled with Java Servlets. It accepts an incoming connection and dynamically creates a text/html
formatted response (i.e., a web page). This web page contains all the information of (and stored in) the current session. The first time you access this servlet, there is no current session, so it is created. This session initially has no information stored in it in form of attributes. However, this servlet adds some attributes to the session. The next time you access this servlet, these attributes will be printed. Hence, sessions are a way to preserve information between HTTP requests to a servlet.
If your application is running in GlassFish, you can access this servlet as http://localhost:8080/myServlets/SessionData.
If you import this project in Eclipse, it may first show you a lot of errors. (I recommend using Eclipse Mars or later.) This is a Maven project, so you should "update" it first in Eclipse by
- Make sure that you can see the
package view
on the left-hand side of the Eclipse window. - Right-click on the project (
JavaServlets
) in thepackage view
. - In the opening pop-up menu, left-click on
Maven
. - In the opening sub-menu, left-click on
Update Project...
. - In the opening window...
- Make sure the project (
JavaServlets
) is selected. - Make sure that
Update project configuration from pom.xml
is selected. - You can also select
Clean projects
. - Click
OK
. - Now the structure of the project in the
package view
should slightly change, the project will be re-compiled, and the errors should disappear.
Now you can actually build the project, i.e., generate a war
file that you can deploy in a servlet container. For building this war
, take the following steps:
- Make sure that you can see the
package view
on the left-hand side of the Eclipse window. - Right-click on the project (
JavaServlets
) in thepackage view
. - In the opening pop-up menu, choose
Run As
. - In the opening sub-menu choose
Run Configurations...
. - In the opening window, choose
Maven Build
- In the new window
Run Configurations
/Create, manage, and run configurations
, chooseMaven Build
in the small white pane on the left side. - Click
New launch configuration
(the first symbol from the left on top of the small white pane). - Write a useful name for this configuration in the
Name
field. You can use this configuration again later. - In the tab
Main
enter theBase directory
of the project, this is the folder calledjavaServlets
containing the Eclipse/Maven project. - Under
Goals
, enterclean compile war:war
. This will build awar
archive. - Click
Apply
- Click
Run
- The build will start, you will see its status output in the console window.
- The folder
target
will contain a filemyServlets.war
after the build. This is the deployable archive with our application.
Under Linux, you can also simply run make_linux.sh
in this project's folder to build the servlet without Eclipse, given that you have Maven installed.
GlassFish is a reference implementation of a Java EE 7 application server.
The following steps are for Linux, but under Windows it will be pretty much the same.
- Go to the GlassFish website https://glassfish.java.net/.
- Select download.
- Download the Java EE 7 Web Profile, at the time of this writing, this is GlassFish 4.1.1
- Unpack the downloaded archive into a folder of your liking (let's call this folder
F
). - Open a terminal console and go (
cd
) into folderF
. - In the folder
F
(where you unpackaged the archive), go to sub-folderglassfish4/bin/
, i.e., toF/glassfish4/bin/
. - Type
./asadmin start-domain
. (Under Windows, typeasadmin start-domain
without the leading./
.) The server will now start. - Under Windows, a window may pop up asking you for allowing the program internet access permission, which you should OK.
- Open your web browser and visit its configuration page at
http://localhost:4848
.
-
Find the file
myServlets.war
in the foldertarget
of this example project. -
Copy it into the folder
F/glassfish4/glassfish/domains/domain1
. -
In your web browser, visit
http://localhost:4848
. -
Click
Applications
in the menu pane on the right. -
It should list
myServlets
. If you click this menu point, you get a list of the installed servlets. -
You can access the servlets as follows
In the stand-alone JSPs example in the JavaServer Pages example set, I describe how stand-alone JavaServer Pages (JSPs) can be built with Jetty. These are JSPs which do not need to be deployed but where the whole application server is packaged into one jar
archive also including the JSPs. You just need to start this jar
to get the application running. Of course, you can modify this example to serve Java Servlets instead rather easily