Article: Java Server Pages

Only available on StudyMode
  • Topic: JavaServer Pages, Java Community Process, Web server
  • Pages : 12 (1783 words )
  • Download(s) : 26
  • Published : February 6, 2013
Open Document
Text Preview
Java Server Pages combined with servlets in action
We want to create a small web application (library), that illustrates the usage of JavaServer Pages combined with Java Servlets. We use the JavaServer Pages to display data (presentation) and the servlets to control and call the business logic of the web application. First we shortly explain the characteristics of these two technologies.

Generals
Author:
Sascha Wolski
http://www.laliluna.de/tutorials.html
Tutorials for Struts, EJB, xdoclet and eclipse.
Date:
September, 23th 2004
Source code:
http://www.laliluna.de/assets/tutorials/java-servlets-jsp-tutorial.zip The sources does not contain the project files. So create a project first and copy the sources to this projects.
PDF Version des Tutorials:
http://www.laliluna.de/assets/tutorials/java-servlets-jsp-tutorial-en.pdf Development Tools
Eclipse 3.x
Optional:
MyEclipse plugin 3.8
(A cheap and quite powerful Extension to Eclipse to develop Web Applications and EJB (J2EE) Applications. I think that there is a test version availalable at MyEclipse.) Application Server
Jboss 3.2.5
You may use Tomcat here if you like.

Java Servlets
Servlets represent java programs that run on a web server. They allow the developer to produce dynamic web sites with java.
A Servlet has the following tasks


It reads and processes data, which a user inputs in a HTML form on a web page.



Other informations can be processed, e.g. what browser or system will be used.



It generate results with the existing data, calls the business logic directly in the servlet or another class, which contains the logic or executes a database query.



The results are formated. If the Browser expects an answer in the HTML format, then the results must be formatted in accordance with the standard. It is possible to return different formats of data with a servlet. (gif, jpeg, doc, etc.).



Suitable answer parameters are set. Befor the servlet return the data to the browser, it sends some parameter. The parameter contains the format, that will returned by the servlet, what time the browser use to cache the site and some more.

Java Server Pages (JSP)
JavaServer Pages (JSP) are text documents, which are similar to HTML Files. But you find also java code in the JSP File. JavaServer Pages allow you to mix regular static HTML with dynamic generated contents of Java code. The java code is inserted in the HTML document on a JSP File, differently to a servlet, where the HTML code is embedded in the java code.

Create a new Web Projekt
Right click on the package explorer and choose New > Project. Choose the wizard Web Project in J2EE.

Set a nice name for your project.

Class with dummy data
In this example we do not use a database binding. We use a class which provide the dummy data and some methods for add, update, delete. Create a new Package de.laliluna.tutorial.library in src of the project. Download the complete project an copy the file SimulatedDB.java from the folder /de/laliluna/tutorial/library in the package.

Create the class book
You can borrow a book in a library, so we need a class that represent the book. Create a package de.laliluna.tutorial.library and a new class New > Class.

The object class book contains the following private properties as variables. •

id



title



author



available

We create for the properties getter- and setter-methods, to access the private properties. Open the class Book and add the properties and getter- and setter-methods. Add constructors to this class.

public class Book {
private
private
private
private

long id = 0;
String title = "";
String author = "";
boolean available = false;

// Contructor
public Book(){}
// Contructor to initial the properties
public Book(long id, String author, String title, boolean available) { this.id = id;
this.author = author;
this.title = title;
this.available = available;
}
public...
tracking img