Contenu connexe Plus de corneliuskoo (15) JSP1. JSP
Cornelius Koo, ST
JavaSchool
2005
Jl. Cemara 2/20, Salatiga
4. package foo;
public class Counter {
private static int count;
public static synchronized int getCount() {
count++;
return count;
}
}
6. <%@ page import=“foo.*” %>
<html>
<body>
The page count is :
<%
out.println(Counter.getCount());
%>
</body>
</html>
9. <%@ page import=“foo.*” %>
<html>
<body>
The page count is :
<%
out.println(Counter.getCount());
%>
</body>
</html>
13. • Scriptlet <% ... %>
• Directive <%@ … %>
• Expression <%= … %>
15. Inside The Service Method
<html>
<body>
<% int count = 0; %>
The page count is :
<%= ++count %>
</body>
</html>
16. 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>”);
}
}
17. Inside The Servlet Scope
<html>
<body>
<%! int count = 0; %>
The page count is :
<%= ++count %>
</body>
</html>
18. 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>”);
}
}
20. • JspWriter - out
• HttpServletRequest - request
• HttpServletResponse - response
• HttpSession - session
• ServletContext - application
• ServletConfig - config
• JSPException - exception
• PageContext - pageContext
• Object - page
26. 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>
27. Overriding jspInit()
<%!
public void jspInit() {
ServletConfig servletConfig = getServletConfig();
String name = servletConfig.getInitParameter("name");
ServletContext ctx = getServletContext();
ctx.setAttribute("name", name);
}
%>
31. Page Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index); %>
Get
<%= pageContext.getAttribute( "attr") %>
32. Request Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index,
PageContext.REQUEST_ATTRIBUTE); %>
Get
<%= pageContext.getAttribute( "attr" ,
PageContext.REQUEST_ATTRIBUTE) %>
33. Session Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index,
PageContext.SESSION_ATTRIBUTE); %>
Get
<%= pageContext.getAttribute( "attr" ,
PageContext.SESSION_ATTRIBUTE) %>
34. Application Scoped Attribute
Set
<% Double index = new Double(45.4); %>
<% pageContext.setAttribute( "attr" , index,
PageContext.APPLICATION_ATTRIBUTE); %>
Get
<%= pageContext.getAttribute( "attr" ,
PageContext.APPLICATION_ATTRIBUTE) %>
41. • We can block the use of scriptlet,
expression and declarations in our jsp
code.
42. <web-app>
...
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
...
</web-app>
45. • <@ include … > insertion happens at
translation time
• <jsp:include … /> insertion happens at
runtime
50. <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
51. <body>
<% if (request.getParameter("userName") ==
null ||
request.getParameter("userName").equals("")) {
%>
<jsp:forward page="login.jsp"/>
<% } %>
Hello ${param.userName}
</body> hello.jsp
53. <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/>
55. <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/>
59. Use <jsp:useBean>
<jsp:useBean
id=“person”
class="jsp.example.bean.Person"
scope=“page“ >
<jsp:setProperty
name="person"
property="name"
value="John" />
</jsp:useBean>
60. • The bean is created only when there’s no
bean object at all.
62. <jsp:useBean id="person"
class="jsp.example.bean.Person"
scope="request">
<jsp:setProperty
name="person" property="*"/>
</jsp:useBean>
67. 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/>
70. <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>