Chapter 7 Being a JSP

Download Report

Transcript Chapter 7 Being a JSP

Chapter 7 Being a JSP
JSP introduction
JSP is a solution for two issues
Servlet is difficult for HTML designers since
they may not know Java
Formatting HTML into a String literal is really
ugly
In the end, JSP is just a servlet
 Your JSP eventually becomes a full-fledged servlet
running in your web app
 It is a lot like any other servlet, except that the servlet
class is written for you-by the Container
 The Container takes what you’ve written in your JSP,
translates it into a servlet class source (.java) file, the
compile that into a Java servlet class
 Also, if you changes JSP files and redeploy them to
Tomcat, you do not have to restart tomcat in order to
make the changes to be effective
Container translates JSP into Servlet
This is the servlet tranlated
from MyJSP.jsp
JSP
Pauline wants to use JSPs in her web
apps
She understands that you can put regular
old Java code in a JSP using a scriptlet
which just means Java code within a
<%…%> tag
class MyClass {
float aFloat;
}
Instance variable
class MyClass {
static float aFloat;
}
Class variable
Instance variable is created for each instance of a class, the
runtime system allocates class variables once per class
regardless of the number of instances created of that class.
JSP
When she deploys it and
runs it, she got an
exception page
Import packages
You can put import statement in a
JSP…you just need a directive
You may use the page directive to import
packages
A directive is a way for you to give special
instructions to the Container at page
translation time
Directives come in three flavors: page, include,
and taglib
Import packages
You can import a single package
E.g., <%@ page import="foo.*" %>
You can also import multiple packages
 E.g., <%@ page import="foo.*,java.util.*" %>
Expression element
Part of the whole point of JSP is to avoid
println()!
JSP expression element automatically prints
out whatever you put between the tags
Please remember that there is no semicolon
when using expression element
E.g. <%= Counter.getCount() %>
Expression element
The container takes everything you type
between the <%= and %> and puts it in as
the argument to a statement that prints to
the implicit response PrintWriter out.
When the Container sees this:
<%= Counter.getCount() %>
It turns it into this
out.print(Counter.getCount());
So far we’ve seen three element
types
Scriptlet: <% %>
Directive: <%@ %>
Expression: <%= %>
Question
Declaring a variable in a scriptlet
The variable declaration is legal, but it
does not quite work the way we want. The
same page is displayed no matter how
many times the page is hit
All scriptlet and expression code lands in a service
method.That means varaibles declared in a
scriptlet are always LOCAL variables.
Declaration
There is another JSP element called a
declaration
Anything between the <%! and %> tag is
added to the class outside the service
method
Time to see real generated servlet
 The real generated servlet is put under a directory of
tomcat
 E.g., I have a counter.jsp file under the directory
C:\tomcat-6.0.26\webapps\ct
The generated servlet file is located at:
 C:\tomcat-6.0.26\work\Catalina\localhost\counter\org\apache\jsp\counter_jsp.java
 When you look at the generated servlet code, you did not
see doGet() or doPost() methods there. Instead, you see a
service method called _jspService()
 This is because this _jspService() is called by the servlet superclass’s
overridden service() method
Implicit object
With implicit objects, you can write a JSP knowing that your
code is going to be part of a servlet
Implicit object
 For example, you can put the following scriptlet
into your JSP:
<%
String[] picked = request.getParameterValues(“fruits”);
……..
%>
This extract the parameter fruits that reflected what the
user selected in a group of checkboxes in a HTML page