java server pages an introduction to jsp. containers and components several clients – one system
TRANSCRIPT
![Page 1: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/1.jpg)
Java Server Pages
An introduction to JSP
![Page 2: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/2.jpg)
Containers and Components
Several clients – one system
![Page 3: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/3.jpg)
Java Server Pages (JSP)
HTML with embedded Java code Good for presentation
No print statements Compiled to a Servlet at runtime
Good performance Easy deployment
When a JSP is updated it’s recompiled Possible to compile manually
Easy for web designers to use Edited in an ordinary HTML editor
Support for Java Beans and Custom tags
![Page 4: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/4.jpg)
JSP – Directives
At the top of a JSP page, the page directive is used <%@ page name=“value” %>
![Page 5: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/5.jpg)
JSP – Page Directive parameters language
Default is java extends
Default is the base JSP class, but you could use your own
import=“package1,package2, class1, class2” session
True or False, determines if sessions should be automatically created. True is default
![Page 6: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/6.jpg)
JSP – Page Directive parameters buffer
The output buffer size autoFlush
True or false, determines if the output should be flushed to the client. True is default
isThreadSafe True or false. True is default
info Descriptive text
![Page 7: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/7.jpg)
JSP – Page Directive parameters errorPage
The page to go to in case of an error contentType isErrorPage
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
![Page 8: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/8.jpg)
Other Directives
Include directive <%@ include file=“relative url” %> Includes the file BEFORE compilation Only static content
![Page 9: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/9.jpg)
JSP – Implicit objects
In JSPs, a couple of objects are allways available request
Subclass of ServletRequest getParameter(), getParameterNames() getAttribute() …
Response Subclass of ServletResponse Not used in normal situations
pageContext Used to get configuration variables for this page getAttribute()
![Page 10: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/10.jpg)
JSP – Implicit objects
session The HttpSession Created automatically if session is not false in the page directive
application javax.servlet.ServletContext Used to get information about the application
out Used to print (in scriptlets) clear(), clearBuffer(), flush()
config javax.servlet.ServletConfig getInitParameter(), getInitParameterNames()
page exception
![Page 11: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/11.jpg)
JSP – Implicit objects
exception Only in error pages getMessage() printStackTrace()
![Page 12: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/12.jpg)
Java Code
Three ways to use Java Code in your JSP <%! Declaration %>
Declares member variables NOT thread safe
<%= MyClass.printMessage() %> Used to print single expressions
![Page 13: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/13.jpg)
Java Code
<% any java code %> Can contain the entire application Use with care Can give very hard to read code
<% if(status == 1){
%><h3>Yo</h3>
<%}Else{%>
<h3>Oy</h3><% } %>
![Page 14: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/14.jpg)
JSP Tags
There are a couple of tags available and you can extend the tag set with your own tags
XML Syntax Doesn’t distract HTML authoring tools
![Page 15: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/15.jpg)
Forward
<jsp:forward page=“relative URL” /> Forwards the request
![Page 16: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/16.jpg)
Include
<jsp:include page=“relative url” flusth=“true/false” />
Includes the output in runtime Can be dynamic content
![Page 17: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/17.jpg)
UseBean
<jsp:useBean id=“instance name” scope=“page|request|session|application” beanName=“package.class”|class=“package.class” />
Used to initialize a bean. Only called once
<jsp:useBean …>
<h4>The bean is created</h4>
</jsp:useBean>
![Page 18: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/18.jpg)
GetProperty
Used to call getXXX methods in a bean <jsp:getProperty name=“bean instance
name” property=“name” /> Will call bean.getName() Name is the same as id in <jsp:useBean>
![Page 19: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/19.jpg)
SetProperty
Used to call setXXX methods in a bean <jsp:setProperty name=“instance name”
property=“*|propertyname” value=“value” /> When * is used as property name, a setXXX
for value in getParameterNames() will be called
![Page 20: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/20.jpg)
Example
package test1; class TestBean{ private String name; private String surname;
public String getName(){ return name; } public String getSurname(){ return surname; } public void setSurname(String _surname){ surname=_surname; } public void setName(String name=_name){ name=_name; } }
![Page 21: Java Server Pages An introduction to JSP. Containers and Components Several clients – one system](https://reader036.vdocuments.net/reader036/viewer/2022070403/56649f295503460f94c423e4/html5/thumbnails/21.jpg)
Example cont.
<%@ page languea=“java” session=“true” %>
<jsp:useBean id=“tb” class=“test1.TestBean” scope=“session”>
<i>The bean is created </i>
<jsp:getProperty name=“tb” property=“name” />
<jsp:setProperty name=“tb” property=“name” value=“Fredrik” />