# Web technologies/2014-2015/Laboratory 7
## Web Servers

A web server can be:
* a computer that is responsible for accepting HTTP requests from clients, which are known as Web browsers, and serving them HTTP responses along with optional data contents, which usually are Web pages such as HTML documents and linked objects (images, etc.);
* A computer program that provides the functionality described in the first sense of the term. For example: Apache HTTP Server, Apache Tomcat Server, Internet Information Services Server (ISS), Jetty, etc.
Basically, all web serves share the following capabilities:
* HTTP – usually, requests of this type are accepted by web servers. The response is usually a HTML document
* HTTPS
* Logging – usually information about requests and responses are logged into files, that can be later queried by internet administrators.
* Configurability
* Authentication
* Dynamic content handling – CGI, SCGI, Perl, Python, JSP, PHP, ASP, ASP.NET etc.
* Content compression
* Virtual Hosts
* Large file support
* Module support
Links:
* Apache Tomcat Server
* Apache HTTP Server
* Jetty Server

## Java Servlets

Servlets are Java technology's answer to CGI programming. They are programs that run on a Web server and build Web pages. Their specification first appeared in June 1997. The current version is 4.0.
Creating a servlet is simple if you know Java, and have a Java compiler For those not used with servlets, but used in executing stand alone Java applications, the way you execute a servlet, it may be strange at first.
To test a servlet you need a web container. The Apache Tomcat Server will do the job.

### Creating and deploying a Java Servlet

* create a class that extends HttpServlet
* override the doGet(...) and doPost(...) methods. The first one handles content submitted using a GET method, and the second one a POST method. Usually they both call the same method for processing and handling data. They have 2 parameters:
** HttpServletRequest: has methods that let you find out about incoming information such as FORM data, HTTP request headers, etc.
** HttpServletResponse: has methods that lets you specify the HTTP response line (200, 404, etc.), response headers (Content-Type, Set-Cookie, etc.), and, most importantly, lets you obtain a PrintWriter used to send output back to the client.
* enter the program code – A simple Servlet example is shown bellow:
* deploy the servlet: For each servlet you need to have the following directory structure (Supose tomcat is installed in $USER_HOME):
** a public directory ($USER_HOME/webapps/SomeServlet/). containing html, jsp files and other public resources. This is the root directory of the application
** a $USER_HOME/webapps/SomeServlet/WEB-INF/web.xml file which represents the deployment descriptor. A short example of a deployment descriptor is as follows:
** a $USER_HOME/webapps/SomeServlet/WEB-INF/classes/ directory. This will contain the Java classes (including the servlet classes) you use in your web application
** a $USER_HOME/webapps/SomeServlet/WEB-INF/lib/ directory. This will contain additional libraries used in the web application
NOTE: Java NetBeans comes bundled with an Apache Tomcat Server. You can directly create/deploy your application from it without requiring the following steps. Eclipse also has the Web tool platform (WTP) which you can use. To install it please consult this tutorial. After following the above mentioned steps the servlet can then be called in a browser by typing http://localhost:8080/SomeServlet/MyServlet.
You will have to have Java EE version installed. You can find it here. Or you can add to you project classpath the servlet 3.0 jar file found here.
If you have an Apache Tomcat installation you can find the Jar file at <TOMCAT_HOME>/lib/servlet-api.jar.

### Examples of servlet usages
#### Retrieving the request headers

Other useful methods:
* the getAuthType and getRemoteUser methods, which break the Authorisation header into its component pieces
* the getDateHeader and getIntHeader methods, which read the specified header and then convert them to Date and int values, respectively

#### Handling cookies

For cookies we have the getCookies method, which returns the contents of the Cookie header, parsed and stored in an array of Cookie objects:

#### Handling sessions

