Web Technologies Java Beans JSP Java Beans What
Web Technologies Java Beans & JSP
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, get. Xxx, set. Xxx & is. Xxx. 2
Java Bean (example) public class Person { private int age; private String name; … … … public void set. Age(int age){ this. age = age; } public void set. Name(String name){ this. name = name; } public int get. Age(){ return this. age; } public String get. Name(){ return this. name; } … … … } 3
Java Server Pages • Overview of JSP Technology • JSP Scripting Elements • JSP Page Directives 4
Overview of JSP Technology • • • What is JSP The need for JSP The benefits of JSP Advantages over other technologies Location of JSP pages 5
What is JSP • Servlets – HTML in java code • JSP – java code in HTML <HTML> <HEAD><TITLE>Java Server Pages</TITLE></HEAD> <BODY> <H 1>JSP</H 1> <%= “Java Server Pages. ” %> <HR> </BODY> </HTML> 6
JSP Lifecycle JSP to Servlet Translation Servlet Compiled Servlet Loaded jsp. Init() called _jsp. Service() called 7
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 8
The benefits of JSP • Easier to write and maintain HTML • Can use standard HTML tools • Can divide up development team 9
Advantages • • • The Java advantage Extensive API Easy to learn Big development community Standardization & server support 10
Location of JSP pages Unlike servlets, JSP pages can be located in any of the locations where HTML files can be put. 11
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 %> %> 12
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. 13
Predefined Variables • To simplify expressions, JSP provides a number of predefined variables (implicit objects). • • request – the Http. Servlet. Request response – the Http. Servlet. Response session – the Http. Session out – the Writer (buffered version of type Jsp. Writer) application – the Servlet. Context config – the Servlet. Config page. Context – introduced to give single point of access to page attributes page – synonym for “this” 14
JSP Scriptlets • To something more than just output the value of a simple expression. • Allows the programmer to insert arbitrary code into the servlets _jsp. Service method. • Have the following form: <% Java Code %> Eg: <% String str = request. get. Parameter(“name”); out. print(“Name : ”+str); %> 15
JSP Declarations • JSP declarations lets the programmer define methods or fields that get inserted into the main body of the generated servlet (outside the _jsp. Service() method) • Have the following form: <%! Field/Method definition %> Eg: <%! private String get. Message(){ return “This is a simple message!!”; } %> <%= get. Message() %> 16
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 17
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 18
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, content. Type, page. Encoding, session, is. ELIgnored, buffer, auto. Flush, info, error. Page, is. Thread. Safe, language & extends 19
JSP Page Directive Attributes import=“java. util. *, java. sql. *” content. Type=“text/html; charset=ISO-8859 -1 ” page. Encoding=“Shift_JIS” session=“true/false” is. ELIgnored=“false/true” buffer=“size in kb” auto. Flush=“true/false” info=“Some info message. ” error. Page=“error. jsp” is. Error. Page=“false/true” is. Thread. Safe=“true/false” org. apache. jasper. runtime. Http. Jsp. Base language=“java” extends=“package. class” javax. servlet. jsp. Http. Jsp. Page 20
Including Files There are three ways of including external files into a JSP document. <jsp: include …>… <%@ include …> <jsp: plugin …>… 21
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=“param. Name” value=“xyz”> </jsp: include> 22
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” %> 23
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” /> 24
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=“My. Applet. class” width=“ 400” height=“ 300”> </jsp: plugin> 25
jsp: plugin Attributes type=“applet” bean can also be used. Code=“My. Applet. 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=“…” 26
jsp: plugin Parameters & Fallback <jsp: plugin type=“applet” code=“My. Applet. class” width=“ 400” height=“ 300”> <jsp: params> <jsp: param name=“P 1” value=“xyz” /> <jsp: param name=“P 2” value=“abc” /> </jsp: params> <jsp: fallback> <b>Java Plugin needed. </b> </jsp: fallback> </jsp: plugin> 27
Using Java Beans & JSP There are three main constructs to use Java Beans in JSP. <jsp: use. Bean ……… /> <jsp: get. Property ……… /> <jsp: set. Property ……… /> 28
jsp: use. Bean Used to load a bean to be used in the JSP document. Syntax: <jsp: use. Bean id=“name” class=“package. Class” /> Eg: <jsp: use. Bean id=“person” class=“iiitmk. Person” /> Equivalent to: <% iiitmk. Person person = new iiitmk. Person(); %> 29
Getting bean properties Used to read properties from beans. Syntax: <jsp: get. Property id=“name” property=“prop. Name” /> Eg: <jsp: get. Property id=“person” property=“name” /> Equivalent to: <%= person. get. Name() %> 30
Setting bean properties Used to set properties of beans. Syntax: <jsp: set. Property id=“name” property=“prop. Name” value=“prop. Value” /> Eg: <jsp: set. Property id=“person” property=“name” value=“Popeye The Sailor” /> Equivalent to: <% person. set. Name(“Popeye The Sailor”); %> 31
Properties & Request Parameters The value of a bean property can be set directly from the value of the corresponding request parameter. Syntax: <jsp: set. Property id=“name” property=“prop. Name” param=“prop. Name” /> Eg: <jsp: set. Property id=“person” property=“name” param=“name” /> <jsp: set. Property id=“person” property=“*” /> 32
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: use. Bean … … … scope=“…” /> Scopes: page, request, session & application 33
Page Scope The default scope of a bean. Bean is bound to a local variable in the _jsp. Service method and also placed in the page. Context predefined variable, accessible by calling get. Attribute() method. Syntax: <jsp: use. Bean … … … scope=“page” /> <jsp: use. Bean … … … /> 34
Request Scope In addition to being bound to a local variable, the bean is also placed in the Http. Servlet. Request object (request) for the duration of the current request. Accessible by get. Attribute() method. Syntax: <jsp: use. Bean … … … scope=“request” /> 35
Session Scope In addition to being bound to a local variable, the bean is also placed in the Http. Session object (session). Accessible by get. Attribute() method. Syntax: <jsp: use. Bean … … … scope=“session” /> 36
Application Scope In addition to being bound to a local variable, the bean is also placed in the Servlet. Context object (application). The servlet context is shared by all the JSP and servlets in the webapplication. Accessible by get. Attribute() method. Syntax: <jsp: use. Bean … … … scope=“application” /> 37
Questions ?
- Slides: 38