38
Web Technologies Java Beans & JSP By Praveen Kumar G

Web Technologies Java Beans & JSP By Praveen Kumar G

Embed Size (px)

Citation preview

Page 1: Web Technologies Java Beans & JSP By Praveen Kumar G

Web TechnologiesJava Beans & JSP

ByPraveen Kumar G

Page 2: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 2

Java Beans

What Are Beans?

Beans are standard java objects.

• Must have a zero-arguments constructor.• Should have no public fields.• Values should be accessed through method calls,

getXxx, setXxx & isXxx.

Page 3: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 3

Java Bean (example)

public class Person {private int age;private String name;… … …public void setAge(int age){

this.age = age;}public void setName(String name){

this.name = name;}public int getAge(){

return this.age;}public String getName(){

return this.name;}

… … …}

Page 4: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 4

Java Server Pages

• Overview of JSP Technology• JSP Scripting Elements• JSP Page Directives

Page 5: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 5

Overview of JSP Technology

• What is JSP• The need for JSP• The benefits of JSP• Advantages over other

technologies• Location of JSP pages

Page 6: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 6

What is JSP

• Servlets – HTML in java code• JSP – java code in HTML

<HTML>

<HEAD><TITLE>Java Server Pages</TITLE></HEAD>

<BODY>

<H1>JSP</H1>

<%= “Java Server Pages.” %>

<HR>

</BODY>

</HTML>

Page 7: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 7

JSP Lifecycle

JSP to ServletTranslation

JSP to ServletTranslation

ServletCompiled

ServletCompiled

ServletLoaded

ServletLoaded

jspInit()called

jspInit()called

_jspService()called

_jspService()called

Page 8: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 8

The need for JSP

With servlets•It is hard to write and maintain

HTML•Cannot use standard HTML tools•HTML is inaccessible to non-java

developers

Page 9: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 9

The benefits of JSP

• Easier to write and maintain HTML• Can use standard HTML tools• Can divide up development team

Page 10: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 10

Advantages

• The Java advantage• Extensive API• Easy to learn• Big development community• Standardization & server support

Page 11: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 11

Location of JSP pages

Unlike servlets, JSP pages can be located in any of the locations where HTML files can be put.

Page 12: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 12

JSP Scripting Elements

JSP scripting elements enable us to insert java code into JSP files.

There are three types –•Expressions <%= Java Expression %>

•Scriptlets <% Java Code %>

•Declarations <%! Field/Method %>

Page 13: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 13

JSP Expressions

A JSP expression is used to insert java code directly into the output.Have the following form

<%= Expression %>Eg:

Current Time: <%= new java.util.Date() %>Op:

Current Time: Tue Aug 22 21:05:47 IST 2006

The expression is evaluated, converted to string and inserted into the page.

Page 14: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 14

Predefined Variables• To simplify expressions, JSP provides a

number of predefined variables (implicit objects).

• request – the HttpServletRequest• response – the HttpServletResponse• session – the HttpSession• out – the Writer (buffered version of type

JspWriter)

• application – the ServletContext• config – the ServletConfig• pageContext – introduced to give single point

of access to page attributes• page – synonym for “this”

Page 15: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 15

JSP Scriptlets• To something more than just output the

value of a simple expression.• Allows the programmer to insert

arbitrary code into the servlets _jspService method.

• Have the following form:<% Java Code %>

Eg:

<%

String str = request.getParameter(“name”);

out.print(“Name : ”+str);

%>

Page 16: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 16

JSP Declarations• JSP declarations lets the programmer

define methods or fields that get inserted into the main body of the generated servlet (outside the _jspService() method)

• Have the following form:<%! Field/Method definition %>

Eg:<%!

private String getMessage(){return “This is a simple message!!”;

}%><%= getMessage() %>

Page 17: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 17

XML Syntax

• XML like syntax for JSP expression, scriptlet & declaration<jsp:expression>…</jsp:expression>

<jsp:scriptlet>…</jsp:scriptlet>

<jsp:declaration>…</jsp:declaration>

• Supported by JSP versio 1.2 & above

• These are case sensitive, should be in lowercase

Page 18: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 18

JSP Directives

A JSP directive affects the overall structure of the servlet that results from the JSP page.

A JSP directive has the form:<%@ directive attribute=“value” … … %>

There are three types:page, include & taglib

Page 19: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 19

JSP Page Directive

The page directive controls the structure of the servlet by importing classes, customizing the superclass, changing content type, etc.The JSP Page directive has the following attributes:import, contentType, pageEncoding, session,isELIgnored, buffer, autoFlush, info, errorPage, isThreadSafe, language & extends

Page 20: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 20

JSP Page Directive Attributes

import=“java.util.*, java.sql.*”

contentType=“text/html; charset=ISO-8859-1 ”pageEncoding=“Shift_JIS”

session=“true/false”

isELIgnored=“false/true”

buffer=“size in kb”

autoFlush=“true/false”

info=“Some info message.”

errorPage=“error.jsp”

isErrorPage=“false/true”

isThreadSafe=“true/false”

language=“java”

extends=“package.class”

org.apache.jasper.runtime.HttpJspBase

