Tuesday, October 5, 2010

JSP elements of jsp


JSP- Java Server Pages: Elements of jsp


Introduction:

A JSP page consists of various components. We use this components to create jsp application. There are three components in jsp of a jsp page:

1). JSP directives

2). JSP scripting

3). JSP actions

JSP Directives:

A directive element in a jsp page provides global information aboud a particular jsp page.
There are three types of directives.

1). Page directives

2). Taglib directives

3). Include directives

We include directive between <@ and %&gt. A directive have more than one attribute.

Syntax:

< % directive attribute=”value” % >


Page Directive:

The page directive defines attributes that tell the web container about the general setting of a jsp page. We specify different attributes with the page directive.

<% page attribute_list %>



Attribute name
              Description
language
Specify the scripting language of the jsp page


extends
Specify the parent class that the jsp generated servlet extends.
import
Import the list of packages, classes, or interfaces into the generated servlet.


session
Specify if the generated servlet can access the session or not. An implicit object, session, is generated if the value is set to true. The default value of session is true.


buffer
Specify the size of the out buffer. If size is set to none, no buffering is performed. The default value of buffer size is 8kb.


autoflush
Specifies the out buffer is flushed automatically if the value is set to true. If the value is set to false, an exception is thrown when the buffer is full. The default value of autoflush attribute is true.



isThreadSafe

This attribute tells the jsp engine that this page can service more than one request at a time. By default, this value is true. If we set to false, the SindleThreadModel is used.

errorPage

This attribute represents the relative URL to the JSP errorPage. The default is false.



isErrorPage

Specify that the current JSP page is an error page, if the attribute value is set to true. The default value is false



contentType


Specify the MIME (multipurpose Internal Mail Extension) for a response. The default value is text/html.





2). Taglib directive

The taglib directive imports a custom tag into the current jsp page. Custom tag is user-defined tag, which is used to perform repetitive tasks in jsp page. The Tag Library Descriptor (TLD) file defines the functionality of a custom tag.



<%@ taglib uri=”tag lib uri” prefix=”specify prefix” % >


Attribute
Description


uri


Locates the TLD file of a custom tag


Prefix


Defines a prefix string to be used for distinguish a custom tag instance.

Type of tag extension


1). Custom tag without body

2). Custom tag with body

3). Custom tag with nested


3). Include directive

The include directive is used to insert text and code at JSP translation time.

< % include file=”URLName” % >

Example for include directive


home.html


< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >

< html >
< head >
< title > Example for include directive < /title >
< /head >
< body >
sureshkumar from home.html
< /body >
< /html >


includeExam.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< %@include file="home.html" % >
< html >
< head >
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
< title > JSP Page < /title >
< /head >
< body >
< h1 > Hello World! < /h1 >
< /body >
< /html >


JSP what happen under background

 
JSP – what happen under background?

Introduction:

The execution of the jsp files lies in a special servlet called pagecompile servlet.

This servlet works in the following manner:

1). When a web server receives a request for a page ending with .jsp, it hands over the request to the pagecompile servlet.

2). The pagecompile servlet scans a list of pages that it has cached. If the request is for a new page, the pagecompile servlet generates a new servlet for this JSP. The new servlet
Combine the content from a buffer of HTML tags with the executable java cod contained in jsp.

3). The pagecompile servlet compiles this servlet into a java class, loads the servlet and turns over execution to the service() method of the new servlet. It finally adds this servlet to its list of cached pages. For subsequent requests, procession will be immediately turned over to the service() method of this cached servlet. 




Saturday, October 2, 2010

JSP-Requirements for running JSP


REQUIREMENTS FOR RUNNING THE JSP

TOMCAT APPLICATION SERVER



There are many version of the tomcat server.

You can download tomcat server from the following apache official site.

www.tomcat.apache.org


If you have tomcat 4 or older version, you must set path in environmental variables.

How to set the Environmental variable if you use tomcat 4.

Step 1:

            Goto desktop




















Step 2:
               Right click on the My Computer





















In system properties window, you select Advanced in Tabbed panel.



















In system properties window, you select Advanced in Tabbed panel.























