Java Server Pages An introduction to JSP Containers

  • Slides: 21
Download presentation
Java Server Pages An introduction to JSP

Java Server Pages An introduction to JSP

Containers and Components n Several clients – one system

Containers and Components n Several clients – one system

Java Server Pages (JSP) n n HTML with embedded Java code Good for presentation

Java Server Pages (JSP) n n HTML with embedded Java code Good for presentation q n No print statements Compiled to a Servlet at runtime q q Good performance Easy deployment n n n Easy for web designers to use q n When a JSP is updated it’s recompiled Possible to compile manually Edited in an ordinary HTML editor Support for Java Beans and Custom tags

JSP – Directives n At the top of a JSP page, the page directive

JSP – Directives n At the top of a JSP page, the page directive is used q <%@ page name=“value” %>

JSP – Page Directive parameters n language q n extends q n n Default

JSP – Page Directive parameters n language q n extends q n n Default is java Default is the base JSP class, but you could use your own import=“package 1, package 2, class 1, class 2” session q True or False, determines if sessions should be automatically created. True is default

JSP – Page Directive parameters n buffer q n auto. Flush q n True

JSP – Page Directive parameters n buffer q n auto. Flush q n True or false, determines if the output should be flushed to the client. True is default is. Thread. Safe q n The output buffer size True or false. True is default info q Descriptive text

JSP – Page Directive parameters n error. Page q n n The page to

JSP – Page Directive parameters n error. Page q n n The page to go to in case of an error content. Type is. Error. Page q Defines if this is a error page. False is default. If it’s an error page a couple of extra objects are available in the page

Other Directives n Include directive q q q <%@ include file=“relative url” %> Includes

Other Directives n Include directive q q q <%@ include file=“relative url” %> Includes the file BEFORE compilation Only static content

JSP – Implicit objects n n In JSPs, a couple of objects are allways

JSP – Implicit objects n n In JSPs, a couple of objects are allways available request q q n Response q q n Subclass of Servlet. Request get. Parameter(), get. Parameter. Names() get. Attribute() … Subclass of Servlet. Response Not used in normal situations page. Context q q Used to get configuration variables for this page get. Attribute()

JSP – Implicit objects n n n session q The Http. Session q Created

JSP – Implicit objects n n n session q The Http. Session q Created automatically if session is not false in the page directive application q javax. servlet. Servlet. Context q Used to get information about the application out q Used to print (in scriptlets) q clear(), clear. Buffer(), flush() config q javax. servlet. Servlet. Config q get. Init. Parameter(), get. Init. Parameter. Names() page exception

JSP – Implicit objects n exception q q q Only in error pages get.

JSP – Implicit objects n exception q q q Only in error pages get. Message() print. Stack. Trace()

Java Code n n Three ways to use Java Code in your JSP <%!

Java Code n n Three ways to use Java Code in your JSP <%! Declaration %> q q n Declares member variables NOT thread safe <%= My. Class. print. Message() %> q Used to print single expressions

Java Code <% any java code %> q Can contain the entire application q

Java Code <% any java code %> q Can contain the entire application q Use with care q Can give very hard to read code <% if(status == 1) { %> <h 3>Yo</h 3> <%} Else{ %> <h 3>Oy</h 3> <% } %> n

JSP Tags n n n There a couple of tags available and you can

JSP Tags n n n There a couple of tags available and you can extend the tag set with your own tags XML Syntax Doesn’t distract HTML authoring tools

Forward n n <jsp: forward page=“relative URL” /> Forwards the request

Forward n n <jsp: forward page=“relative URL” /> Forwards the request

Include n n n <jsp: include page=“relative url” flusth=“true/false” /> Includes the output in

Include n n n <jsp: include page=“relative url” flusth=“true/false” /> Includes the output in runtime Can be dynamic content

Use. Bean <jsp: use. Bean id=“instance name” scope=“page|request|session|application” bean. Name=“package. class”|class=“package. class” /> n

Use. Bean <jsp: use. Bean id=“instance name” scope=“page|request|session|application” bean. Name=“package. class”|class=“package. class” /> n Used to initialize a bean. Only called once <jsp: use. Bean …> <h 4>The bean is created</h 4> </jsp: use. Bean> n

Get. Property n n Used to call get. XXX methods in a bean <jsp:

Get. Property n n Used to call get. XXX methods in a bean <jsp: get. Property name=“bean instance name” property=“name” /> q q Will call bean. get. Name() Name is the same as id in <jsp: use. Bean>

Set. Property n n n Used to call set. XXX methods in a bean

Set. Property n n n Used to call set. XXX methods in a bean <jsp: set. Property name=“instance name” property=“*|propertyname” value=“value” /> When * is used as property name, a set. XXX for value in get. Parameter. Names() will be called

Example n n package test 1; class Test. Bean{ private String name; private String

Example n n package test 1; class Test. Bean{ private String name; private String surname; public String get. Name(){ return name; } public String get. Surname(){ return surname; } public void set. Surname(String _surname){ surname=_surname; } public void set. Name(String name=_name){ name=_name; } n n n n }

Example cont. <%@ page languea=“java” session=“true” %> <jsp: use. Bean id=“tb” class=“test 1. Test.

Example cont. <%@ page languea=“java” session=“true” %> <jsp: use. Bean id=“tb” class=“test 1. Test. Bean” scope=“session”> <i>The bean is created </i> <jsp: get. Property name=“tb” property=“name” /> <jsp: set. Property name=“tb” property=“name” value=“Fredrik” />