04/18/2001 1 JavaServer Page Presented by: Hongmei Yu 04/18/2001
Jan 21, 2016
04/18/2001 1
JavaServer Page
Presented by: Hongmei Yu
04/18/2001
04/18/2001 2
JavaServer Page
• What is a JavaServer Page?
• Why to Use JavaServer Page
• JSP and JavaBeans
• JSP Session
• Two Basic Ways of Using JSP Tecnology
• Final words
04/18/2001 3
Introduction to JavaServer Page
• JavaServer Pages are text files that combine standard HTML and new scripting tags.
• JSPs are look like HTML, but they get compiled into Java servlets the first time they are invoked.
• The resulting servlet is a combination of the HTML from the JSP file and embedded dynamic content specified by the new tags.
04/18/2001 4
What is a JavaServer Page
• Definition: JSP is a dynamic capability for web pages that allows java as well as a few special tags to be embedded into a web file(HTML/XML, etc). The suffix traditionally ends with .jsp to indicate to the web server that the file is a JSP file. JSP is a server side technology.
04/18/2001 5
A simple Example
<HTML><HEAD><TITLE>Simple JSP Example</TITLE></HEAD>
<BODY>
<P>How many times?</P>
<FORM METHOD="GET" ACTION="SimpleJSP.jsp"><INPUT TYPE="TEXT" SIZE="2" NAME="numtimes"><INPUT TYPE="SUBMIT"></FORM>
</BODY></HTML>
The main HTML file(SimpleJSP.html)
04/18/2001 6
A simple Example
<%@ page language="java" %><HTML><HEAD><TITLE>Simple JSP Example</TITLE></HEAD>
<BODY><P> <% int numTimes = Integer.parseInt(request.getParameter("numtimes")); for (int i = 0; i < numTimes; i++) { %> CSI 668!<BR> <% } %></P><%@ include file="PageFooter.html" %></BODY></HTML>
The response file(SimpleJSP.jsp)
04/18/2001 7
• JSP directives: Sets attributes for the page 1.page directive --defines a number of important attributes that affect the whole page.
< %@ page Attributes %> 2.include directive --notifies the container to include the content of the resource in the current JSP.The included file is parsed by the
JSP and this happens only at translation time.
<%@ include file=“FILENAME” %> 3.taglib directive --Allows the page to use custom user defined tags.
<%@ taglib uri=“tagLibraryURI” prefix=“tagPrefix” %>
04/18/2001 8
• Scripting Elements—are used to include scripting code (normally java code) within the JSP. 1.Declarations <%! Java variable and method declaration(s) %> 2.Scriptlets <% Java code statement%> 3.Expressions <%= Java expression to be evaluated %>
04/18/2001 9
• Standard Actions affect the runtime behavior of the JSP affect the response sent back to the client
1. <jsp:useBean> 2. <jsp:setProperty> 3. <jsp:getProperty> 4.<jsp:param> 5.<jsp:include> 6.<jsp:forward> 7.<jsp:plugin>
04/18/2001 10
Browser Server
User enters value into form and clicks submit button
Response displayed in browser window
Interprets JSP and usesdata from form to generate response
Time
Sends requests to serverFor JSP page, includingData from form
Sends response to browserContaining HTML code
04/18/2001 11
04/18/2001 12
Comparison with Existing Technologies
CGI(Common Gateway Interface)
– The simplest and oldest way to use an HTTP
request to control the HTTP output of a server-side application.
– Required loading/unloading of application each time.
– Used interpreted languages usually(PERL, C++, or Python).
By comparison:
1.JSP can maintain state on the server between requests(since it can use Servlet sessions).
2.Spawns a new thread for each request.
3.Does not have to be loaded each time, once it has been initiated.
4.Runs in a ready-loaded JVM as an extension to the web server.
04/18/2001 13
Comparison with Existing Technologies
Web Server APIs
--Many web servers have their own built-in APIs for creating dynamic content.
ISAPI for Microsoft IIS
NSAPI for Netscape web servers
--When using a server API, the code is written specifically for that web server and particular platform.
04/18/2001 14
Comparison with Existing Technologies
Client_Side Scripting(JavaScript, Jscript, VBScript)
Vary interpretations by different browser versions.
A user can also decide to disable scripting altogether.
Since JSP runs on the server as such, the browser is not an issue(unless the JSP products HTML code which contains client side scripting code.)
04/18/2001 15
Comparison with Existing Technologies
Servlets
Servlets provide the ability to build dynamic content for web sites using the Java language , and are supported on all major web server platforms.
The JSP specification is built on top of the Java Servlet API.Anything that can be done with a JSP can be done with a Servlet.
JSPs provide a much cleaner separation of presentation from logic, and are simpler to write.
Servlets and JSP are work well together.
04/18/2001 16
JSP ASP
Comparison with Existing Technologies
ASP
Platforms All major web platforms Microsoft only
Base Language Java Jscript or VBScript
Components JSP Tags, JavaBeans, or COM/DCOM
Enterprise JavaBeans
Code Interpretation Once Each instance
04/18/2001 17
Comparison with Existing Technologies
JSPs score over ASP in that:
• JSPs are interpreted only once, to Java byte-code, and re-interpreted only when the file is modified.
• JSPs run on all the main web servers
• JSPs provide better facilities for separation of page code and template data by means of JavaBeans, Enterprise JavaBeans and custom tag libraries.
ASP
04/18/2001 18
Why to use JavaServer Page
• Easy and Rapid Web Development,and Easy and Rapid Web Development,and MaintenanceMaintenance
Emphasizing Reusable ComponentsEmphasizing Reusable Components
Separating Content Generation from Separating Content Generation from PresentationPresentation
Platform IndependencePlatform Independence
Simplifying Page Development with TagsSimplifying Page Development with Tags
04/18/2001 19
JSP and JavaBeansThe JavaBeans specification allows software components to be written in Java. This means reusable
bean classes can be defined, greatly reducing future maintaince requirements.A property of a JavaBean is simply the data (state) of the bean. Properties are accessible by two
methods: the getter and the setter. The value of the property is accessed by the getter method. If the property is writeable, its value is changed by the setter method.
Loading a Bean - <jsp:usebean>
Initializing a Bean - <jsp:setProperty>
Displaying Dynamic Content - <jsp:getProperty>
04/18/2001 20
JSP and JavaBeansJSP:useBean action is used to associate a JavaBean with the JSP.
<jsp: useBean id=“name” scope=“page| request| session| application”
beandetails/>
class=“className”
Class=“className” type=“typeName”
Type=“typeName”
Id---the case sensitive name used to identify the object instace.
Scope—The scope within which the reference is available. The default value is page.
04/18/2001 21
JSP and JavaBeansJsp:setProperty
-----is used in conjunction with the useBean action described in the preceding section,
and sets the value of simple and indexed properties in a bean.
<jsp:setProperty name=“ beanName “ propertydetails />
name--- The name of a bean instance defined by a <jsp:bean> tag
property—The name of the bean property whose value is being set.
04/18/2001 22
JSP and JavaBeansjsp: getProperty
---is complementary to the jsp:setProperty action and is used to access the properties of a bean. It access the value of a property, converts it to a String, and prints it to the output stream.
<jsp:getProperty name=“ name” property=“ propertyName” />
name--- The name of the bean instance from which the property is obtained.
property--- The name of the property to retrieve.
04/18/2001 23
A example for using JavaBeanAn HTML form(which obtains input from the user to be processed by our JSP) ,SetProTest.html
<html>
<head>
<title>A example </title>
</head>
<body bgcolor="#FFFFFF">
<form action="wordpro1.jsp" method="POST">
Enter word:
<input type="text" name="word">
<select name="mode">
<option value="1" selected>Reverse</option>
<option value="2" selected>SpellCheck</option>
</select>
<input type="submit" name="Go" value="Submit">
</form>
</body>
</html>
04/18/2001 24
A example for using JavaBean
The main JSP file(wordpro1.jsp)
<jsp:useBean id="help" scope="session" class="SpellCheck" />
<jsp:setProperty name="help" property="*"/>
<html>
<body>
You entered the input, <b>
<jsp:getProperty name=“help” property=“word”/> </b><br>
The processed output is :<br>
<%= Integer.parseInt(request.getParameter("mode"))==1 ? help.reverse() :""+help.check() %>
</body>
</html>
04/18/2001 25
A example for using JavaBean
The bean(SpellCheck.java)/*** This bean encapsulatesthe functionality to spell check a string
*/
public class SpellCheck {
private String word;
public SpellCheck() {}
/**
* Method to reverse the string uses
* @return the reversed string
*/
public String reverse(){
return (new StringBuffer(word).reverse()).toString();
}
04/18/2001 26
/**
* Check the spelling of the word. This method has ano body, and just
* returns true for the example
* @return boolean, true if the spelling is right
*/
public boolean check(){
return true;
}
/**
* Access method for the word property
* @return the current value of the word property
*/
public String getWord() {
return word;
}
A example for using JavaBean
The bean continue(SpellCheck.java)
04/18/2001 27
/**
* Sets the value of the word property
* @param aWord the new value of the word property
*/
public void setWord(String aWord) {
word = aWord;
}
}
A example for using JavaBean
The bean continue(SpellCheck.java)
04/18/2001 28
What’s a session?
A session can be defined as a series of related interactions between a single client and server, which take place over a period of time.
JSP Session
For example, consider a common three-step user interaction with a web site:
1.The user browses catalog on a web site, and chooses something from it to putting the shopping basket.
2. The user continues to use other tools and utilities on the site.
3.The user decides to complete the order selected in the first step.
04/18/2001 29
JSP Session
Catalog
Order
Transfer the selected items to the order at some point during the session
Time
Order and pay for items
HTTP connection
Select items from catalog
Browsing
04/18/2001 30
JSP Session
Session Lifecycle
It is created on the server as a result of a request and assigned a unique session ID and this ID is then passed to the client.
The session itself, is however considered ‘new’ until the client return the session ID to the server indicating that a session has been established. This associated the client with that particular session object.
A session exits on the server until it is invalidated(client logout, for example) or the server is stopped.
04/18/2001 31
A example of JSP Session
The first page(p1.jsp)
<HTML>
<BODY>
<FORM METHOD=POST ACTION="p2.jsp">
Please input your name:
<INPUT TYPE=TEXT NAME="thename">
<INPUT TYPE=SUBMIT VALUE="SUBMIT">
</FORM>
</BODY>
</HTML>
04/18/2001 32
A example of JSP Session
The second page(p2.jsp---1)
<HTML>
<BODY>
<%@ page language="java" %>
<%! String name=""; %>
<%
name = request.getParameter("thename");
session.putValue("thename", name);
%>
Your name is: <%= name%>
<p>
04/18/2001 33
<FORM METHOD=POST ACTION="p3.jsp">
What's the topic you like ?
<INPUT TYPE=TEXT NAME="topic">
<P>
<INPUT TYPE=SUBMIT VALUE="SUBMIT">
</FORM>
</BODY>
</HTML>
A example of JSP Session
The second page(p2.jsp—2)
04/18/2001 34
A example of JSP Session
The third page(p3.page)
<HTML>
<BODY>
<%@ page language="java" %>
<%! String topic=""; %>
<%
topic = request.getParameter("topic");
String name = (String) session.getValue("thename");
%>
Your name is : <%= name%>
<P>
Your topic is: <%= topic%>
</BODY>
</HTML>
04/18/2001 35
Two Basic Ways of Using JSP TechnologyThe Page-Centric Approach
This model allows JSPs or Servlets direct access to some resource like a database or legacy application to service a client’s request. The JSP page is where the incoming request is intercepted processed, and the response sent back to the client.
04/18/2001 36
Two Basic Ways of Using JSP Technology
The Page-Centric ApproachPage-View
JSP Business Processing
RequestResponse
Page-View With Bean
JSP Business Processing
Bean
Request
Response
04/18/2001 37
Two Basic Ways of Using JSP Technology
The Page-Centric Approach
Advantage:
It is simple to program, and allows the page author to generate dynamic content easily, based upon the request and the state of the resources.
Disadvantage: separation of presentation from content
not be suitable for complex implementation
Does not scale up well for a large number of simultaneous clients
04/18/2001 38
Two Basic Ways of Using JSP TechnologyThe “Dispatcher” Approach(or “N-tiered” approach)
------A SERVLET( or JSP) acts as a mediator or controller, delegating requests to JSP pages and JavaBeans.
The application is composed of multiple tiers, the JSP, interacts with the back end resource via another object or Enterprise JavaBeans component. The Enterprise JavaBeans server and the EJB provide managed access to resources, support transactions and access to underlying security mechanisms, thus addressing the resource sharing .
04/18/2001 39
Two Basic Ways of Using JSP Technology
The Steps in “N-tiered” application design:
Step1—identify the correct objects and their interaction.
Step2—identify the JSPs and Servlets. These should be divided into two categories, depending on the role they play.
---Front end JSPs or Servlets that manage application flow and business logic evaluation. This is where a lot of method invocation on the objects, or usage of EJBs, can be coded. They are not responsible for any presentation, and act as a point to intercept the HTTP requests coming from users.
---Presentation JSPs that generate HTML or XML, with their main purpose in life being presentation of dynamic content.
The figure below shows the relationship between these two categories.
04/18/2001 40
Two Basic Ways of Using JSP Technology
The front-end component accepts a request, and then determines the appropriate presentation component to forward it to. The presentation component then processes the request and returns the response to another front component or directly back to the user.
The “Dispatcher” Approach
04/18/2001 41
Final Words
• Some useful links
http://java.sun.com/products/jsp.
http://archives.java.sun.com/archives/jsp-interest.html
http://www.flashline.com/components/appservermatrix.jsp
http://www.flashline.com/
http://www.taglib.com/ /com
04/18/2001 42
Thank you !