1
JSP [JAVA SERVER PAGES] Introduction • • • The are a number of ways to get data from one computer to another the first was called UNIX-to-UNIX copy • • The next development was the File Transfer Protocol • • Then came HTTP which abstracts content away from files on a disk to arbitrary URLs that can be any type of resource • • After HTTP it came HTML that can specify how data should look through a wide variety of tags, images, links and tables • • The client server technology which is based on request response mechanism made a big change in file transfer technologies • • Today a web server can do a lot things besides sending a file in response to a request like running a program and sending the output of that program to the user • • The world wide web came up with static pages similar to pages in a file which had no interaction with the user • • Then came up dynamic pages which has dynamic content that immediately reacts to the user’s need • • The server generates dynamic content with the help of programs that resides inside the server called common gateway interface (CGI) A sample CGI program in C #include <stdio.h> #include
int main(int argc, char **argv) { time_t now;
2 printf(“\n”); printf(“\n”); time(&now); printf(“The time is now: %s”, ctime(&now)); printf(“\n”); printf(“\n”); exit(0); }; Introduction (contd.) request Client
CGI Server
response • Application servers was introduced to improve the performance of the server • The application server runs as a separate process and stays running between requests • Application servers are extensible allowing programmers to add new features as needed
3
Diagram of an Application Server
CGI 1
2 Server
Client
CGI
4
CGI
CGI
3 Application Server
1- Browser requests HTML 2- Web server sends request to application server 3 – Application generates HTML 4- Server sends HTML back to browser
Servlets • A servlet is a small piece of code that extends a server’s functionality • • A servlet is an object written in Java that is equipped to receive a request and build and send back a response • • Written in java a servlet inherits all of the language’s strength like cross platform so it is possible to develop a servlet under linux and deploy it on NT without needing to change or recompiling • • If a server supports java it can load a new servlet and there is no need to modify the server • • The servlet architecture is designed to eliminate the need to reload the servlet every time a request is made
Servlets and JSP
4 • •
• • • • • •
• •
• •
• •
• • • • • •
The servlet is loaded once as it is needed after that it stays active turning requests into responses as fast as the web server can send them Servlets completely replaced CGI The next step was to building a templating system on top of servlet A servlet reads a file and figures out what to do based on special tags present in the file and does it A Boston based company called Art Technology Group (ATG) came up with an idea to translate the special tags directly into Java and then compile and run the java code The revolutionary concept called page compilation was introduced in the ATG application server named Dynamo Sun was very impressed by the concept of page compilation and they licensed the technology for inclusion in their Java Web Server The set of special tags that ATG had defined were somewhat cumbersome and sun refined these tags to create JSP Sun considered JSP as important enough to be included as a formal part of Java 2 Enterprise Edition The flow of information through a JSP is shown in the following figure
5
Flow of information through a Java Server Page 1 Client
1. Browser requests HTML
Server 7 2
6
2. Server sends request to Java Engine 3. If needed the Java Engine reads the JSP file
Servlet
Servlet
Servlet
5
5. The servlet runs and generates HTML
HTML
6. Java Engine sends HTML to server
Java Engine 4
4. The JSP is turned into a servlet compiled and loaded
3
7. Server sends HTML back to browser
Java Server Pages (JSP) • In a simple statement Java Server Pages is a combination of Java statements and HTML tags <% if (session.isNew()) %> Welcome user nice to see you <% else %> Glad to welcome you back JSP comments • In HTML comments are written like
6 Welcome To Java News Today
Your on-line home for up to the minute Java news
§ Home Page § Industry news § Enterprise Java § Standard Edition Java § Micro edition & devices § Beans § Editorials | |