javax.servlet.jsp.HttpJspPage

org.apache.jasper.runtime.HttpJspBase

javax.servlet.jsp.HttpJspPage

Page 21: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 21

Including Files

There are three ways of including external files into a JSP document.

<jsp:include …>…

<%@ include …>

<jsp:plugin …>…

Page 22: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 22

The jsp:include Action

This includes the output of a secondary page at the time the main page is requested.

The output of the sub page must be HTML generated by a servlet or JSP.<jsp:include page=“/inc/header.jsp” flush=“true” />

<jsp:include page=“/inc/header.jsp” flush=“true”>

<jsp:param name=“paramName” value=“xyz”>

</jsp:include>

Page 23: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 23

The Include Directive

This includes directive is used to include a file in the main JSP at the time of translation into a servlet.

The code of the included file is added to that of the JSP document.<%@ include page=“/inc/header.jsp” %>

Page 24: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 24

Forwarding Requests

This action is used to get the output of a JSP file completely from another JSP or servlet.

The output of the auxiliary JSP or servlet is sent to the client, not that of the current JSP.

<jsp:forward page=“xyz.jsp” />

Page 25: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 25

The jsp:plugin Action

Used to embed a java applet into the generated output.

Java applets are rarely used in web pages now a days.<jsp:plugin type=“applet”

code=“MyApplet.class”

width=“400” height=“300”>

</jsp:plugin>

Page 26: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 26

jsp:plugin Attributestype=“applet” bean can also be used.

Code=“MyApplet.class”

width=“…”

height=“…”

codebase=“base directory for the applet”

align=“…” laet, right, top, bottom or middle

hspace=“…”

vspace=“…”

archive=“specify JAR file”

title=“Title for the Applet”

jreversion=“1.2”

iepluginurl=“…”

nspluginurl=“…”

Page 27: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 27

jsp:plugin Parameters & Fallback

<jsp:plugin type=“applet”

code=“MyApplet.class”

width=“400” height=“300”>

<jsp:params>

<jsp:param name=“P1” value=“xyz” />

<jsp:param name=“P2” value=“abc” />

</jsp:params>

<jsp:fallback>

<b>Java Plugin needed.</b>

</jsp:fallback>

</jsp:plugin>

Page 28: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 28

Using Java Beans & JSP

There are three main constructs to use Java Beans in JSP.

<jsp:useBean ……… />

<jsp:getProperty ……… />

<jsp:setProperty ……… />

Page 29: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 29

jsp:useBean

Used to load a bean to be used in the JSP document.

Syntax: <jsp:useBean id=“name” class=“package.Class” />

Eg: <jsp:useBean id=“person” class=“iiitmk.Person” />

Equivalent to: <% iiitmk.Person person = new iiitmk.Person(); %>

Page 30: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 30

Getting bean properties

Used to read properties from beans.

Syntax: <jsp:getProperty id=“name” property=“propName” />

Eg: <jsp:getProperty id=“person” property=“name” />

Equivalent to: <%= person.getName() %>

Page 31: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 31

Setting bean properties

Used to set properties of beans.

Syntax: <jsp:setProperty id=“name” property=“propName”

value=“propValue” />

Eg: <jsp:setProperty id=“person” property=“name”

value=“Popeye The Sailor” />

Equivalent to: <% person.setName(“Popeye The Sailor”); %>

Page 32: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 32

Properties & Request Parameters

The value of a bean property can be set directly from the value of the corresponding request parameter.

Syntax: <jsp:setProperty id=“name” property=“propName”

param=“propName” />

Eg: <jsp:setProperty id=“person” property=“name”

param=“name” />

<jsp:setProperty id=“person” property=“*” />

Page 33: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 33

Sharing Beans (scope)

The scope of a bean defines where the bean is stored and how it is accessible. By default it is accessible as a local variable. Other places of storing beans are the request, session and application.Syntax: <jsp:useBean … … … scope=“…” />

Scopes:

page, request, session & application

Page 34: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 34

Page Scope

The default scope of a bean. Bean is bound to a local variable in the _jspService method and also placed in the pageContext predefined variable, accessible by calling getAttribute() method.Syntax: <jsp:useBean … … … scope=“page” /> <jsp:useBean … … … />

Page 35: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 35

Request Scope

In addition to being bound to a local variable, the bean is also placed in the HttpServletRequest object (request) for the duration of the current request.Accessible by getAttribute() method.Syntax: <jsp:useBean … … … scope=“request” />

Page 36: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 36

Session Scope

In addition to being bound to a local variable, the bean is also placed in the HttpSession object (session).Accessible by getAttribute() method.

Syntax: <jsp:useBean … … … scope=“session” />

Page 37: Web Technologies Java Beans & JSP By Praveen Kumar G

April 22, 2023 © 2006 IIITM-K 37

Application Scope

In addition to being bound to a local variable, the bean is also placed in the ServletContext object (application). The servlet context is shared by all the JSP and servlets in the webapplication.Accessible by getAttribute() method.

Syntax: <jsp:useBean … … … scope=“application” />

Page 38: Web Technologies Java Beans & JSP By Praveen Kumar G

Questions ?