A Brief Introduction to Java Server Page
K. Yue April 2001
Introduction
- Comparable to ASP. Allows embedding Java code inside HTML contents.
- Used to be called 'Page Compilation.'
- JRun supports Java Servlet 2.2 and JSP 1.1.
- Java Server Web Development Kit from Sun supports JSP 1.1.
- JSP files end with the extension .jsp.
- Use XML tags.
Architecture
- A JSP file request is usually handled by the JSP-compliant server the first
time it is accessed since last modification as below:
- A special servlet (such as JSPServlet in WebLogic) should have been configured
to handle all .jsp file.
- If there is no compilation error, the .jsp file is compiled to a servlet
class file (for example, in WebLogic, hello.jsp is compiled to _hello.class,
which is stored in a special directory). The servlet class file implements
javax.servlet.jsp.JspPage.
- The generated JspPage servlet class is invoked.
JSP Core Syntax
- Page directives are used to define attributes that apply to an entire JSP
page.
Example: HelloWorld.jsp
<%@ page info="A Hello World JSP Page" %>
<% for (int i=0; i<10; i++) {
out.println("Hello, World.");
}
%>
- Scriplets contain code fragments valid in the page scripting language.
- Include directives are used to include a file of text or code in the JSP
source file.
Example: IncludeIncorrect.jsp
<%@ page info="A Simple Hello World Page"
contentType="text/html;charset=ISO-8859-1"
%>
<BODY BGCOLOR="#CCCCFF">
<%@ include file="HelloWorld.jsp" %>
</BODY>
Note that this may cause trouble in some JSP server as the page directive is
now duplicated.
-
Regular comments (sent to the client): <!-- and -->.
-
Hidden comments (not sent to the client): <%-- and --%>
Example: ParameterSnoop.jsp
<%@ page import = "java.util.*" %>
<%-- The directive import allows importing Java packages. --%>
<%
out.println("Request Parameters:");
Enumeration enum = request.getParameterNames();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
String values[] = request.getParameterValues(name);
if (values != null) {
for (int i = 0; i <
values.length; i++) {
out.println(name
+ " (" + i + "): " + values[i]);
}
}
}
%>
-
Declaration directives variables or methods valid in the page scripting
language.
-
Expression directives contains expressions valid in the page scripting
language.
Example: TempCount.jsp
<%@ page isThreadSafe="true"
info="Simple useless non-permanent counter" %>
<%-- Define a static variable count --%>
<%! int count = 0; %>
<BODY BGCOLOR="ccccff">
The number of visitors since this JSP page is loaded:
<%= ++count %>.
</BODY>
- Important built-in objects:
- JspWriter out
-- to write to the browser
- HttpServletRequest request
-- the request object.
- HttpServletResponse response --
the response object.
- PageContext pageContext
-- the page context for this JSP
- HttpSession session
-- the session object for the
client
(if any)
- ServletContext application
-- The servlet (application)
context
- ServletConfig config
-- The ServletConfig for this JSP
- Object page
-- the instance of this page's
implementation
class (i.e.,
'this')
- The built-in objects out and request are used most
frequently.
- Many of these objects (e.g., out, response and request)
are not available within the declaration directives.
Using Java Beans
- Very Basic Steps:
- Create the Java Beans to be used.
- Put the class files of the Java Beans in the appropriate
path. To be safe, that should be in your Java classpath, and not in
your vendor classpath.
- In JRun in voyager, that is the directory youraccount/JRun/Web-INF/classes.
- Use the tag <jsp:useBean> to declare beans to
be used within your jsp programs.
- Use the tags <jsp:setProperty> and <getProperty>
to set and get properties of the Java beans.
- <jsp:useBean>:
- To specify the beans to be used.
- Useful attributes:
- id: bean id (name) to refer to.
- scope: scope of bean existence; values: page,
session, application and request.
- class: initiate the bean from a class.
- Note that the useBean tag can include other tags,
such as setProperty.
- <jsp:setProperty>
- to set a property of the named bean.
- Useful attributes:
- name: bean id.
- property: property name.
- value: property value; may use expression directive
as values.
- param: setting property value from HTTP user
parameters.
- <jsp:getProperty>
- to get a property of the named bean.
- Useful attributes:
- name: bean id.
- property: property name.
- Limitation:
- Only Java Bean, not EJB.
- Cannot get indexed properties.
Example: HelloBean.jsp
<jsp:useBean
id="hello" class="temp.HelloBean" />
<BODY BGCOLOR=#CCCCFF>
Hello,
<SPAN STYLE="COLOR:BLUE"><%= hello.getMessage() %></SPAN>.
</BODY>
temp/HelloBean.java (an 'ugly' version)
package temp;
public class HelloBean {
private String message = "You are the sunshine of my
life.";
public String getMessage() {
return message;
}
}