Thursday, October 25, 2007

Jsp example


Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database driven web applications. JSP enable the developers to directly insert java code into jsp file, this makes the development process very simple and its maintenance also becomes very easy. JSP pages are efficient, it loads into the web servers memory on receiving the request very first time and the subsequent calls are served within a very short period of time.
In today's environment most web sites servers dynamic pages based on user request. Database is very convenient way to store the data of users and other things. JDBC provide excellent database connectivity in heterogeneous database environment. Using JSP and JDBC its very easy to develop database driven web application.
Java is known for its characteristic of "write once, run anywhere." JSP pages are platform independent. Your port your .jsp pages to any platform.
Installing JSP

First of all download JavaServer Web Development Kit (JSWDK1.0.1) from http://java.sun.com/products/servlet/download.html. JSWDK comes with full documentation and it's very easy to install, so the installation process is not mentioned here. The JSWDK is the official reference implementation of the servlet 2.1 and JSP 1.0 specifications. It is used as a small stand-alone server for testing servlets and JSP pages before they are deployed to a full Web server that supports these technologies. It is free and reliable, but takes quite a bit of effort to install and configure.
Other Servers that support JSP
Apache Tomcat.
Tomcat is the official reference implementation of the servlet 2.2 and JSP 1.1 specifications. It can be used as a small stand-alone server for testing servlets and JSP pages, or can be integrated into the Apache Web server.
• Allaire JRun.
JRun is a servlet and JSP engine that can be plugged into Netscape Enterprise or FastTrack servers, IIS, Microsoft Personal Web Server, older versions of Apache, O’Reilly’s WebSite, or StarNine WebSTAR.
• New Atlanta’s ServletExec.
ServletExec is a fast servlet and JSP engine that can be plugged into most popular Web servers for Solaris, Windows, MacOS, HP-UX and Linux. You can download and use it for free, but many of the advanced features and administration utilities are disabled until you purchase a license.
• Gefion's LiteWebServer (LWS). LWS is a small free Web server that supports servlets version 2.2 and JSP 1.1.
• GNU JSP. free, open source engine that can be installed on apache web server.
• PolyJSP. PolyJsp is based on XML/XSL and has been designed to be extensible. Now supports WebL
• JRUN. Available for IIS server.
• WebSphere. IBM's WebSphere very large application server now implements JSP.

Developing first JSP
Java Server Pages are save with .jsp extension. Following code which generates a simple html page.


First JSP page.


<%="Java Developers Paradise"%>


<%="Hello JSP"%>




In jsp java codes are written between '<%' and '%>' tags. So it takes the following form : <%= Some Expression %> In this example we have use
<%="Java Developers Paradise"%>
JPS pages are high level extension of servlet and it enable the developers to embed java code in html pages. JSP files are finally compiled into a servlet by the JSP engine. Compiled servlet is used by the engine to serve the requests.
javax.servlet.jsp package defines two classes:
• JSPPage
• HttpJspPage

These classes defines the interface for the compiled JSP page. These interfaces are:
• jspInit()
• jspDestroy()
• _jspService(HttpServletRequest request,HttpServletResponse response)

In the compiled JSP file these methods are present. Programmer can define jspInit() and jspDestroy() methods, but the _jspService(HttpServletRequest request,HttpServletResponse response) method is generated by the JSP engine.

In this lesson we will learn about the various tags available in JSP with suitable examples. In JSP tags can be devided into 4 different types. These are:

1. Directives
In the directives we can import packages, define error handling pages or the session information of the JSP page.

2. Declarations
This tag is used for defining the functions and variables to be used in the JSP.

3. Scriplets
In this tag we can insert any amount of valid java code and these codes are placed in _jspService method by the JSP engine.

4. Expressions
We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream.

Now we will examine each tags in details with examples.
DIRECTIVES
Syntax of JSP directives is:
<%@directive attribute="value" %>
Where directive may be:
1. page: page is used to provide the information about it.
Example: <%@page language="java" %>

2. include: include is used to include a file in the JSP page.
Example: <%@ include file="/header.jsp" %>

3. taglib: taglib is used to use the custom tags in the JSP pages (custom tags allows us to defined our own tags).
Example: <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>

and attribute may be:
1. language="java"
This tells the server that the page is using the java language. Current JSP specification supports only java language.
Example: <%@page language="java" %>

2. extends="mypackage.myclass"
This attribute is used when we want to extend any class. We can use comma(,) to import more than one packages.
Example: <%@page language="java" import="java.sql.*,mypackage.myclass" %>

3. session="true"
When this value is true session data is available to the JSP page otherwise not. By default this value is true.
Example: <%@page language="java" session="true" %>

4. errorPage="error.jsp"
errorPage is used to handle the un-handled exceptions in the page.
Example: <%@page language="java" session="true" errorPage="error.jsp" %>

5. contentType="text/html;charset=ISO-8859-1"
Use this attribute to set the mime type and character set of the JSP.
Example: <%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1" %>

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com