Documente Academic
Documente Profesional
Documente Cultură
CS-422
What are JSPs
• A logical evolution of java servlets
– most servlets dynamically create HTML and integrate it with some
computational and database output
• Servlets on their own had some pretty stiff competition form PHP,
ASP and ColdFusion
– these technologies for the most part (especially ColdFusion) don’t need a
programmer; ASP and PHP definitely have a programming language in
their background but scripting in VBScript or PHP is still easier than Java
• some one at Sun recognized that a servlet was just some Java with
HTML embedded in it, and thought: why not embed some Java in
regular HTML; then create a class that will scan through it and make
the source code for a servlet, then compile it and deploy it
• The Java Server Page is born
An Example...
<!-- JSP that processes a "get" request containing data. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<!-- head section of document -->
<head>
<title>Processing "get" requests with data</title>
</head>
<!-- body section of document -->
<body>
<% // begin scriptlet
String name = request.getParameter( "firstName" );
if ( name != null ) {
%> <%-- end scriptlet to insert fixed template data --%>
<h1>
Hello <%= name %>, <br />
Welcome to JavaServer Pages!
</h1>
<% // continue scriptlet
} // end if
else {
%> <%-- end scriptlet to insert fixed template data --%>
<form action = "welcome.jsp" method = "get">
<p> Type your first name and press Submit</p>
• taglib
• include
page directive
• Language – “scriptingLanguage”
• Extends – “classname”
• Import – “importList” – java.lang.*, javax.servlet.*,
javax.servlet.jsp.*, and javax.servlet.http.* are imported implicitily
• Session “true|false”
• Buffer – “none|sizekb”
• autoFlush- “true|false”
• isThreadSafe – “true|false”
• Info – “info_text”
• errorPage – “error_URL”
• isErrorPage – “true|false”
• contentType – “contentInfo”
• pageEncoding – “pageEncodingInfo”
taglib directive
• Import a taglib into the page
• Map the URI to,a prefix for use within the page
<jsp:forward page=“nextjsp.jsp”>
<jsp:param name = date value=“<%=java.util.Date() %>” />
</jsp:forward>
<jsp:plugin>
• Adds an applet or JavaBean to a Web page in the form of a
browser=specific object or embed XHTML element. It also allows the
client to download and install the java Plug-in if it is not already
present.
• Attributes:
– type - bean or applet
– code - Class that represents the component
– codebase- location of the class specified in the code attribute and the archives specified in the
archives attribute
– align - Alignment od the component (left,right,center)
– archive - space separated list of archives (JARS) containing resources
– height - component height in pixels
– hspace - number of pixels to right and left of component
– jreversion - version of the JRE required
– name - name of component
– vspace - number of pixels above and below the component
– title - Text describing the component
– width - component width in pixels or percent
– nspluginurl - location of plugin for Netscape Navigator
– iepluginurl - location of plugin for Internet Explorer
<jsp:useBean>
• Enables a JSP to manipulate or use a JavaBean. The action creates or
locates an existing object for use in the JSP.
• Attributes:
– id - the name used to manipulate the Java object (case sensitive)
– scope - page, request,session,or application (default is page)
– class - fully qualified class name of the Java object
– beanName - The name that can be used with method instantiate of class
java.beans.Beab to load a JavaBean into memory.
– Type - type of JavaBean, can be same as class attribute , a superclass of
that type or an interface implemented by that type
<jsp:setProperty>