Warning
This page is located in archive.

Servlets and JSPs

MVC (Model-View-Controller)

Model-View-Controller is a software architecture that separates the data model, business logic, and UI logic, while providing a loose coupling between these elements. In AppEngine typically Servlet is the controler (provides busines logic), JSP is the view (provides UI logic) and JavaBeans represent the data model.

Request lifecycle

When request is sent to the server (e.g. form is submitted via POST) then it is processed by a servlet (controller). The servlet process the input parameters, calculates the output values, creates, stores or loads needed entities of data model (JavaBeans) and forwards the request with the output values to a JSP (view). JSP creates the HTML (or other type) output based on the output values and thus providing the UI and its logic. JSP can also load or store data entities, but only those that releate to the UI logic.

Note that MVC allows us calling different JSPs where each can generate different UI based on the type of device (e.g. smart phone and PC).

Servlet

Class HttpServlet is ancestor of generic class Servlet and provides methods doGet, doPost, doPut and doDelete for handling the http requests.

In these methods we write code that represents the bussines logic. Do not use any class variables (both static and not static), the same servlet can be called from different threads by different users. Instead use HttpSession to store variables that should last longer than one request. You can obtain HttpSession from request by request.getSession(). To enable HttpSession on AppEngine you need to edit configuration file WEB-INF/appengine-web.xml.

In a file web.xml the servlets are mapped onto url. If our server is running at www.myserver.com and we map servlet MyServlet onto url /dosomething then all requests to the url www.myserver.com/doSomething will be handled by MyServlet.

The web.xml file also contains welcome-file tag where we can specify url to which the user will be redirected if s/he sends request to www.myserver.com.

JSP (Java Server Pages)

JSP is in fact only a different way how to write servlet where is much more convinient to produce HTML or XML output. On the server side the JSP is translated to Servlet and compited to bytecode.

JSPs are stored in the WEB-INF directory together with static content (e.g. images, static HTML files, css files) so their path is determined by subdirectories in the WEB-INF directory.

If you plan to use taglibs (YES YOU ARE) then add the jars that are needed for correct function of taglibs in JSP editor using this tutorial.

If you still have errors in the project after finishing the tutorial you need to change type of java to JDK in Eclipse: Window → Preferences → Java → Installed JREs

You can find more information about JSP in this tutorial. More information about core tags can be found here. Detailed tutorial about taglibs is available here.

JavaBean

JavaBean is Java object that has getVariableName() and setVariableName() methods for each class variable that can be accesed form outside. You need to replace the VariableName in the methods with the real name of the variable. E.g. for class variable “int amout” the methods will be “int getAmout()” and “void setAmount(int value)”.

In following practices we will learn how to store the JavaBeans to database and load them from database.

Example

  1. Download this Eclipse project of AppEngine application. Unzip and import the project into Eclipse (File → Import… → Existing project into workspace) and study it. You need to add libraries for taglibs and allow HttpSession.

Exercise

  1. Create new functionality similar to the one already present (e.g. converter from inches to mm).
    1. Use JSP, HttpServlet and JavaBeans.

Example 2

The previous example with prevention of double form submission and with serializable JavaBeans for working HttpSession on AppEngine can be found here.

courses/a4m39wa2/tutorials/02/start.txt · Last modified: 2014/12/03 13:36 (external edit)