1 / 21

JavaServer Page

JavaServer Page. by Antonio Ko. Overview. Introduction What is a servlet? What can servlets do? Servlets Vs JSP Syntax Samples JavaBean Tag Library Conclusion. Introduction. Java Server Pages (JSP) is basically Sun's answer to Microsoft's Active Server Pages (ASP).

hana
Download Presentation

JavaServer Page

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaServer Page by Antonio Ko

  2. Overview • Introduction • What is a servlet? • What can servlets do? • Servlets Vs JSP • Syntax • Samples • JavaBean • Tag Library • Conclusion

  3. Introduction • Java Server Pages (JSP) is basically Sun's answer to Microsoft's Active Server Pages (ASP). • Advantages over other technologies: • It is in Java • No tied to a particular server product • JSP is actually based on Java Servlet

  4. What is Servlet • Java’s answer to the Common Gateway Interface (CGI). • Applet: a java program that runs within the web browser. • Servlet: a java program that runs within the web server.

  5. What can Servlets do • Search Engines • Personalization Systems • E-Commerce Applications • Shopping Carts • Product Catalogs • Intranet Applications • Groupware Applications: bulletin boards, file sharing, etc.

  6. Servlets vs JSP • Servlets • code looks like a regular Java program. • JSP • embed Java commands directly within HTML • Let’s examine a Servlet program next to a JSP program… • Each of these prints, “Hello, World!”

  7. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<HTML>"); out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>"); out.println("<BODY>"); out.println("<BIG>Hello World</BIG>"); out.println("</BODY></HTML>"); } }

  8. <html> <head> <title>Hello, World JSP Example</title> </head> <body> <h2> Hello, World! The current time in milliseconds is <%= System.currentTimeMillis() %> </h2> </body> </html>

  9. Syntax • Three main types of JSP constructs embed in a HTML page: • Scripting elements • Directives • actions

  10. Scripting Element • Three forms available: • <%= expression %>, for output • <% code %>, for a block of Java code • <%! code %>, for declaration

  11. Directives • A JSP directive affects the overall structure of the servlet that results from the JSP page. • Syntax: <%@ directive attribute =“value” %> • Three types of directives: • page • include • taglib

  12. Action • JSP actions are XML tags that invoke built-in web server functionality. • e.g. <jsp:setProperty name = “myBean” property = “message” value = “This is my message” />

  13. Sample1 <html> <head> <title>Untitled Document</title> </head> <body> <%= "hello world"%> </body> </html>

  14. Sample2 <html> <head> <title>Untitled Document</title> </head> <body> <% if(Math.random()<0.5) {%> Have a <BR>nice<BR>day <% }else {%> Have a <BR>lousy<BR>day <%}%> </body> </html>

  15. Sample 3 <%@ page import="java.util.*" %> <HTML> <BODY> <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now <%= getDate() %> </BODY> </HTML>

  16. JavaBean • An object that holds data with setter and getter methods. • Can be used to store data through out the session. public class SimpleBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } }

  17. Tag Library • One of the features of JSP • Simplify complex server-side behavior into simple elements • Creates custom JSP tags into a library. • Each tag library has a tag library descriptor • TLD describes each tag information • Hide underlying implementation

  18. Tag Example <!—TagExample --><html> <head> <title>Get Name and Course with DropList tag</title> </head> <body> <%@ taglib uri="mytags" prefix ="mytag" %> <mytag:tagExample name = "Joe " lname="Doe" /> </body> </html>

  19. // This is myTagExample.java package tony; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; /** * SimpleTag handler that prints "Hello, world!" */ public class myTagExample.java extends SimpleTagSupport { protected String name=""; protected String lastName=""; public void doTag() throws JspException, IOException { getJspContext().getOut().write(name+ " :Hello world: “ +lastName); } public void setName(String name){ this.name = name; } public void setLname(String lname){ this.lastName = lname; } }

  20. Tag Library Descriptor <tag> <name>tagExample</name> <tag-class>tony.myTagExample </tag-class> <body-content>EMPTY</body-content> <description> perform </description> <attribute> <name>name</name> <required>true</required> </attribute> <attribute> <name>lname</name> <required>true</required> </attribute> </tag>

  21. Conclusion

More Related