After select Advanced, you click Environment Variables
Environmental window will open.



















There are two parts here.

1). User variables

2). System variables

You only use user variables

1). CATALINA_HOME

Select New Button. New User Variable window will open. Where you give variable name as CATALINA_HOME and Variable value as F:\Apache. Because I have installed Apache tomcat server in F:\Apache. And click OK.



















2). JAVA_HOME
 
            You follow above steps to create another user variable for JAVA_HOME

Variable name     JAVA_HOME

Variable Value    (where you have installed java) F:\jdk1.5




















3). PATH
            Create another user variables for PATH

            Variable name                         PATH

            Variable value             F:\jdk1.5\bin; (note put the semicolon)






















4). CLASSPATH

            Create another user variable for CLASSPATH

            Variable name                    CLASSPATH

             Variable value      F:\jdk1.5\lib; F:\Apache\lib\ jsp-api.jar; F:\Apache\lib\servlet-api.jar;

























Note: if you use Tomcat 4 or older version.
  
If you use latest version such as tomcat 5 or tomcat 6. No need to create environment variables

We want to check the server is run properly or not.

Goto F:\Apache\bin\

Select startup















Open the browser

Enter http://localhost:8080 in address bar













How To create Folder For Running jsp


Where to create folder to save jsp file and html file.

Go to Apache Tomcat Folder( where you have installed tomcat)

For Example I have installed           F:\Apache

Open Apache Tomcat Folder.














Open webapps folder













Create New Folder and named something such as suresh as your desire.
















Open Folder suresh.

You create WEB-INF Folder

You create jsp Folder for storing jsp

You create html Folder for storing html














Next you have to create web.xml.

Go to back webapps











Open the Folder examples and WEB-INF
























Copy the web.xml file and paste into F:\Apache\webapps\suresh\WEB-INF
















go back jsp folder

F:\Apache\webapps\suresh\jsp

Create jsp file

Open the notepad

Type the following coding

< html >
< head > < title > jsp example < /title > < /head >
< body >
hai welcome
< %= welcome to jsp wolrd % >
< /body >
< /html >

Start the tomcat server


















Open the browser such as windows Internet Explorer or FireFox

                http://localhost:8080/suresh/jsp/welcome.jsp

                You will get output

                Hai welcome

                Welcome to jsp world.


JSP-Introduction of jsp


Introduction Of the JSP (Java Server Pages)

            A web application consists of the presentation and business logic.

            Presentation layer- Web Designer

Business Logic- Web Developer


JSP is used to perform Presentation.

What is the presentation layer?

In web site, what are the pages are interacted with the user or client which pages are called UserInterface. Presentation layer is also known as UserInterface.
                       
                        UserInterface- UI components.


Web designer don’t need to know the business logic. They know HTML pages to design the web pages in terms of the page layout, color, and text.

Presentation logic represents the static content.

Business Logic represents the dynamic content.

A web designer can design the web page by using HTML at the same time the web developer can use java code and other jsp tags to code the business logic. So both web designer and web developer can work independently.


Although we use servlet to perform presentation logic, there are some reasons we use jsp to create presentation logic.


Compiling the jsp page.

A jsp page after compilation generates a servlet and therefore incorporates all servlet functionalities.

Servlet and jsp share common features such as OS(Operating System) independent, create DB web application and server side programming.

Difference Between Jsp And Servlet

Servlet files: combine HTML and servlet( HTML file for the static content and servlet file for dynamic content).

In servlet, a change made to any files, we require to re-compile the servlet.

In jsp, jsp allows java to be embedded directly into an HTML page by using tags. The HTML content and java content can also be placed in separate files. So any changes made to HTML content is automatically compiled and loaded onto the server.

Jsp Life cycle:

When the client browser request for the page, the server sends the request to the JSP engine. JSP engine is part of a web container that compiles a jsp page to servlet.


Once a jsp page is translated to a servet, the container invokes life cycle method.

jspInit(): this method is invoked when the servlet is initialized.

jspService(): this method is invoked when the request for the jsp page is received.

jspDestroy(): this method is invoked before the servlet is removed from the service.