Top Banner
JSP Cornelius Koo, ST JavaSchool 2005 Jl. Cemara 2/20, Salatiga
70

JSP

Dec 13, 2014

Download

Technology

corneliuskoo

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JSP

JSP

Cornelius Koo, ST

JavaSchool

2005

Jl. Cemara 2/20, Salatiga

Page 2: JSP

Accessing Class via JSP

Page 3: JSP

<html>

<body>

The page count is :

<%

out.println(foo.Counter.getCount());

%>

</body>

</html>

Page 4: JSP

package foo;

public class Counter {

private static int count;

public static synchronized int getCount() {

count++;

return count;

}

}

Page 5: JSP

Importing Class Using

Page Directives

Page 6: JSP

<%@ page import=“foo.*” %>

<html>

<body>

The page count is :

<%

out.println(Counter.getCount());

%>

</body>

</html>

Page 7: JSP

<%@ page import=“foo.*, java.util.*,

java.sql.*” %>

Page 8: JSP

Scriptlet

Page 9: JSP

<%@ page import=“foo.*” %>

<html>

<body>

The page count is :

<%

out.println(Counter.getCount());

%>

</body>

</html>

Page 10: JSP

Expression

Page 11: JSP

<%@ page import=“foo.*” %>

<html>

<body>

The page count is :

<%= Counter.getCount() %>

</body>

</html>

Page 12: JSP

out.print( Counter.getCount() );

Page 13: JSP

• Scriptlet <% ... %>

• Directive <%@ … %>

• Expression <%= … %>

Page 14: JSP

Declaring Variables and

Methods

Page 15: JSP

Inside The Service Method

<html>

<body>

<% int count = 0; %>

The page count is :

<%= ++count %>

</body>

</html>

Page 16: JSP

Public class basicCounter_jsp extends HttpServlet {

public void _jspService(

HttpServletRequest req,

HttpServletResponse res) throws

java.io.IOException {

PrintWriter out = response.getWriter();

response.setContentType(“text/html”);

out.write(“<html><body>”);

int count = 0;

out.write(“The page count is :”);

out.write( ++count );

out.write(“</body></html>”);

}

}

Page 17: JSP

Inside The Servlet Scope

<html>

<body>

<%! int count = 0; %>

The page count is :

<%= ++count %>

</body>

</html>

Page 18: JSP

Public class basicCounter_jsp extends HttpServlet {

int count = 0;

public void _jspService(

HttpServletRequest req,

HttpServletResponse res) throws

java.io.IOException {

PrintWriter out = response.getWriter();

response.setContentType(“text/html”);

out.write(“<html><body>”);

out.write(“The page count is :”);

out.write( ++count );

out.write(“</body></html>”);

}

}

Page 19: JSP

Implicit Objects

Page 20: JSP

• JspWriter - out

• HttpServletRequest - request

• HttpServletResponse - response

• HttpSession - session

• ServletContext - application

• ServletConfig - config

• JSPException - exception

• PageContext - pageContext

• Object - page

Page 21: JSP

Comment

Page 22: JSP

• <!-- HTML Comments -->

• <%-- JSP Comments --%>

Page 23: JSP

The Compilation Process

Page 24: JSP
Page 25: JSP

Init Param

Page 26: JSP

DD

<web-app>

...

<servlet>

<servlet-name>TestInitServlet</servlet-name>

<jsp-file>/init.jsp</jsp-file>

<init-param>

<param-name>name</param-name>

<param-value>zaradaz</param-value>

</init-param>

</servlet>

...

</web-app>

Page 27: JSP

Overriding jspInit()

<%!

public void jspInit() {

ServletConfig servletConfig = getServletConfig();

String name = servletConfig.getInitParameter("name");

ServletContext ctx = getServletContext();

ctx.setAttribute("name", name);

}

%>

Page 28: JSP

Attributes in JSP

Page 29: JSP

PageContext

Page 30: JSP
Page 31: JSP

Page Scoped Attribute

Set

<% Double index = new Double(45.4); %>

<% pageContext.setAttribute( "attr" , index); %>

Get

<%= pageContext.getAttribute( "attr") %>

Page 32: JSP

Request Scoped Attribute

Set

<% Double index = new Double(45.4); %>

<% pageContext.setAttribute( "attr" , index,

PageContext.REQUEST_ATTRIBUTE); %>

Get

<%= pageContext.getAttribute( "attr" ,

PageContext.REQUEST_ATTRIBUTE) %>

Page 33: JSP

Session Scoped Attribute

Set

<% Double index = new Double(45.4); %>

<% pageContext.setAttribute( "attr" , index,

PageContext.SESSION_ATTRIBUTE); %>

Get

<%= pageContext.getAttribute( "attr" ,

PageContext.SESSION_ATTRIBUTE) %>

Page 34: JSP

Application Scoped Attribute

Set

<% Double index = new Double(45.4); %>

<% pageContext.setAttribute( "attr" , index,

PageContext.APPLICATION_ATTRIBUTE); %>

Get

<%= pageContext.getAttribute( "attr" ,

PageContext.APPLICATION_ATTRIBUTE) %>

Page 35: JSP

Finding Attributes

<%= pageContext.findAttribute( "attr" ) %>

Priority :

1. Request

2. Session

3. Application

Page 36: JSP

