You are on page 1of 22

JAVA SERVER PAGES

JSP (Java Server Pages)


User request

Web server/servlet engine Check to see if the file has changed since last compiled

Jsp handler servlet

Jsp source code

Existing servlet

File changed? yes Create source file

No,execute existing servlet

Compile to a servlet

Execute new servlet

IMPLICIT OBJECTS IN JSP


request - Encapsulates the client request. Usually a sub class of HttpServletRequest response - Encapsulates the servers response. Usually a sub class of HttpServletResponse session - Object of HttpSession. The session object associated with every client. Used to store client data. out - object representing the output stream. exception - uncaught subclass of Throwable that is passed to the error page URL. config - object representing the ServletConfig. application - Object of ServletContext. Used to store global info. To be shared by all the clients
3

COMPONENTS OF A JSP PAGE

<%!int MAX_AGE = 50; %>

DIRECTIVES
Directives are elements that provide global information, about an entire JSP page. Three types of directives Page Directive Include Directive

Taglib Directive

<%@ directive attribute=value %>

ATTRIBUTES OF A PAGE DIRECTIVE


<%@ page import=java.util.List,java.util.Date errorPage=err.jsp %> ATTRIBUTE language=scripting import=import list DEFINITION This attribute tells the server what language is used to compile the server. This attribute specifies the list of packages available to this jsp. The packages are seperated by commas.

isThreadSafe=boolean

Specifies that the jsp engine can service more than one thread is set to true.
Refers to the file name which will handle the exceptions. This attribute states whether or not a jsp page is an error page. Default is false.
6

errorPage=filename isErrorPage=boolean

Global Error page

Multiple err pages can be defined based on err code and exception type

Err.jsp

When NumberFormatException arises , it redirects to Err.jsp

Local err pages

Place this line in the current page

<%@page errorPage="Err2.jsp" %>

When any exception arises in the current page, it redirects to Err2.jsp


Eventhough Global err pages are defined, Local err pages are shadowed

ATTRIBUTES OF A INCLUDE DIRECTIVE


The include directive is used to insert text and code at jsp translation time. <%@ include file= index.jsp%> It happens at compile time It is best suited for including static page

Home.jsp

Converts in to java code

.. ..
Single Servlet

Complied

Index.jsp

req thread

instance

.class
10

res

Including static page

Index.jsp

11

ACTION TAGS
Actions are used for encapsulating common tasks. Especially used to create objects of java beans. <jsp:include> <jsp:forward> Includes static and dynamic page. Dispatches the current page to another resource (html, servlet, jsp). Used to pass values using a name and value pair.

<jsp:param>

12

ACTION TAGS
<jsp:useBean> <jsp:setProperty> <jsp:getProperty> Associates an instance of a Bean. Sets the Value for the Bean Property Retrives the Value from the Bean Property
13

<jsp:include> Action
<jsp:include page= First.jsp > <jsp:param name=name value=ram/> <jsp:include>

includes dynamic page Happens at run time

Home.jsp

Jsp param tag encrypts special characters such as blank space, &, >,< etc

First.jsp The parameters are passed to the included file at run time and then it includes
14

<jsp:forward> Action
<jsp:forward page= second.jsp > <jsp:param name=name value = ram/ > </jsp:forward>

This happens at run time The equivalent code in servlet RequestDispatcher rd = request.getRequestDispatcher(Second.jsp); rd.forward(request,response); Note: parameters can take only string values
15

ATTRIBUTES OF <jsp:useBean> ACTION


ATTRIBUTE id scope class DEFINITION This attribute gives the identity of the instance. This attribute represents the life of the object. refers to the class name of the bean.

Equivalent code in java (Form submit) <%Person per = new Person(); %>

<jsp:useBean id="per" class="com.samples.beans.Person">

Identifies standard action

Declares an identifier for bean Object

Declares class type


16

ATTRIBUTES OF <jsp:setProperty> ACTION


ATTRIBUTE name DEFINITION refers to the name of the bean instance (same as id)

property
value

Name of the bean property to which a value is set.


The value that is set to the referred bean property.

<jsp:setProperty name="per" property="empName" />

Identifies the bean instance Identifies the setter method in the bean class If html control parameters are same as bean fields, No need of param attr
17

setProperty
<input type="text" name="empName"/>

<jsp:setProperty name="per" property="empName" />

<input type="text" name=txtName"/>

<jsp:setProperty name="per" property="empName" param = txtName/>

setEmpName(String empName)

18

<jsp:setProperty name="per" property=*" />

It binds the all the form values to bean setters

ATTRIBUTES OF <jsp:getProperty> ACTION


<jsp:getProperty property="empName" name="per"/>

Identifies getter methods in bean class

Name of bean instance

19

Why Beans in Jsp

Posted to

Programmer explicitly converts the parameters to respected type

20

Form bean architecture

Bean Form jsp

21

Jsp:useBean demo

Posted to

Using action tags, Scripting can be eliminated No need of explicit conversion

22

You might also like