knitr::opts_chunk$set(echo = FALSE, results = 'asis')
r6obj_docstat <- rmddochelper::R6ClassDocuStatus$new() r6obj_docstat$setProject(psProject = "JEE7FEWADevPracSummary") # r6obj_docstat$setVersion(psVersion = "0.0.901") # r6obj_docstat$setStatus(psStatus = "Initialisation") # r6obj_docstat$setVersion(psVersion = "0.0.902") # r6obj_docstat$setStatus(psStatus = "Practicals Lesson 4") # r6obj_docstat$setVersion(psVersion = "0.0.903") # r6obj_docstat$setStatus(psStatus = "Practicals Lesson 5 - Developing Servlets") #r6obj_docstat$setVersion(psVersion = "0.0.904") #r6obj_docstat$setStatus(psStatus = "Practicals Lesson 6 - Views with JSP") # r6obj_docstat$setVersion(psVersion = "0.0.905") # r6obj_docstat$setStatus(psStatus = "Practicals Lesson 7 - RESTful Web Services") r6obj_docstat$setVersion(psVersion = "0.0.906") r6obj_docstat$setStatus(psStatus = "P 8 - RESTful Client") r6obj_docstat$setDate(psDate = "2016-06-28") r6obj_docstat$include_doc_stat(psTitle = "## Document Status")
r6ob_abbrtable <- rmddochelper::R6ClassTableAbbrev$new() r6ob_abbrtable$include_abbr_table(psAbbrTitle = "## Abbreviations")
This document contains a summary of the practicals offered during the course r r6ob_abbrtable$add_abbrev(psAbbrev = "JEE7FEWADev", psMeaning = "Java Enterprise Edition 7 Front End Web Application Development")
. The goal of this summary is to quickly be able to familiarize with the material presented in the different chapters in order to get a better overview over the topic of webapplication development and a deaper understanding on the mechanisms behind the whole area.
The practicals are based on certain technologies one of which is r r6ob_abbrtable$add_abbrev(psAbbrev = "NB", psMeaning = "Netbeans")
An instance of java.util.Logger
is added to an existing program.
Instances of Logger objects are created for each class in which logging should be done. It is important to declare the Logger instance as public static final
. Inside the code where events should be logged, the method log()
of the Logger object is called. This method accepts two arguments
Configuration of the logger is taken by default form a property file in <path-to-jdk>/jre/lib
. The configuration properties file and the path to that file can be adapted using the Project Properties
in NB.
rqudocuhelper::insertOdgAsPdf(psOdgFileStem = "NbProjectProperties")
Basic steps of creating a web application and component types are explored
Java web application is created which contains a Servlet, a r r6ob_abbrtable$add_abbrev(psAbbrev = "JSP", psMeaning = "Java Server Page")
and a r r6ob_abbrtable$add_abbrev(psAbbrev = "JAX-RS", psMeaning = "Java API for RESTful Web Services")
web service.
A JSP is created using the file creation and templating functionality in NB. Additional information can be inserted using r r6ob_abbrtable$add_abbrev(psAbbrev = "JSTL", psMeaning = "Java Standard Tagging Library")
together with library methods from java.util
or other Java components. The result of the JSP can be shown by right-clicking on the JSP-file and by clicking on Run
.
rqudocuhelper::insertOdgAsPdf(psOdgFileStem = "NewMyJsp", pnPaperWidthScale = 0.5)
A Servlet is created in NB similarly using the file creation and temlating functionality. But from a technical point of view, it is something completely different from a JSP. While JSPs are enriched HTML-pages which are automatically converted into Servlets on the application server, Servlets are full-fledged java programs which run on the application server. The author or the programmer has to write everything by him- or herself.
A RESTful service is created by adding special annotations to a r r6ob_abbrtable$add_abbrev(psAbbrev = "POJO", psMeaning = "Plain Old Java Object")
. The minimal set of annotations consists of
@Path("<name_of_path>")
: Annotation of the POJO denoting the path of the resource@GET
: Annotation of the method that should be executed when an HTTP-GET request is issued by the client@PUT
: same as above but for HTTP-PutBesides the annotation, the JAX-RS webservice also needs a deployment descriptor which is a file called web.xml
in xml-format that maps the resource to the servlet. At this early stage, we need a browser plugin such as RestClient
for Firefox to test the RS-functionality.
A r r6ob_abbrtable$add_abbrev(psAbbrev = "CSS", psMeaning = "Cascading Style Sheet")
is added to the WebApp
project and is used to change the layout of the result returned from the servlet MyServlet
. The CSS
is included into the servlet via the insertion of an html Link into the result returned form the servlet.
A new CSS-file can easily be created using the creation functionality provided by NB.
rmddochelper::insertOdgAsPdf(psOdgFileStem = "NewCss", pnPaperWidthScale = 0.5)
Once the Webpage which is served from the modified Servlet is shown, the so-called Web Inspector
can be used to show the structure of the page. The Web Inspector
consists of a Browser-specific tool that is used to display the different elements of the Webpage. Since these tools are Browser-specific, they must be activated for each browser in a different way. In most cases there is a Browser-Menu for Tools
. In Firefox, one can activate it as shown below.
rmddochelper::insertOdgAsPdf(psOdgFileStem = "WebInspector", pnPaperWidthScale = 0.5)
One very useful feature of the Web Inspector is the possibility to change existing css-settings graphically in the browser. The changes can then be included in the css-file on the server.
\pagebreak
In this lesson practicals servlets which act as controllers will be created
Here, we start to create the WebMediaManager
application which is the project for the rest of the course. The controller class is called FileMediaManager
which implements the interface MediaManager
. This class is responsible for managing the single media items. Those items are represented by simple POJOs which are specified in class MediaItem
. The class MediaGroup
organizes media items into logical groups. The method listMediaItems()
of class FileMediaManager
returns a list of MediaGroups. The class MediaTypeFilenameFilter
is used to filter the items according to their media type.
The POJOs representing the media items and the class FileMediaBean
which represents the state of the files and how they are displayed is copied from the project JavaFxMediaViewer
. Once these classes are copied, a new servlet called MediaController
is created using the creation function in NB as shown below and as described in the previous practical.
rmddochelper::insertOdgAsPdf(psOdgFileStem = "NewServlet", pnPaperWidthScale = 0.5)
In the above created servlet the bean class FileMediaBean
is used to display the filenames of the items in the directory fxmedia
. The list of items is generated from the FileMediaBean
object. From this object a list of MediaGroups
is generated and for each MediaGroup
, the list of items is extracted. In the deployment descriptor a welcome-file is specified such that the MediaController
servlet is called, even if the user only enters the context root. That means, if in NB the WebApplication project is right-clicked and Run
is selected, the browser opens the URL http://localhost:8080/WebMediaManager/
and directly starts the MediaController
-servlet.
A new servlet will be added to upload new media (images and videos) to the application. The extension for the upload functionality consists of two parts.
upload.html
which contains a form that allows to specify the media item to be uploadedUploadServlet
which handles the upload process on the server-side. The upload process is done by creating a new MediaItem POJO on the server using the MediaManager's createMediaItem()
-method. This method requires an instance of a media item and an input stream. This input stream is taken from the collection of parts that was created from the request. The upload of a media item is done using method createMediaItem()
of the MediaManager
class. That means, the upload servlet requires not only an instance of FileMediaBean
but also an instance of MediaManager
.
The view pages for the project WebMediaManager
are generated using the Bootstrap CSS look and feel.
Following good practices in servlet based MVC applications, the view should be separated from the controller servlet. Hence the part of the MediaController
servlet that produced the HTML output of the list of media items is moved into a JSP-based view page.
To be able to pass the view to a JSP, we have to forward the FileMediaBean
to the JSP. This is done with a RequestDispatcher
object which is created from the request
object via the getRequestDispatcher
method which takes the path to the JSP as input argument. The FileMediaBean
object must be set as attribute of the request
object. Both the request
and the response
objects are forwarded by the dispatcher
object. The following code is used to process requests in the MediaController
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // get the real path for directory "fxmedia" using the context ServletContext context = getServletContext(); String realPath = context.getRealPath("fxmedia"); // create FileMediaModel using an instance of FileMediaBean FileMediaBean fmm = new FileMediaBean(realPath); fmm.loadData(); // forward request via dispatcher to jsp request.setAttribute("fileBean", fmm); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/manager.jsp"); dispatcher.forward(request, response); }
From the JSP the FileMediaBean
is accessed with a jsp:useBean
declaration shown below
<jsp:useBean id="fileBean" type="com.example.bean.FileMediaBean" scope="request" />
From that declaration on, the components of the FileMediaBean
can be accessed through the reference fileBean
.
View Pages of the Upload servlet are also moved out of the servlet into a JSP-based view page. The process is similar to what was done under P 6-1. Important is that the process of the upload itself does not change, but only what is displayed after the upload is now served through a JSP and no longer directly from the servlet.
The MediaController
servlet is extended by a switch statement on an action parameter. By default the action is set to a value of "manager" which cases the controller to show the old behavior. That means the loadData()
method of the FileMediaBean
object is executed and forwarded to the manager.jsp
. The second action is called when a user clicks on a link of a media item. Then the value of the action is set to item
and that causes to open the item, i.e. images are opened in a large format and videos are played using the appropriate player.
The settings view page consists of two parts
settings.jsp
which presents the page that allows to select the options.settings.jsp
. The second action is set when settings are changed in settings.jsp
. This then sets the qualifier object and loads the items with the new settings.This practical implements a set of RESTful Web Services to respond to GET requests to show the collection of media items or to show a single item. Items can be removed from the collection via the DELETE request. New items can be added using POST or PUT.
The RESTful web service is implemented in class MediaResource
. In that class, annotations of the resource path, the context and HTTP-method GET
result in the list of all media items returned when sending a GET to the defined resource path.
Additional resources in the form of annotated methods are added to get or delete single elements
New items are added using the PUT method of HTTP. This is again implemented via annotated methods in our MediaResource
class.
Simple Java SE client application to test the RESTful resources.
The client is a simple Java SE application. It is created using the NB-Menu-items: File > New Project > Java > Java Application
rmddochelper::insertOdgAsPdf(psOdgFileStem = "NewJavaSeApp", pnPaperWidthScale = 0.5)
This application requires additional libraries. This is done by first defining a new library in the NB-library manager via Tools > Libraries and then adding the newly defined library to the project libraries.
rmddochelper::insertOdgAsPdf(psOdgFileStem = "NewLibraries", pnPaperWidthScale = 0.5)
Adding the new library to the project makes the content available inside of that project.
rmddochelper::insertOdgAsPdf(psOdgFileStem = "AddLibraryToProject", pnPaperWidthScale = 0.5)
Reading and deleting works by passing the respective HTTP method calls to the server.
r6ob_abbrtable$writeToTsvFile()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.