servlet&jsp related theory

4
What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servletinterface, which defines life-cycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services. Servlet Life Cycle The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps. 1.  If an instance of the servlet does not exist, the Web container a. Loads the servlet class. b. Creates an instance of the servlet class. c. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet. 2.  Invokes the service method, passing a request and response object. Service methods are discussed in the section Writing Service Methods. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method. Finalization is discussed in Finalizing a Servlet.

Upload: siddhesh-pange

Post on 06-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Servlet&JSP Related Theory

8/3/2019 Servlet&JSP Related Theory

http://slidepdf.com/reader/full/servletjsp-related-theory 1/4

What Is a Servlet? 

A servlet  is a Java programming language class used to extend the capabilities

of servers that host applications accessed via a request-response programming

model. Although servlets can respond to any type of request, they are

commonly used to extend the applications hosted by Web servers. For suchapplications, Java Servlet technology defines HTTP-specific servlet classes.

The javax.servlet and javax.servlet.http packages provide interfaces and classes

for writing servlets. All servlets must implement the Servletinterface, which

defines life-cycle methods.

When implementing a generic service, you can use or extend

the GenericServlet class provided with the Java Servlet API.

The HttpServlet class provides methods, such as doGet and doPost, for

handling HTTP-specific services.

Servlet Life Cycle 

The life cycle of a servlet is controlled by the container in which the servlet has

been deployed. When a request is mapped to a servlet, the container performs

the following steps.

1.  If an instance of the servlet does not exist, the Web container

a.  Loads the servlet class.

b.  Creates an instance of the servlet class.c.  Initializes the servlet instance by calling the init method.

Initialization is covered in Initializing a Servlet.

2.  Invokes the service method, passing a request and response object.

Service methods are discussed in the section Writing Service Methods.

If the container needs to remove the servlet, it finalizes the servlet by calling

the servlet's destroy method. Finalization is discussed in Finalizing a Servlet.

Page 2: Servlet&JSP Related Theory

8/3/2019 Servlet&JSP Related Theory

http://slidepdf.com/reader/full/servletjsp-related-theory 2/4

Page 3: Servlet&JSP Related Theory

8/3/2019 Servlet&JSP Related Theory

http://slidepdf.com/reader/full/servletjsp-related-theory 3/4

White spaces after the opening <%@ and before the closing %> are optional.

The Page Directive 

The Page directive has the following syntax:

<%@ page (attribute="value")* %>

Or, if you want to use the more informal syntax:

<%@ page attribute1="value1" attribute2="value2" ... %>

The Page directive supports 11 attributes. These attributes are summarized in

Table

Attribute Value Type Default Value

Language  Scripting language

name 

"java" 

contentType  MIME type,  character set

"text/html;charset=ISO-8859-1"  

extends Class name  None 

import Fully qualified class

name or package

name 

None 

buffer  Buffer size or false 8192

autoFlush  Boolean  "true" 

session Boolean  "true" 

isThreadSafe Boolean  "true" 

errorPage URL None 

isErrorPage boolean false

The Include Directive

The include directive is the second type of the JSP directive elements. This

directive enables JSP page authors to include the contents of other files in the

current JSP page.

Page 4: Servlet&JSP Related Theory

8/3/2019 Servlet&JSP Related Theory

http://slidepdf.com/reader/full/servletjsp-related-theory 4/4

 

Scripting Elements

Scripting elements allow you to insert Java code in your JSP pages. There are

three types of 

scripting elements: Scriptlets

Declarations

Expressions

The three elements are discussed in the following sections.

Scriptlets 

Throughout the previous chapters and up to this point in this chapter, you

have seen scriptlets in

the examples. Scriptlets are the code blocks of a JSP page. Scriptlets start with

an opening <% tag

and end with a closing %> tag.

Declarations

Declarations allow you to declare methods and variables that can be used from

any point in the JSP page. Declarations also provide a way to create

initialization and clean-up code by utilizing the jspInit and jspDestroy methods.

A declaration starts with a <%! and ends with a %> and can appear anywhere

throughout the page.

Expressions

Expressions are the last type of JSP scripting elements. Expressions get

evaluated when the JSP page is requested and their results are converted into

a String and fed to the print method of the out implicit object. If the result

cannot be converted into a String, an error will be raised at translation time. If 

this is not detected at translation time, at request-processing time, aClassCastException will be raised.

An expression starts with a <%= and ends with a %>. You don't add a semicolon

at the end of an expression.