Web Technologies Java Beans & JSP By Praveen

Web Technologies Java Beans & JSP By Praveen

Web Technologies Java Beans & JSP By Praveen Kumar G 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. February 24, 2020 2006 IIITM-K

2 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; } } February 24, 2020 2006 IIITM-K 3 Java Server Pages

Overview of JSP Technology JSP Scripting Elements JSP Page Directives February 24, 2020 2006 IIITM-K 4 Overview of JSP Technology What is JSP The need for JSP The benefits of JSP Advantages over other technologies

Location of JSP pages February 24, 2020 2006 IIITM-K 5 What is JSP Servlets HTML in java code JSP java code in HTML Java Server Pages

JSP

<%= Java Server Pages. %>

February 24, 2020 2006 IIITM-K 6 JSP Lifecycle JSP JSP to to Servlet Servlet Translation Translation

Servlet Servlet Compiled Compiled Servlet Servlet Loaded Loaded jspInit() jspInit() called called February 24, 2020

2006 IIITM-K _jspService() _jspService() called 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

February 24, 2020 2006 IIITM-K 8 The benefits of JSP Easier to write and maintain HTML Can use standard HTML tools Can divide up development team February 24, 2020 2006 IIITM-K 9

Advantages The Java advantage Extensive API Easy to learn Big development community Standardization & server support February 24, 2020 2006 IIITM-K 10 Location of JSP pages Unlike servlets, JSP pages can be located in any of the locations where HTML files can be put.

February 24, 2020 2006 IIITM-K 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 February 24, 2020

2006 IIITM-K %> 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. February 24, 2020 2006 IIITM-K 13 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 February 24, 2020

2006 IIITM-K 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 _jspService method. Have the following form: <% Java Code %> Eg: <% String str = request.getParameter(name); out.print(Name : +str); %>

February 24, 2020 2006 IIITM-K 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 _jspService() method) Have the following form: <%! Field/Method definition %> Eg: <%! private String getMessage(){

return This is a simple message!!; } %> <%= getMessage() %> February 24, 2020 2006 IIITM-K 16 XML Syntax XML like syntax for JSP expression, scriptlet & declaration

Supported by JSP versio 1.2 & above These are case sensitive, should be in lowercase February 24, 2020 2006 IIITM-K 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 February 24, 2020 2006 IIITM-K 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, contentType, pageEncoding, session,isELIgnored, buffer, autoFlush,

info, errorPage, isThreadSafe, language & extends February 24, 2020 2006 IIITM-K 19 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 org.apache.jasper.runtime.HttpJspBase org.apache.jasper.runtime.HttpJspBase language=java javax.servlet.jsp.HttpJspPage extends=package.class javax.servlet.jsp.HttpJspPage February 24, 2020 2006 IIITM-K 20 Including Files There are three ways of including

external files into a JSP document. <%@ include > February 24, 2020 2006 IIITM-K 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.

February 24, 2020 2006 IIITM-K 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 %>

February 24, 2020 2006 IIITM-K 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. February 24, 2020

2006 IIITM-K 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. February 24, 2020

2006 IIITM-K 25 jsp:plugin Attributes type=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= February 24, 2020 2006 IIITM-K 26 jsp:plugin Parameters & Fallback

Java Plugin needed. February 24, 2020 2006 IIITM-K 27 Using Java Beans & JSP There are three main constructs to use Java Beans in JSP.

February 24, 2020 2006 IIITM-K 28 jsp:useBean Used to load a bean to be used in the JSP document. Syntax: Eg: Equivalent to: <% iiitmk.Person person = new iiitmk.Person(); %>

February 24, 2020 2006 IIITM-K 29 Getting bean properties Used to read properties from beans. Syntax: Eg: Equivalent to: <%= person.getName() %>

February 24, 2020 2006 IIITM-K 30 Setting bean properties Used to set properties of beans. Syntax: Eg:

Equivalent to: <% person.setName(Popeye The Sailor); %> February 24, 2020 2006 IIITM-K 31 Properties & Request Parameters The value of a bean property can be set directly from the value of the corresponding request parameter. Syntax:

Eg: February 24, 2020 2006 IIITM-K 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: Scopes: page, request, session & application February 24, 2020 2006 IIITM-K 33 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: February 24, 2020 2006 IIITM-K 34 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: February 24, 2020 2006 IIITM-K 35 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:

February 24, 2020 2006 IIITM-K 36 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:

February 24, 2020 2006 IIITM-K 37 Questions ?

Recently Viewed Presentations

  • Principles of Marketing

    Principles of Marketing

    Marketsare the set of all actual and potential buyers of a product or service. Marketing . management : is the art and science of choosing target markets and building profitable relationships with them.
  • In-House Counsel Masterclass: Corporate M&A Trends Tuesday 18

    In-House Counsel Masterclass: Corporate M&A Trends Tuesday 18

    Lengthy underwriting process tending to delay transaction. Now. ... Policy can extend time periods and quantum/insurance limit for claims under warranties. Defects arising between signing and completion can be covered.
  • 5.189.141.65

    5.189.141.65

    410 Tarriff points and UKCAT 50:50 weighting. AAAb from three A2s and one further AS (one must be Chemistry or Biology + another science if not both taken) ST GEORGE'S. 8 GCSEs/IGCSEs at average grade A, to include Biology, Chemistry,...
  • Virus Informáticos

    Virus Informáticos

    Dr. Roberto Lombardo (Entre Ríos) Dr. Edgardo Schapachnik (Buenos Aires) Actividades prácticas: Dr. Eduardo Alonso (La Plata) Dr. Marcelo Bassino (Trenque Lauquen) Dr. Diego Esandi (Neuquén) Dr. Diego Garófalo (Rosario) Dr. Mario Heñin (Chaco) Dr. Silvia Nanfara (Río Gallegos) Dr....
  • Structure of the Atom (The tiny universe)
  • Lecture 18: Coherence and Synchronization  Topics: directory-based coherence

    Lecture 18: Coherence and Synchronization Topics: directory-based coherence

    Lecture 18: Coherence and Synchronization Topics: directory-based coherence protocols, synchronization primitives (Sections 5.1-5.5) * * Cache Coherence Protocols Directory-based: A single location (directory) keeps track of the sharing status of a block of memory Snooping: Every cache block is accompanied...
  • BoulderPhone.com GIGABIT ETHERNET IN THE WORKPLACE; LEVERAGING EXISTING

    BoulderPhone.com GIGABIT ETHERNET IN THE WORKPLACE; LEVERAGING EXISTING

    BoulderPhone.com GIGABIT ETHERNET IN THE WORKPLACE; LEVERAGING EXISTING INFRASTRUCTURE FOR THE COMING WIRELESS DATA TSUNAMI BoulderPhone.com GET READY - - The Business Community has installed an extensive fabric of Cat5E and Cat6 4-pair copper cable for office voice & data...
  • Motion Review - rowan.k12.ky.us

    Motion Review - rowan.k12.ky.us

    speed How far something moves in a certain amount of time and the slope of a position vs. time graph. A graph that looks like a straight line. A variable that is not dependent on the other (on the x-axis)....