Transcript config page
Q: According to Intel, the Pentium
conforms to the IEEE standards 754
and 854 for floating point arithmetic.
If you fly in aircraft designed using a
Pentium, what is the correct
pronunciation of "IEEE"?
A: Aaaaaaaiiiiiiiiieeeeeeeeeeeee!
Advanced Java Class
Web Applications – part 2
(JSPs, etc.)
JSPs (Java Server Pages)
•
What are they?
– When you’re editing it: HTML that contains
java code
– After it’s compiled: a Servlet
– What the client computer receives: just
HTML
JSPs (Java Server Pages)
• Functionality
– The java part is pre-processed on the server
side, so the client never knows the difference.
– Functionality is interchangeable with servlets
– By the time they reach the user, they are
HTML, so JavaScript & applets still have a
place in enhancing user experience.
Advantages of using a combination
of JSPs & servlets
•
can use WYSIWYG editor, and write
HTML directly.
•
nonprogrammers can write the HTML
parts of a JSP page
•
cleaner than using either alone
JSPs and MVC Layers
•
Ideally, the View layer is accomplished by
passing a JavaBean to the JSP, and then the
only java code in the JSP is the display code of
showing those values.
•
The Controller layer is then left to the Servlet
and it’s helper classes
•
And the Model layer is implemented by other
class that are also on the server side.
•
See figure 13-12 on page 858.
JSP tags & API (fig. 13-13)
•
Ultimate Goal: limit actual Java code in
JSPs, so that people who don’t know
how to program can edit and create the
view layer of the application (i.e. graphic
designers)
– How to do this:
•
Javabean tags, custom tag libraries
– Okay compromises:
•
Scriptlets, Expressions
JSP as HTML or XHTML
•
Comments: <%-- comment --%>
•
Directives: <%@ … %>
–
–
Page ex.: <%@ page import= “java.util.*” %>
Include ex:
<%@ include file= “global_header.html” %>
•
Declaration ex:
<%! Type fieldname=expression; %>
•
Expression: <%= expression %>
•
Scriptlet: <% for (…) {…} …int i=5;…%>
JSPs as XML Documents
•
Benefits:
– can validate with dtd spec files
– Can create and edit with code
•
Bad effects:
– Appear a little more complex
– A little longer
JSPs as XML Documents – Syntax
(fig. 13-15)
•
•
•
•
•
•
•
•
<jsp:root>…</jsp:root>
<jsp:directive.page import=”value” />
<jsp:directive.include file=”filename” />
<jsp:text>….</jsp:text>
<jsp:declaration>…</jsp:declaration>
<jsp:expression>…</jsp:expression>
<jsp:scriptlet>…</jsp:scriptlet>
<jsp:forward page=”URN”/>
JSP as XML Syntax, continued
• beans
– <jsp:plugin type=”bean | applet”…/>
– <jsp:usebean id=”instance” scope=”page | request |
session | application” type=”className”>
– <jsp:getProperty name=”bean” property=”field” />
– <jsp:setProperty name=”bean” property=”field”
value=”value”>
• custom libraries
– <tagPrefix:customtag attribute=value… />
Server Processing
1.
2.
3.
HTML, XHTML, or XML page -> java source file that
defines a Servlet class
java class compiled to byte code
run the resulting servlet
Note: If you change a jsp, the web server will automatically
recompile it and show it to you when you reload the
page.
In other words, you don’t have to restart tomcat if you
change a jsp.
(You do have to restart tomcat if you recompile java code.)
Predefined Variables in JSP
•
•
•
•
•
•
•
•
request (argument to doGet/doPost)
response (argument to doGet/doPost)
pageContext
session (request.getSession())
application (getServletConfig().getContext())
out (PriterWriter that writes to response
outputStream)
config
page (like “this”)
JSP tags for JavaBeans
•
Good for taking code out of the view layer, so
that non-programmers can create/edit the view
layer
•
Three relevant tags:
–
–
–
•
Usebean – will try to look up in scope, or will
instantiate. Class must have a no-args constructor
getProperty, setProperty – will access a get/set
method with the corresponding name – make sure
it’s there
these match the XML syntax, but can also be used
in HTML/XHTML
See Chapter 8 for more details (not covered).
Custom Tags – a mere intro
•
The Ultimate Step in taking code out of the jsp page
•
You can define your own tags that define more interesting
functionality, such as for loops, if statements, & other code blocks.
•
They can have attributes that you use to pass variables into the
scope of the custom tag
•
Declaring a library:
–
–
<%@ taglib uri=”…” prefix=”…” %>
<jsp:root … xmlns:myPrefix=”…” …>..</jsp:root>
•
Syntax for using a tag: <libraryPrefix:tagName … />
•
Some predefined libraries:
–
–
Sun Microsystems JSP Standard Tag Library (JSTL)
Jakarta Taglibs is a free implementation of JSTL –
http://jakarta.apache.org/taglibs
Building Robust Web Apps
•
Make Servlets thread-safe
•
Allow for the fact that users do weird
things
•
Use relative addresses within your app.
Multithreading
•
There is one instance of the Servlet running in
the web app, but it is being accessed by a
different thread for each client
•
Your variables must be thread-safe
–
–
–
Don’t use fields of servlet as working storage – use
session or request scope instead, because each
client has a separate session and request
local variables are thread-safe
if only one client at a time should run a method,
declare it synchronized
Users Can...
•
press BACK and FORWARD buttons,
rather than using your carefully created
navigation buttons
•
bookmark a page in the middle of your
site and go straight there, instead of via
the flow you designed
•
get impatient and click “submit” many
times.
What you can do about what users
can do...
•
Turn off caching for pages with dynamic
content.
•
Check for validity of needed session
attributes, and redirect to login if they
aren’t there.
•
Disable buttons (using JavaScript) as
soon as they are pressed the first time.
Use Relative Addresses
If you use relative addresses within web
pages, you can switch:
–
–
–
–
protocols
hosts
domain names
ports
Without having to change your internal links!