Title: JavaServer Page
1JavaServer Page
2Overview
- Introduction
- What is a servlet?
- What can servlets do?
- Servlets Vs JSP
- Syntax
- Samples
- JavaBean
- Tag Library
- Conclusion
3Introduction
- Java Server Pages (JSP) is basically Sun's answer
to Microsoft's Active Server Pages (ASP). - Advantages over other technologies
- It is in Java
- No tied to a particular server product
- JSP is actually based on Java Servlet
4What is Servlet
- Javas answer to the Common Gateway Interface
(CGI). - Applet a java program that runs within the web
browser. - Servlet a java program that runs within the web
server.
5What can Servlets do
- Search Engines
- Personalization Systems
- E-Commerce Applications
- Shopping Carts
- Product Catalogs
- Intranet Applications
- Groupware Applications bulletin boards, file
sharing, etc.
6Servlets vs JSP
- Servlets
- code looks like a regular Java program.
- JSP
- embed Java commands directly within HTML
- Lets examine a Servlet program next to a JSP
program - Each of these prints, Hello, World!
7- import java.io.
- import javax.servlet.
- import javax.servlet.http.
- public class HelloWorld extends HttpServlet
- public void doGet(HttpServletRequest req,
HttpServletResponse res) - throws ServletException, IOException
- res.setContentType("text/html")
- PrintWriter out res.getWriter()
- out.println("ltHTMLgt")
- out.println("ltHEADgtltTITLEgtHello
Worldlt/TITLEgtlt/HEADgt") - out.println("ltBODYgt")
- out.println("ltBIGgtHello Worldlt/BIGgt")
- out.println("lt/BODYgtlt/HTMLgt")
-
-
8- lthtmlgt
- ltheadgt
- lttitlegtHello, World JSP Examplelt/titlegt
- lt/headgt
- ltbodygt
- lth2gt Hello, World!
- The current time in milliseconds is
- lt System.currentTimeMillis() gt
- lt/h2gt
- lt/bodygt
- lt/htmlgt
9Syntax
- Three main types of JSP constructs embed in a
HTML page - Scripting elements
- Directives
- actions
10Scripting Element
- Three forms available
- lt expression gt, for output
- lt code gt, for a block of Java code
- lt! code gt, for declaration
11Directives
- A JSP directive affects the overall structure of
the servlet that results from the JSP page. - Syntax lt_at_ directive attribute value gt
- Three types of directives
- page
- include
- taglib
12Action
- JSP actions are XML tags that invoke built-in web
server functionality. - e.g.
ltjspsetProperty name myBean property
message value This is my message /gt
13Sample1
lthtmlgt ltheadgt lttitlegtUntitled Documentlt/titlegt lt/h
eadgt ltbodygt lt "hello world"gt lt/bodygt lt/htmlgt
14Sample2
lthtmlgt ltheadgt lttitlegtUntitled Documentlt/titlegt lt/h
eadgt ltbodygt lt if(Math.random()lt0.5) gt Have a
ltBRgtniceltBRgtday lt else gt Have a
ltBRgtlousyltBRgtday ltgt lt/bodygt lt/htmlgt
15Sample 3
lt_at_ page import"java.util." gt ltHTMLgt ltBODYgt lt!
Date theDate new Date() Date
getDate() System.out.println( "In
getDate() method" ) return theDate
gt Hello! The time is now lt getDate()
gt lt/BODYgt lt/HTMLgt
16JavaBean
- An object that holds data with setter and getter
methods. - Can be used to store data through out the session.
public class SimpleBean private String
message "No message specified" public String
getMessage() return(message) public
void setMessage(String message)
this.message message
17Tag Library
- One of the features of JSP
- Simplify complex server-side behavior into simple
elements - Creates custom JSP tags into a library.
- Each tag library has a tag library descriptor
- TLD describes each tag information
- Hide underlying implementation
18Tag Example
lt!TagExample --gtlthtmlgt ltheadgt lttitlegtGet Name
and Course with DropList taglt/titlegt lt/headgt ltbody
gt lt_at_ taglib uri"mytags" prefix "mytag"
gt ltmytagtagExample name "Joe " lname"Doe"
/gt lt/bodygt lt/htmlgt
19- // This is myTagExample.java
- package tony
- import javax.servlet.jsp.JspException
- import javax.servlet.jsp.tagext.SimpleTagSupport
- import java.io.IOException
- /
- SimpleTag handler that prints "Hello, world!"
- /
- public class myTagExample.java extends
SimpleTagSupport - protected String name""
- protected String lastName""
- public void doTag() throws JspException,
IOException - getJspContext().getOut().write(name
" Hello world lastName) -
- public void setName(String name)
- this.name name
-
- public void setLname(String lname)
- this.lastName lname
20Tag Library Descriptor
- lttaggt
- ltnamegttagExamplelt/namegt
- lttag-classgttony.myTagExample lt/tag-classgt
- ltbody-contentgtEMPTYlt/body-contentgt
- ltdescriptiongt
- perform
- lt/descriptiongt
- ltattributegt
- ltnamegtnamelt/namegt
- ltrequiredgttruelt/requiredgt
- lt/attributegt
- ltattributegt
- ltnamegtlnamelt/namegt
- ltrequiredgttruelt/requiredgt
- lt/attributegt
- lt/taggt
21Conclusion