| Java, Eclipse and Web programming Tutorials |
Version 0.3
Copyright © 2009 Lars Vogel
01.09.2009
| Revision History | ||
|---|---|---|
| Revision 0.3 | 09.04.2009 | Lars Vogel |
| Split of another article to have it separately | ||
| Revision 0.2 | 18.05.2009 | Lars Vogel |
| New overview, improved structure | ||
| Revision 0.3 | 01.09.2009 | Lars Vogel |
| Added info about cloud | ||
Table of Contents
Java has strong support for web development. The following gives a short overview of the available Java technologies.
I start with a very general explanation of web applications and then try to give the broad picture for Java.
After you finished the overview you can create your first Java web application with Servlet and JSP development with Eclipse WTP
If you develop a web application (independent of the programming language your are using) your put your web application on a server (and not your local computer). The web application runs on the server and people can access it there.
Of course it is possible to use your local computer as a server but usually you want to have a fixed server which runs 24 hours and 7 days so that web clients can always reach your server under a pre-defined address. For example http://www.vogella.de/blog contains my blog. My blog is a web application powered by Wordpress. Wordpress is a web application written in the programming language php.
Java web applications are running inside a container on the server. This container runs on the server.
Instead of running your application directly on a dedicated server you could also run it in a cloud environment. This cloud environment provides the necessary server for your application. An example for this is the Google App Engine .
This container provides a runtime environment for Java web applications. The container is for Java web applications what the JVM (Java Virtual Machine) is for local running Java applications. The container itself runs in the JVM.
In general Java distinguish two containers: the web container and the Java EE container. Typical web container in the Java world are Tomcat or Jetty.
A web container supports the execution of Java servlets and JavaServer Pages. A Java EE container supports additional functionality for example distribution of server load.
Most of the modern Java web frameworks are based on servlets and JavaServer Pages. Popular Java web frameworks are JavaServer Faces, Struts, Spring. These web frameworks usually can run in a web container.
Traditionally is has been difficult to start Java web development as a server was required for hosting the Java web container. Google offers a free of charge (for starters) solution based on Java. For details see Google App Engine - Java Development (with Eclipse) - Tutorial
A Java web application is a collection of dynamic resources (such as Servlets, JavaServer Pages, Java classes and jars) and static resources (HTML pages and pictures). Can be deployed as a .war file. The war file which is a zip file which contains the complete content of the corresponding web application.
A servlet is a Java class which answers a HTTP request within a web container. The functionality, the handling and the behaviour of Servlets are defined via the JCP process and there are several official version of Java servlets.
Servlets 2.5 - Latest official spec, part of Java EE 5
Servlets 3.0 - Currenlty beening working on, planned to be part of Java EE 6
JavaServer Pages (JSP) are files which contains HTML and Java code. The web cotainer compiles the JSP into a servlet at the first time of accessing this JSP.
Java Server pages are based on a standard which is developed via the JCP process. The current actual version is 2.1.
Within a web application a session is the identification of the user by the server. The session management API provided by the application server identifies the user across multiple page requests. As the HTTP protocol is stateless all information about the request must be stored in the request itself.
The management of the session is done via the HttpSession Interface.
Table 1. Session Parameters
| getAttribute(), getAttributeNames(), setAttribute(), removeAttribute() | Set, get and remove objects from a user session |
| getId() | Every session created by the server has a unique 'id' associated with it in order to identify this session from other sessions. This method returns the 'id' of this session. |
| getCreationTime() | Returns the date and time this session was created. As a result you get the time this user first accessed your site |
| getLastAccessedTime() | Returns a long value indicating the last time the user accessed any resource on this server |
| isNew() | True, if the session is new |
| invalidate() | Destroys the session, e.g. after logout |
The ServletContext, the HttpSession and the HttpRequest objects are called in the servlet specification as scoped containers. Each of these objects allows to stored and receive data via the methodssetAttribute() and getAttribute().
The lifetime of the data of these scoped containers is defined:
HttpRequest: (Only) for the lifetime of the request
HttpSession: for the lifetime of the session between the user and the system
ServletContext: For the lifetime of the application
Thank you for practicing with this tutorial.
Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.
I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.