Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Technologies Java Beans & JSP By Praveen Kumar G.

Similar presentations


Presentation on theme: "Web Technologies Java Beans & JSP By Praveen Kumar G."— Presentation transcript:

1 Web Technologies Java Beans & JSP By Praveen Kumar G

2 December 21, 2015© 2006 IIITM-K2 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.

3 December 21, 2015© 2006 IIITM-K3 Java Bean (example) public class Person { private intage; private Stringname; … … … 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; } … … … }

4 December 21, 2015© 2006 IIITM-K4 Java Server Pages Overview of JSP Technology JSP Scripting Elements JSP Page Directives

5 December 21, 2015© 2006 IIITM-K5 Overview of JSP Technology What is JSP The need for JSP The benefits of JSP Advantages over other technologies Location of JSP pages

6 December 21, 2015© 2006 IIITM-K6 What is JSP Servlets – HTML in java code JSP – java code in HTML Java Server Pages JSP

7 December 21, 2015© 2006 IIITM-K7 JSP Lifecycle JSP to Servlet Translation JSP to Servlet Translation Servlet Compiled Servlet Compiled Servlet Loaded Servlet Loaded jspInit() called jspInit() called _jspService() called _jspService() called

8 December 21, 2015© 2006 IIITM-K8 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

9 December 21, 2015© 2006 IIITM-K9 The benefits of JSP Easier to write and maintain HTML Can use standard HTML tools Can divide up development team

10 December 21, 2015© 2006 IIITM-K10 Advantages The Java advantage Extensive API Easy to learn Big development community Standardization & server support

11 December 21, 2015© 2006 IIITM-K11 Location of JSP pages Unlike servlets, JSP pages can be located in any of the locations where HTML files can be put.

12 December 21, 2015© 2006 IIITM-K12 JSP Scripting Elements JSP scripting elements enable us to insert java code into JSP files. There are three types – Expressions Scriptlets Declarations

13 December 21, 2015© 2006 IIITM-K13 JSP Expressions A JSP expression is used to insert java code directly into the output. Have the following form Eg: Current Time: Op: Current Time: Tue Aug 22 21:05:47 IST 2006 The expression is evaluated, converted to string and inserted into the page.

14 December 21, 2015© 2006 IIITM-K14 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”

15 December 21, 2015© 2006 IIITM-K15 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: Eg: <% String str = request.getParameter(“name”); out.print(“Name : ”+str); %>

16 December 21, 2015© 2006 IIITM-K16 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: Eg: <%! private String getMessage(){ return “This is a simple message!!”; } %>

17 December 21, 2015© 2006 IIITM-K17 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

18 December 21, 2015© 2006 IIITM-K18 JSP Directives A JSP directive affects the overall structure of the servlet that results from the JSP page. A JSP directive has the form: There are three types: page, include & taglib

19 December 21, 2015© 2006 IIITM-K19 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

20 December 21, 2015© 2006 IIITM-K20 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

21 December 21, 2015© 2006 IIITM-K21 Including Files There are three ways of including external files into a JSP document. … …

22 December 21, 2015© 2006 IIITM-K22 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.

23 December 21, 2015© 2006 IIITM-K23 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.

24 December 21, 2015© 2006 IIITM-K24 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.

25 December 21, 2015© 2006 IIITM-K25 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”>

26 December 21, 2015© 2006 IIITM-K26 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=“…”

27 December 21, 2015© 2006 IIITM-K27 jsp:plugin Parameters & Fallback <jsp:plugin type=“applet” code=“MyApplet.class” width=“400” height=“300”> Java Plugin needed.

28 December 21, 2015© 2006 IIITM-K28 Using Java Beans & JSP There are three main constructs to use Java Beans in JSP.

29 December 21, 2015© 2006 IIITM-K29 jsp:useBean Used to load a bean to be used in the JSP document. Syntax: Eg: Equivalent to:

30 December 21, 2015© 2006 IIITM-K30 Getting bean properties Used to read properties from beans. Syntax: Eg: Equivalent to:

31 December 21, 2015© 2006 IIITM-K31 Setting bean properties Used to set properties of beans. Syntax: Eg: Equivalent to:

32 December 21, 2015© 2006 IIITM-K32 Properties & Request Parameters The value of a bean property can be set directly from the value of the corresponding request parameter. Syntax: Eg:

33 December 21, 2015© 2006 IIITM-K33 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

34 December 21, 2015© 2006 IIITM-K34 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:

35 December 21, 2015© 2006 IIITM-K35 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:

36 December 21, 2015© 2006 IIITM-K36 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:

37 December 21, 2015© 2006 IIITM-K37 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:

38 Questions ?


Download ppt "Web Technologies Java Beans & JSP By Praveen Kumar G."

Similar presentations


Ads by Google