JSP Cornelius Koo, ST JavaSchool 2005 Jl. Cemara 2/20, Salatiga
JSP
Cornelius Koo, ST
JavaSchool
2005
Jl. Cemara 2/20, Salatiga
Accessing Class via JSP
<html>
<body>
The page count is :
<%
out.println(foo.Counter.getCount());
%>
</body>
</html>
package foo;
public class Counter {
private static int count;
public static synchronized int getCount() {
count++;
return count;
}
}
Importing Class Using
Page Directives
<%@ page import=“foo.*” %>
<html>
<body>
The page count is :
<%
out.println(Counter.getCount());
%>
</body>
</html>
<%@ page import=“foo.*, java.util.*,
java.sql.*” %>
Scriptlet
<%@ page import=“foo.*” %>
<html>
<body>
The page count is :
<%
out.println(Counter.getCount());
%>
</body>
</html>
Expression
<%@ page import=“foo.*” %>
<html>
<body>
The page count is :
<%= Counter.getCount() %>
</body>
</html>
out.print( Counter.getCount() );
• Scriptlet <% ... %>
• Directive <%@ … %>
• Expression <%= … %>
Declaring Variables and
Methods
Inside The Service Method
<html>
<body>
<% int count = 0; %>
The page count is :
<%= ++count %>
</body>
</html>
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>”);
}
}
Inside The Servlet Scope
<html>
<body>
<%! int count = 0; %>
The page count is :
<%= ++count %>
</body>
</html>
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>”);
}
}
Implicit Objects
• JspWriter - out
• HttpServletRequest - request
• HttpServletResponse - response
• HttpSession - session
• ServletContext - application
• ServletConfig - config
• JSPException - exception
• PageContext - pageContext
• Object - page
Comment
• <!-- HTML Comments -->
• <%-- JSP Comments --%>
The Compilation Process
Init Param
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>
Overriding jspInit()
<%!
public void jspInit() {
ServletConfig servletConfig = getServletConfig();
String name = servletConfig.getInitParameter("name");
ServletContext ctx = getServletContext();
ctx.setAttribute("name", name);
}
%>
Attributes in JSP
PageContext
Page Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index); %>
Get
<%= pageContext.getAttribute( "attr") %>
Request Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index,
PageContext.REQUEST_ATTRIBUTE); %>
Get
<%= pageContext.getAttribute( "attr" ,
PageContext.REQUEST_ATTRIBUTE) %>
Session Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index,
PageContext.SESSION_ATTRIBUTE); %>
Get
<%= pageContext.getAttribute( "attr" ,
PageContext.SESSION_ATTRIBUTE) %>
Application Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index,
PageContext.APPLICATION_ATTRIBUTE); %>
Get
<%= pageContext.getAttribute( "attr" ,
PageContext.APPLICATION_ATTRIBUTE) %>
Finding Attributes
<%= pageContext.findAttribute( "attr" ) %>
Priority :
1. Request
2. Session
3. Application
Directives
Page Directive
<%@ page import=“foo.*, java.util.*,
java.sql.*”%>
Taglib Directive
<%@ taglib tagdir=“/WEB-INF/tags/zip”
prefix=“zip” %>
Include Directive
<%@ include file=“page.html” %>
Blocking Java Code
• We can block the use of scriptlet,
expression and declarations in our jsp
code.
<web-app>
...
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
...
</web-app>
Actions
<jsp:include … />
• <@ include … > insertion happens at
translation time
• <jsp:include … /> insertion happens at
runtime
Include Directive
<body>
<%@ include file="header.jspf" %>
<h3>The main body</h3>
<%@ include file="footer.jspf" %>
</body>
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>
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>
<jsp:forward … />
<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
<body>
<%if (request.getParameter("userName") == null || request.getParameter("userName").equals("")) {
%>
<jsp:forward page="login.jsp"/>
<%} %>
Hello ${param.userName}
</body> hello.jsp
<jsp:useBean … />
<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/>
<jsp:getProperty … />
<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/>
<jsp:setProperty … />
<jsp:setProperty
name="person"
property="name"
value="John" />
Creating Bean
Use <jsp:useBean>
<jsp:useBean
id=“person”
class="jsp.example.bean.Person"
scope=“page“ >
<jsp:setProperty
name="person"
property="name"
value="John" />
</jsp:useBean>
• The bean is created only when there’s no
bean object at all.
Shorter Way
<jsp:useBean id="person"
class="jsp.example.bean.Person"
scope="request">
<jsp:setProperty
name="person" property="*"/>
</jsp:useBean>
Pre-Condition
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>
Inherited Bean
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/>
• Person person = new Employee();
type class
<jsp:plugin>
<jsp:params>
<jsp:param>
<jsp:fallback>
<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>