Your SlideShare is downloading. ×
JSP
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,249
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
52
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. JSP Cornelius Koo, ST JavaSchool 2005Jl. Cemara 2/20, Salatiga
  • 2. Accessing Class via JSP
  • 3. <html><body>The page count is :<% out.println(foo.Counter.getCount());%></body></html>
  • 4. package foo;public class Counter { private static int count; public static synchronized int getCount() { count++; return count; }}
  • 5. Importing Class Using Page Directives
  • 6. <%@ page import=“foo.*” %><html><body>The page count is :<% out.println(Counter.getCount());%></body></html>
  • 7. <%@ page import=“foo.*, java.util.*, java.sql.*” %>
  • 8. Scriptlet
  • 9. <%@ page import=“foo.*” %><html><body>The page count is :<% out.println(Counter.getCount());%></body></html>
  • 10. Expression
  • 11. <%@ page import=“foo.*” %><html><body>The page count is :<%= Counter.getCount() %></body></html>
  • 12. out.print( Counter.getCount() );
  • 13. • Scriptlet <% ... %>• Directive <%@ … %>• Expression <%= … %>
  • 14. Declaring Variables and Methods
  • 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>”); }}
  • 19. Implicit Objects
  • 20. • JspWriter - out• HttpServletRequest - request• HttpServletResponse - response• HttpSession - session• ServletContext - application• ServletConfig - config• JSPException - exception• PageContext - pageContext• Object - page
  • 21. Comment
  • 22. • <!-- HTML Comments -->• <%-- JSP Comments --%>
  • 23. The Compilation Process
  • 24. Init Param
  • 25. 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>
  • 26. Overriding jspInit()<%! public void jspInit() { ServletConfig servletConfig = getServletConfig(); String name = servletConfig.getInitParameter("name"); ServletContext ctx = getServletContext(); ctx.setAttribute("name", name); }%>
  • 27. Attributes in JSP
  • 28. PageContext
  • 29. Page Scoped AttributeSet<% Double index = new Double(45.4); %><% pageContext.setAttribute( "attr" , index); %>Get<%= pageContext.getAttribute( "attr") %>
  • 30. Request Scoped AttributeSet<% Double index = new Double(45.4); %><% pageContext.setAttribute( "attr" , index, PageContext.REQUEST_ATTRIBUTE); %>Get<%= pageContext.getAttribute( "attr" , PageContext.REQUEST_ATTRIBUTE) %>
  • 31. Session Scoped AttributeSet<% Double index = new Double(45.4); %><% pageContext.setAttribute( "attr" , index, PageContext.SESSION_ATTRIBUTE); %>Get<%= pageContext.getAttribute( "attr" , PageContext.SESSION_ATTRIBUTE) %>
  • 32. Application Scoped AttributeSet<% Double index = new Double(45.4); %><% pageContext.setAttribute( "attr" , index, PageContext.APPLICATION_ATTRIBUTE); %>Get<%= pageContext.getAttribute( "attr" , PageContext.APPLICATION_ATTRIBUTE) %>
  • 33. Finding Attributes<%= pageContext.findAttribute( "attr" ) %>Priority :1. Request2. Session3. Application
  • 34. Directives
  • 35. Page Directive<%@ page import=“foo.*, java.util.*, java.sql.*”%>
  • 36. Taglib Directive<%@ taglib tagdir=“/WEB-INF/tags/zip” prefix=“zip” %>
  • 37. Include Directive<%@ include file=“page.html” %>
  • 38. Blocking Java Code
  • 39. • We can block the use of scriptlet, expression and declarations in our jsp code.
  • 40. <web-app>... <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <scripting-invalid>true</scripting-invalid> </jsp-property-group> </jsp-config>...</web-app>
  • 41. Actions
  • 42. <jsp:include … />
  • 43. • <@ include … > insertion happens at translation time• <jsp:include … /> insertion happens at runtime
  • 44. Include Directive<body><%@ include file="header.jspf" %> <h3>The main body</h3><%@ include file="footer.jspf" %></body>
  • 45. 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>
  • 46. 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 headers title"/></jsp:include>
  • 47. <jsp:forward … />
  • 48. <body> Please login first, and dont 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
  • 49. <body> <% if (request.getParameter("userName") == null || request.getParameter("userName").equals("")) { %> <jsp:forward page="login.jsp"/> <% } %> Hello ${param.userName}</body> hello.jsp
  • 50. <jsp:useBean … />
  • 51. <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/>
  • 52. <jsp:getProperty … />
  • 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/>
  • 54. <jsp:setProperty … />
  • 55. <jsp:setProperty name="person" property="name" value="John" />
  • 56. Creating Bean
  • 57. Use <jsp:useBean><jsp:useBean id=“person” class="jsp.example.bean.Person" scope=“page“ > <jsp:setProperty name="person" property="name" value="John" /></jsp:useBean>
  • 58. • The bean is created only when there’s no bean object at all.
  • 59. Shorter Way
  • 60. <jsp:useBean id="person" class="jsp.example.bean.Person" scope="request"> <jsp:setProperty name="person" property="*"/></jsp:useBean>
  • 61. Pre-Condition
  • 62. 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>
  • 63. Inherited Bean
  • 64. 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/>
  • 65. • Person person = new Employee(); type class
  • 66. <jsp:plugin><jsp:params><jsp:param><jsp:fallback>
  • 67. <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>

×