jsp and servlet overview - j2ee tutorial 1
TRANSCRIPT
Presentation Overview Introduction to J2EEExplain the major technologies within
the J2EE designationJ2EE applicationsJ2EE servers
The Java 2 Platform Platform introduced June, 1999 J2SE – Java 2 Standard Edition
Java for the desktop / workstation http://java.sun.com/j2se
J2ME – Java 2 Micro Edition Java for the consumer device http://java.sun.com/j2me
J2EE - Java 2 Enterprise Edition Java for the server http://java.sun.com/j2ee
Java Servlets Servlets are the Java platform technology of
choice for extending and enhancing web servers.
Servlets provide a component-based, platform-independent method for building web-based applications, without the performance limitations of CGI programs.
http://java.sun.com/products/servlets/index.html
Java Servlets Servlets have access to the entire family of
Java APIs, including the JDBCTM API to access enterprise databases.
Servlets can also access a library of HTTP-specific calls and receive all the benefits of the mature Java language, including portability, performance, reusability, and crash protection
http://java.sun.com/products/servlets/index.html
Anatomy of a Servlet init() – the init() function is called when the servlet is
initialized by the server. This often happens on the first doGet() or doPut() call of the servlet.
destroy() – this function is called when the servlet is being destroyed by the server, typically when the server process is being stopped.
http://java.sun.com/docs/books/tutorial/servlets/lifecycle/index.html
Anatomy of a Servlet doGet() – the doGet() function is called when the
servlet is called via an HTTP GET. doPost() – the doPost() function is called when the
servlet is called via an HTTP POST. POSTs are a good way to get input from HTML forms
http://java.sun.com/docs/books/tutorial/servlets/lifecycle/index.html
Anatomy of a Servlet HTTPServletRequest object
Information about an HTTP request Headers Query String Session Cookies
HTTPServletResponse object Used for formatting an HTTP response
Headers Status codes Cookies
Sample Servlet import java.io.*; //Apache Tomcat sample codeimport javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet
{ public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>");
out.println("<body>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); }
}
JSP – JavaServer Pages JavaServer Pages technology uses XML-like tags
and scriptlets written in the Java programming language to encapsulate the logic that generates the content for the page.
Any and all formatting (HTML or XML) tags are passed directly back to the response page.
By separating the page logic from its design and display and supporting a reusable component-based design, JSP technology makes it faster and easier than ever to build web-based applications.
http://java.sun.com/products/jsp/index.html
Sample JSP<html> <!- Apache Tomcat Samples -
><!-- Copyright (c) 1999 The Apache Software Foundation. All rights reserved.--><body bgcolor="white"><jsp:useBean id='clock' scope='page' class='dates.JspCalendar'
type="dates.JspCalendar" />
<font size=4><ul><li> Day of month: is <jsp:getProperty name="clock" property="dayOfMonth"/><li> Year: is <jsp:getProperty name="clock" property="year"/><li> Month: is <jsp:getProperty name="clock" property="month"/><li> Time: is <jsp:getProperty name="clock" property="time"/><li> Date: is <jsp:getProperty name="clock" property="date"/><li> Day: is <jsp:getProperty name="clock" property="day"/><li> Day Of Year: is <jsp:getProperty name="clock" property="dayOfYear"/><li> Week Of Year: is <jsp:getProperty name="clock" property="weekOfYear"/><li> era: is <jsp:getProperty name="clock" property="era"/><li> DST Offset: is <jsp:getProperty name="clock" property="DSTOffset"/><li> Zone Offset: is <jsp:getProperty name="clock" property="zoneOffset"/></ul></font>
</body></html>