Introduction to JSP
JavaServer Pages (JSP) is a technology based on the Java language and enables the development of dynamic web sites. JSP was developed by Sun Microsystems to allow server side development. JSP files are HTML files with special Tags containing Java source code that provide the dynamic content.
The following shows the Typical Web server,different clients connecting via the Internet to a Web server. In this example,the Web server is running on Unix and is the very popular Apache Web server
First static web pages were displayed. Typically these were people?s first experience with making web pages so consisted of My Home Page sites and company marketing information. Afterwards Perl and C were languages used on the web server to provide dynamic content. Soon most languages including Visualbasic,Delphi,C and Java could be used to write applications that provided dynamic content using data from text files or database requests. These were known as CGI server side applications. ASP was developed by Microsoft to allow HTML developers to easily provide dynamic content supported as standard by Microsoft?s free Web Server,Internet Information Server (IIS). JSP is the equivalent from Sun Microsystems,a comparison of ASP and JSP will be presented in the following section.
The following diagram shows a web server that supports JSP files. Notice that the web server also is connected to a database.
JSP source code runs on the web server in the JSP Servlet Engine. The JSP Servlet engine dynamically generates the HTML and sends the HTML output to the client?s web browser.
Use Of JSP
JSP is easy to learn and allows developers to quickly produce web sites and applications in an open and standard way. JSP is based on Java,an object-oriented language. JSP offers a robust platform for web development.
Main reasons to use JSP:
1. Multi platform
2. Component reuse by using Javabeans and EJB.
3. Advantages of Java.
4. You can take one JSP file and move it to another platform,web server or JSP Servlet engine.
This means you are never locked into one vendor or platform.
HTML and graphics displayed on the web browser are classed as the presentation layer. The Java code (JSP) on the server is classed as the implementation.
By having a separation of presentation and implementation,web designers work only on the presentation and Java developers concentrate on implementing the application.
JSP architecture
JSPs are built on top of SUN Microsystems' servlet technology. JSPs are essential an HTML page with special JSP tags embedded. These JSP tags can contain Java code. The JSP file extension is .jsp rather than .htm or .html. The JSP engine parses the .jsp and creates a Java servlet source file. It then compiles the source file into a class file,this is done the first time and this why the JSP is probably slower the first time it is accessed. Any time after this the special compiled servlet is executed and is therefore returns faster.
Steps required for a JSP request:
1. The user goes to a web site made using JSP. The user goes to a JSP page (ending with .jsp). The web browser makes the request via the Internet.
2. The JSP request gets sent to the Web server.
3. The Web server recognises that the file required is special (.jsp),therefore passes the JSP file to the JSP Servlet Engine.
4. If the JSP file has been called the first time,the JSP file is parsed,otherwise go to step 7.
5. The next step is to generate a special Servlet from the JSP file. All the HTML required is converted to println statements.
6. The Servlet source code is compiled into a class.
7. The Servlet is instantiated,calling the init and service methods.
8. HTML from the Servlet output is sent via the Internet.
9. HTML results are displayed on the user's web browser.
JSP tags
There are five main tags:
1. Declaration tag
2. Expression tag
3. Directive tag
4. Scriptlet tag
5. Action tag
Declaration tag ( <%! %> )
This tag allows the developer to declare variables or methods.
Before the declaration you must have <%!
At the end of the declaration,the developer must have %>
Code placed in this tag must end in a semicolon ( ; ).
Declarations do not generate output so are used with JSP expressions or scriptlets.
<%!
private int counter = 0 ;
private String get Account ( int accountNo) ;
%>
This tag allows the developer to embed any Java expression and is short for out.println().
A semicolon ( ; ) does not appear at the end of the code inside the tag.
Example:To show the current date and time.
Date : <%= new java.util.Date() %>
A JSP directive gives special information about the page to the JSP Engine.
There are three main types of directives:1) page - processing information for this page.
2) Include - files to be included.
3) Tag library - tag library to be used in this page.
Directives do not produce any visible output when the page is requested but change the way the JSP Engine processes the page.
Example:
You can make session data unavailable to a page by setting a page directive (session) to false.
There are three main roles of action tags :
1) enable the use of server side Javabeans
2) transfer control between pages
3) browser independent support for applets.
Javabeans
A Javabean is a special type of class that has a number of methods. The JSP page can call these methods so can leave most of the code in these Javabeans. For example,if you wanted to make a feedback form that automatically sent out an email. By having a JSP page with a form,when the visitor presses the submit button this sends the details to a Javabean that sends out the email. This way there would be no code in the JSP page dealing with sending emails (JavaMail API) and your Javabean could be used in another page (promoting reuse).
Example:To use a Javabean in a JSP page use the following syntax:
The following is a list of Javabean scopes:
1. page - valid until page completes.
2. request - bean instance lasts for the client request
3. session - bean lasts for the client session.
4. application - bean instance created and lasts until application ends.
Between <% and %> tags,any valid Java code is called a Scriptlet. This code can access any variable or bean declared.
Example:to print a variable.
<%
String username = "visualbuilder" ;
out.println ( username ) ;
%>
No comments:
Post a Comment