Java Server Pages(jsp).pptx

24
JAVA SERVER PAGES(JSP) By Sujala V.Koparde

description

JSP ppt

Transcript of Java Server Pages(jsp).pptx

Java Server Pages(jsp)

Java Server Pages(jsp)BySujala V.KopardeWhat is JSP?

Java Server Page(JSP) is a server side program that is similar in design & functionality to a java servlet.

JavaServer Pages is the Java 2 Platform, Enterprise Edition(J2EE) technology for building applications for generating dynamic web content

How JSP differs from Java servlet? Or Compare JSP with Java servlet

A Java Servlet is written using the Java Programming Language and responses are encoded as an output String object that is passed to the println() method.

The output String object is formatted in HTML,XML or whatever formats are required by the client.

In Servlets you need to have lots of println statements to generate HTML. E.g. out.println(""); out.println("Welcome to my servlet page."); out.println("");

In contrast, JSP is written in HTML,XML or in the clients format that is interspersed with scripting elements, directives and actions comprised of Java Programming language and JSP syntax i.e. JSPs are essentially an HTML page with special JSP tags embedded. These JSP tags can contain Java code. E.g.

4Servlet is written in java language. Whereas jsp contains both jsp tags and html tags. JSP offers basically the same features found in java servlet because a JSP is converted to a Java servlet the first time that a client requests the JSP.

Servlet is a java class, So for every change, we have to compile the code to reflect the change. JSP is a file, Its automatically converted into a servlet on deploying. We can't compile it explicitly, the changes will get reflect by saving the file.

Servlet is mainly used for writing business logics (i.e. functional design) JSP is mainly used for presentation of data (i.e. web page design(look and feel))What are the advantages of JSP?

Content and display logic are separated

Recompile automatically when changes are made to the source file

Platform-independent

How Does JSP work?(architecture)

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.The JSP request gets sent to the Web server.The Web server recognises that the file required is special (.jsp),therefore passes the JSP file to the JSP Servlet Engine.If the JSP file has been called the first time,the JSP file is parsed,otherwise go to step 7.The next step is to generate a special Servlet from the JSP file. All the HTML required is converted to println statements.The Servlet source code is compiled into a class.The Servlet is instantiated,calling the init and service methods.HTML from the Servlet output is sent via the Internet.HTML results are displayed on the user's web browser.

Life Cycle methods of JSP. There are three methods that are automatically called when a JSP is requested & when the JSP terminates normally. These are: jspInit() service() jspDestroy()

The jspInit() method is identical to init() method in a java servlet and is called first when the JSP is requested. It is used to initialize objects and variables that are used throughout the life of the JSP.

service() is the JSP equivalent of the service() method of a servlet class. The server calls the service() for each request, passing it the request and the response objectsThe jspDestroy() method is identical to the destroy() method in a java servlet. It is automatically called when the JSP terminates. It is used for clean up where resources used during the execution of the JSP are released, such as disconnecting from a database.

JSP Tags

A JSP program consists of a combination of HTML tags and JSP tags. JSP tags define java code that is to be executed before the output of the JSP program is sent to the browser. A JSP tag begins with JSP tags are embedded into HTML component of s JSP program and are processed by a JSP virtual engine such as Tomcat.

There are five types of JSP tags:1. Comment tag2. Declaration tag3. Directive Tag4. Expression tag5. Scriptlet tag

1.Comment tag: Describes the functionality of statements that follow the comment tag.A comment tag opens with Syntax:

Example:

2.Declaration tag: This tag allows the developer to declare variables , objects and methods that will be used in Java Server Pages.Declaration tag starts with "" surrounding the declaration. Syntax :-

Examples

3.Directive Tag: A directive tag commands the JSP virtual engine to perform a specific task such as importing a java packages required by objects and methods used in a declaration statement. this tag opens with .There are three directives: import -is used to import java packages into the JSP program include- inserts a file at the location of the include tag. taglib - specifies a file that contains a tag library and allows the use of custom tags in the page Examples: : imports the java.util package : includes books.html file located in the the keogh directory : loads the myTags.tld library

4.Expression tag: It used for an expression statement whose result replaces the expression tag when the JSP virtual engine resolves JSP tagsSyntax is as follows Example to show the current date and time.

Note: You cannot use a semicolon to end an expression 5.Scriptlet tag: We can embed any amount of java code in the JSP scriplets.Syntax :For example, to print a variable.

Variables and objects

With JSP declarations, we can declare variables and objects that will be used later in the JSP expressions or scriplets.

Example: A JSP program that declares and uses a variable

My first JSP page

Your age is :

Example: Declaring multiple variables within a single JSP tag

JSP programming

Example: Declaring objects and arrays within a single JSP tag

JSP programming

Methods

Example: Defining and calling methods JSP programming

Your curved grade is :

Control statements

The primary method of flow control provided by JSP for designers are :if/else statements. switch statementsThese statements can check for something, such as the existence of a variable, and control what is output based on what it finds. Take a look at the following code:Example: JSP program using an if statement and a switch statement

JSP programming

69) { %> You passes! Better luck next time.

Your final grade is a A Your final grade is a B

Your final grade is a C

Your final grade is an F

Loops

Example: Using the for loop, while loop and the do..while loop to load HTML tables

JSP programming

First Second Third