There are cases in which we need to store some persistent information regarding user input data. One example is an online shopping store where we need to keep track of what the user has added to its shopping cart. Several ways of achieving this exist:
* Cookies – it has a disadvantage of not working on browser that either do not support or have cookies disabled
* URL rewriting – basically means that every URL has to have some extra data appended at its end, data which uniquely identifies its session
* Hidden form fields – we could have a hidden input type in the form which stores the session identifier
* Using Servlets – Java servlets handles sessions pretty straight forwarding, by introducing a HttpSession class for managing the session data for current request. It also has the advantage of keeping data on server side, making it client safe.
We will focus in what follows on the last choice:
The HttpSession class offers us all the methods required for handling sessions:
* getValue("key") – returns the value for the given key. Deprecated since version 2.2
* getAttribute("key") – to be used instead of the getValue
* putValue("key", value) – stores a session key with a given value. Deprecated since version 2.2. (It was used in association with getValue)
* setAttribute("key", value) - stores a session key with a given value. It is used together with getAttribute
* getValueNames - all the attribute names in a String array. Deprecated since version 2.2
* getAttributeNames – all the attribute names in an Enumeration
* getSession(true) – looks up the HttpObject currently associated with the request
* getId – returns the unique identifier generated for each session
* isNew – returns true if the client has never seen the session. This usually happens because it was just created rather than being referenced by an incoming client request. It returns false for already existing sessions.
* getCreationTime – returns the time in milliseconds since the epoch, at which the session was made.
* getLastAccessedTime - returns the time, in milliseconds since the epoch, at which the session was last sent from the client
* getMaxInactiveInterval – returns the amount of time in seconds, that the session should go without access before being automatically invalidated. A negative value indicates that the session should never timeout
The following fragments of code show us how we can get and set session variables:

#### Form data processing

Alternatively we could use the getParameterNames method to get all the parameter names. The method belongs to the HttpServletRequest class:
Links:
* Servlet examples
* Server Side Languages Comparison
* Speed comparison amongst Server Side Languages

### JDBC

Java DataBase Connection (JDBC) is a standard API, through which Java developers can access databases, regardless of the driver and database product.
NOTE: see and try also HSQLDB for an easy to run and test SQL database.
In order for JDBC to be used we need:
* at least JDK 1.1
* a JDBC driver (see https://www.geeksforgeeks.org/jdbc-drivers/. Here is a list). If you do not want to download and install any JDBC drivers, and if you use JDK 1.1, you should know that Sun provides for this JDK version a driver that is ODBC compatible. You should be able, by using it, to connect to any ODBC compliant database. Nevertheless you will need to create an ODBC data-source for your database, for your applications to access it.
There are several steps needed in order to access a database through JDBC:
* Load database driver (2 options exist):
** load the default Sun jdbc-odbc driver
* load the connector/J driver for MySQL databases. You can download it for free from http://www.mysql.com/products/connector/j/. You will need to add it to your $CLASSPATH in order to use it.
* Connect to the database driver:

### Execute queries:

Executing updates: can be accomplished in 3 different ways:
* using Statement: used for sending simple SQL instructions, without parameters
* using PreparedStatement: used for sending pre-compiled parameterised SQL
* statements using CallableStatement: allows the use of stored procedures located on the DBMS server
* executing queries:
NOTE: Queries can also be executed by the means of the 3 methods described above.
Links:
* JDBC tutorial

## Exercises

Start a simple Java servlet (use the examples from the lab) and deployed the on a web container. Steps for setting up Servlets in Eclipse IDE can be found here.
I recommend using Jetty
You can find useful information here and here.
For JDBC please consult these links: link1, link2. For more information about installing JDBC in eclipse please consult these links: link1
Complete Example of using sqlite in Java can be found here.

## Homework

* Create 2 small servlets and 2 HTML pages with the following functionality:
** the first HTML page will contain a form having the following fields:
*** user first name (text)
*** user last name (text)
*** user username (text)
*** user password (password - at least 6 characters with validation)
*** user retyped password (password)
*** user address (text)
*** user email (text – with js validation)
*** user interests (textarea)
*** clear form button and a submit button
*** Only the first part of the Homework is mandatory. The second part will be added to the end of the JSP laboratory from next week.
** the second HTML page will contain a form for querying a database for users as follows:
*** a search textbox where the searched user (complete or partial) will be entered
*** 2 radio buttons (one called Exact search and the other Containing) for search options.
*** a submit button
** the first servlet will store the user information from the first HTML page both inside a session and inside a database. The database structure will be designed so that it can hold the submitted information.
** the second servlet will return the result of the query performed by the second HTML page.
HINT: see for a way of creating a database from Netbeans
Gabriel Iuhasz, 2019-10-29, iuhasz.gabriel@e-uvt.ro