Directives

Page 37: JSP

Page Directive

<%@ page import=“foo.*, java.util.*,

java.sql.*”%>

Page 38: JSP

Taglib Directive

<%@ taglib tagdir=“/WEB-INF/tags/zip”

prefix=“zip” %>

Page 39: JSP

Include Directive

<%@ include file=“page.html” %>

Page 40: JSP

Blocking Java Code

Page 41: JSP

• We can block the use of scriptlet,

expression and declarations in our jsp

code.

Page 42: JSP

<web-app>

...

<jsp-config>

<jsp-property-group>

<url-pattern>*.jsp</url-pattern>

<scripting-invalid>true</scripting-invalid>

</jsp-property-group>

</jsp-config>

...

</web-app>

Page 43: JSP

Actions

Page 44: JSP

<jsp:include … />

Page 45: JSP

• <@ include … > insertion happens at

translation time

• <jsp:include … /> insertion happens at

runtime

Page 46: JSP

Include Directive

<body>

<%@ include file="header.jspf" %>

<h3>The main body</h3>

<%@ include file="footer.jspf" %>

</body>

Page 47: JSP

Include Directive

• header.jspf

<h1>This is the Header</h1>

• footer.jspf

<b><i>JavaSchool, school of object

technology</i></b><br>

<address> Jl. Cemara 2/20, Salatiga

</address>

Page 48: JSP

Include Actions

<jsp:include page="header.jspf" flush="true"/>

<jsp:include page="action_header.jsp" flush="true">

<jsp:param name="title" value="This is the header's title"/>

</jsp:include>

Page 49: JSP

<jsp:forward … />

Page 50: JSP

<body>

Please login first, and don't forget to enter your name <br>

<form name="form1" method="post" action="hello.jsp">

<input type="text" name="userName">

<input type="submit" name="Submit" value="Submit">

</form>

</body> login.jsp

Page 51: JSP

<body>

<%if (request.getParameter("userName") == null || request.getParameter("userName").equals("")) {

%>

<jsp:forward page="login.jsp"/>

<%} %>

Hello ${param.userName}

</body> hello.jsp

Page 52: JSP

<jsp:useBean … />

Page 53: JSP

<jsp:useBean

id="person"

class="jsp.example.bean.Person"

scope="request"/>

Person is : <jsp:getProperty name="person" property="name"/> <br/>

Address : <jsp:getProperty name="person" property="address"/> <br/>

Gender : <jsp:getProperty name="person" property="gender"/> <br/>

Age : <jsp:getProperty name="person" property="age"/> <br/>

Page 54: JSP

<jsp:getProperty … />

Page 55: JSP

<jsp:useBean

id="person"

class="jsp.example.bean.Person"

scope="request"/>

Person is :

<jsp:getProperty

name="person"

property="name"/>

<br/>

Address : <jsp:getProperty name="person" property="address"/> <br/>

Gender : <jsp:getProperty name="person" property="gender"/> <br/>

Age : <jsp:getProperty name="person" property="age"/> <br/>

Page 56: JSP

<jsp:setProperty … />

Page 57: JSP

<jsp:setProperty

name="person"

property="name"

value="John" />

Page 58: JSP

Creating Bean

Page 59: JSP

Use <jsp:useBean>

<jsp:useBean

id=“person”

class="jsp.example.bean.Person"

scope=“page“ >

<jsp:setProperty

name="person"

property="name"

value="John" />

</jsp:useBean>

Page 60: JSP

• The bean is created only when there’s no

bean object at all.

Page 61: JSP

Shorter Way

Page 62: JSP

<jsp:useBean id="person"

class="jsp.example.bean.Person"

scope="request">

<jsp:setProperty

name="person" property="*"/>

</jsp:useBean>

Page 63: JSP

Pre-Condition

Page 64: JSP

Pre-Condition

<td><input type="text" name="name"></td>

<td><input type="text" name="address"></td>

<td><input type="radio" name="gender"value="true">Male</td>

<td><input type="radio" name="gender"value="false">Female</td>

<td><input type="text" name="age"></td>

Page 65: JSP

Inherited Bean

Page 66: JSP
Page 67: JSP

Usage

<jsp:useBean id="person"

type="jsp.example.bean.Person"class="jsp.example.bean.Employee"scope="request">

<jsp:setProperty name="person" property="*"/>

</jsp:useBean>

Person : <jsp:getProperty name="person" property="name"/> <br/>

Address : <jsp:getProperty name="person" property="address"/><br/>

Gender : <jsp:getProperty name="person" property="gender"/> <br/>

Age : <jsp:getProperty name="person" property="age"/> <br/>

Emp ID : <jsp:getProperty name="person" property="empID"/> <br/>

Page 68: JSP

• Person person = new Employee();

type class

Page 69: JSP

<jsp:plugin>

<jsp:params>

<jsp:param>

<jsp:fallback>

Page 70: JSP

<jsp:plugin type="applet" code="jsp.example.TestApplet" codebase="/classes/applets" height="100" width="100">

<jsp:params>

<jsp:param name="color" value="black"/>

<jsp:param name="speed" value="fast"/>

<jsp:param name="sound" value="off"/>

</jsp:params>

<jsp:fallback>Your browser cannot display the applet</jsp:fallback>

</jsp:plugin>