+ All Categories
Home > Documents > JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can...

JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can...

Date post: 13-Dec-2015
Category:
Upload: hope-ferguson
View: 218 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
JavaServer Page JavaServer Page by Antonio Ko by Antonio Ko
Transcript
Page 1: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

JavaServer PageJavaServer Pageby Antonio Koby Antonio Ko

Page 2: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

OverviewOverview► IntroductionIntroduction►What is a servlet?What is a servlet?►What can servlets do?What can servlets do?►Servlets Vs JSPServlets Vs JSP►SyntaxSyntax►SamplesSamples► JavaBeanJavaBean►Tag LibraryTag Library►ConclusionConclusion

Page 3: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

IntroductionIntroduction

► Java Server Pages (JSP) is basically Java Server Pages (JSP) is basically Sun's answer to Microsoft's Active Sun's answer to Microsoft's Active Server Pages (ASP). Server Pages (ASP).

►Advantages over other technologies:Advantages over other technologies: It is in JavaIt is in Java No tied to a particular server productNo tied to a particular server product

► JSP is actually based on Java ServletJSP is actually based on Java Servlet

Page 4: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

What is ServletWhat is Servlet

► Java’s answer to the Common Java’s answer to the Common Gateway Interface (CGI).Gateway Interface (CGI).

►Applet: a java program that runs Applet: a java program that runs within the web browser.within the web browser.

►Servlet: a java program that runs Servlet: a java program that runs within the web server.within the web server.

Page 5: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

What can Servlets doWhat can Servlets do

Search EnginesSearch Engines Personalization SystemsPersonalization Systems E-Commerce ApplicationsE-Commerce Applications Shopping CartsShopping Carts Product CatalogsProduct Catalogs Intranet ApplicationsIntranet Applications Groupware Applications: bulletin Groupware Applications: bulletin

boards, file sharing, etc.boards, file sharing, etc.

Page 6: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Servlets vs JSPServlets vs JSP

ServletsServlets code looks like a regular Java program.code looks like a regular Java program.

JSPJSP embed Java commands directly within embed Java commands directly within

HTMLHTML Let’s examine a Servlet program next Let’s examine a Servlet program next

to a JSP program…to a JSP program… Each of these prints, “Hello, World!”Each of these prints, “Hello, World!”

Page 7: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

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("<HTML>"); out.println("<HEAD><TITLE>Hello

World</TITLE></HEAD>"); out.println("<BODY>"); out.println("<BIG>Hello World</BIG>"); out.println("</BODY></HTML>"); }}

Page 8: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

<html> <head><title>Hello, World JSP Example</title></head> <body> <h2> Hello, World! The current time in milliseconds is <%= System.currentTimeMillis() %> </h2></body></html>

Page 9: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

SyntaxSyntax

►Three main types of JSP constructs Three main types of JSP constructs embed in a HTML page:embed in a HTML page: Scripting elementsScripting elements DirectivesDirectives actionsactions

Page 10: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Scripting ElementScripting Element

► Three forms available:Three forms available:1.1. <%= expression %>, for output<%= expression %>, for output

2.2. <% code %>, for a block of Java code<% code %>, for a block of Java code

3.3. <%! code %>, for declaration<%! code %>, for declaration

Page 11: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

DirectivesDirectives

► A JSP directive affects the overall A JSP directive affects the overall structure of the servlet that results structure of the servlet that results from the JSP page.from the JSP page.

► Syntax:Syntax: <%@ directive attribute =“value” %> <%@ directive attribute =“value” %>

► Three types of directives:Three types of directives:1.1. pagepage2.2. includeinclude3.3. taglibtaglib

Page 12: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

ActionAction

► JSP actions are XML tags that invoke JSP actions are XML tags that invoke built-in web server functionality.built-in web server functionality. e.g. e.g.

<jsp:setProperty name = “myBean” property = “message” value = “This is my

message” />

Page 13: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Sample1Sample1<html><head><title>Untitled Document</title></head>

<body><%= "hello world"%>

</body></html>

Page 14: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Sample2Sample2<html><head><title>Untitled Document</title></head><body><% if(Math.random()<0.5) {%>

Have a <BR>nice<BR>day<% }else {%>

Have a <BR>lousy<BR>day<%}%></body></html>

Page 15: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Sample 3Sample 3<%@ page import="java.util.*" %><HTML><BODY><%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; }%>Hello! The time is now <%= getDate() %></BODY></HTML>

Page 16: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

JavaBeanJavaBean►An object that holds data with setter An object that holds data with setter

and getter methods.and getter methods.►Can be used to store data through out Can be used to store data through out

the session.the session.public class SimpleBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; }}

Page 17: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Tag LibraryTag Library

►One of the features of JSPOne of the features of JSP►Simplify complex server-side behavior Simplify complex server-side behavior

into simple elementsinto simple elements►Creates custom JSP tags into a library.Creates custom JSP tags into a library.►Each tag library has a tag library Each tag library has a tag library

descriptordescriptor TLD describes each tag informationTLD describes each tag information

►Hide underlying implementation Hide underlying implementation

Page 18: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Tag ExampleTag Example

<!—TagExample --><html><head><title>Get Name and Course with DropList tag</title></head><body><%@ taglib uri="mytags" prefix ="mytag" %><mytag:tagExample name = "Joe " lname="Doe" /></body></html>

Page 19: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

// This is myTagExample.java// This is myTagExample.javapackage tony;package tony;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;import javax.servlet.jsp.tagext.SimpleTagSupport;import java.io.IOException;import java.io.IOException;/**/** * SimpleTag handler that prints "Hello, world!"* SimpleTag handler that prints "Hello, world!" */*/public class myTagExample.java extends SimpleTagSupport {public class myTagExample.java extends SimpleTagSupport { protected String name="";protected String name=""; protected String lastName="";protected String lastName=""; public void doTag() throws JspException, IOException {public void doTag() throws JspException, IOException { getJspContext().getOut().write(name+getJspContext().getOut().write(name+

" :Hello world: “ +lastName); " :Hello world: “ +lastName); }} public void setName(String name){public void setName(String name){ this.name = name;this.name = name; }} public void setLname(String lname){public void setLname(String lname){ this.lastName = lname;this.lastName = lname; }}}}

Page 20: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

Tag Library DescriptorTag Library Descriptor<tag><tag>

<name>tagExample</name><name>tagExample</name> <tag-class>tony.myTagExample </tag-class><tag-class>tony.myTagExample </tag-class> <body-content>EMPTY</body-content><body-content>EMPTY</body-content> <description><description>

performperform </description></description> <attribute><attribute> <name>name</name><name>name</name> <required>true</required><required>true</required> </attribute></attribute> <attribute><attribute> <name>lname</name><name>lname</name> <required>true</required><required>true</required> </attribute></attribute> </tag></tag>

Page 21: JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►

ConclusionConclusion


Recommended