14
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

Embed Size (px)

DESCRIPTION

CSI 3125, Preliminaries, page 3 JSP (Java Server Pages) Html+java=jsp In servlet need 3 file html, web.xtml,java Next technology of servlet is jsp Inserted of 3 file in servlet  jsp with 1file JSp ->.jsp extension Executed on the webserver apache tomcat, web logic, sphere… Used for UI/front end/ display purpose (multipurpose)

Citation preview

Page 1: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 1

JSP (Java Server Pages)

Page 2: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 2

JSP (Java Server Pages) • JavaServer Pages (JSP) is a server-side

programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications.

• JSP have access to the entire family of Java APIs, including the JDBC API

Page 3: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 3

JSP (Java Server Pages) • Html+java=jsp• In servlet need 3 file html, web.xtml,java• Next technology of servlet is jsp

Inserted of 3 file in servlet jsp with 1fileJSp ->.jsp extension

• Executed on the webserver apache tomcat, web logic, sphere…

• Used for UI/front end/ display purpose (multipurpose)

Page 4: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 4

JSP (Java Server Pages) • JSP execution Procedure• When JSP compile (using JSP compiler)-->

converted to servlet file• Servlet file is compiled and generate. class file• This class file is loaded into memory and

executed by JVM

Page 5: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 5

JSP (Java Server Pages) • JSP LIFE CYCLE• • Same as Servlet life cycle init(), service(), destroy();• JSP has • JspInit() - is called when JSP is loaded in memory• jspService(); - is called when the client request• jspDestroy(); - invoked when JSP is removed from

memory• • not need to write these methods they automatically run

Page 6: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 6

JSP (Java Server Pages) • There are five types of JSP tags: • Directive tag • Comment Tag • Expression tag • Declaration statement tag • Scriplet tag

Page 7: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 7

JSP (Java Server Pages) • DIRECTIVES • To perform a specific task• They have the general form: • <%@ directive-name [attribute="value"

attribute="value" ...] %> • There are diff standard directives • page • include

Page 8: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 8

JSP (Java Server Pages) DIRECTIVES Tag • The page Directive • The page directive apply different properties for the page • syntax: • <%@ page [attribute="value" attribute="value" ...] %> • Following are names of some of important the attributes of

the page directive used in JSP • import: : This attribute imports the java packages and it's

classes .• Eg: <%@ page import="java.util.*" %> • contentType: This attribute specifies the type i.e. used for the

JSP response • Eg:<%@ page contentType="text/plain" %>

Page 9: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 9

JSP (Java Server Pages) DIRECTIVES Tag • The include directive• The include directive merges the contents of

another file like a #include C preprocessor directive.

• The syntax is • <%@ include file="filename" %> • where filename can specify with path. • Eg: • <%@ include file="/doc/legal/disclaimer.html" %>

Page 10: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 10

JSP (Java Server Pages) Comment Tag <%- - This is a hidden JSP comment - -%>Expression tag • <%= expression %> • The expression can have any data value• Eg:<%= new java.util.Date() %>

Page 11: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 11

JSP (Java Server Pages) Declaration statement tag Declare variables and functionsEg: <%! private int example = 0 ; private int test = 5 ; %> • SCRIPTLETS • A scriptlet consists of one or more valid Java statements • Ie java code• <% Java Code %> • Eg: <% System.out.println( "Evaluating date now); %>

Page 12: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 12

JSP (prog) <html><body><br><%@ page import=" java.util.*"%><%@ page contentType="text/html" %> <%@ include file="popo.jsp" %>

<%--will not display comment line --%>

<%! int a=10;int b=20;%>

<% out.println("sum of "+a+" and "+b+" = "+(a+b));%>

</body></html>

Page 13: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 13

JSP ( Method def and calling prog) <html><body><%! int add(int x,int y){ return (x+y);}%>Calling Method <%=add(10,20)%>

</body></html>

Page 14: CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)

CSI 3125, Preliminaries, page 14

JSP (Java Server Pages)