What is J2EE?
J2EE (Java 2 Platform Enterprise Edition) is a combination of tools and java technologies available to simplify application development and deployment on the Internet. Technologies include Servlets, JSP(s), Enterprise Java Beans and JDBC to name a few. Tools include the JDK and Forte for Java
What are Java Servlets?
Servlets are Java technology's replacement for CGI programming. They are java programs that run on a web server or J2EE container and provide dynamic web content. Commonly used to extend the functionality of a web server. Built upon a request-response model (doGet,doPost methods) Access to Java API(s) bundled with JDK such as JDBC.
Advantages of Servlets over CGI
Efficient/Scalable: JVM is loaded once and each HTTP request is handled by a lightweight java thread. CGI executes a heavyweight OS process for each user request. The overhead of this OS process degrades performance and impacts scalability. Powerful: simplified session tracking (session API provides support for getting track of state using cookies, URL rewriting and hidden form fields) and data sharing that allows easy implementation of things like connection pooling. Portable: runs on any web server with servlet support. (All of them)
Client Server Architecture
HttpServlet Class
All Servlet classes extend the HttpServlet abstract class HttpServlet simplies writing HTTP servlets by providing a framework for handling the HTTP protocol. Since HttpServlet is abstract, you must extend it and implement at least one of the methods (doGet,doPost) Your Servlet class is declared as public so the web server or J2EE container can access it.
Typical Servlet Class
import javax.servlet.*; // contains generic (protocol-independent) servlet classes import javax.servlet.http.*; // contains HTTP servlet classes public class TypicalServletClass extends HttpServlet { public void init() throws ServletException { } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void destroy() { } }
Life of a Servlet
Life cycle is controlled by the web server or container in which the servlet is deployed. (1) If instance of servlet class does not exist:
Loads the servlet class Creates an instance of the servlet class Initializes the servlet instance by calling the init() method.
Life of a Servlet (Cont)
(2) Invokes a service method (doGet(), doPost()) and passing in a request and response object. (3) If the servlet needs to be garbage collected (removed from memory), then the destroy() method is called by the web server/J2EE container (finally)
doGet vs. doPost Method
doGet is called in response to an HTTP GET request. A GET request is a request to get a resource from the server. This is the case of a browser requesting a web page. It also happens with HTML Forms that specify the METHOD="GET" in the FORM tag. doPost is called in response to an HTTP POST request. A POST request is a request to post or send form data to a resource on the server. Both methods are called by the default (superclass) implementation of service in the HttpServlet base class. You override one or both to perform your servlet's actions.
So Which One do I implement?
doGet has some limitations in the amount of data that can be passed as parameters doGet data is sent as a part of the URL and thus sensative data may be exposed Most implement doPost and and doGet placing the code that will perform some action in doPost and calling doPost from doGet passing in the request and response objects. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
Simple Servlet Example
import java.io.*; // for system input and output import javax.servlet.*; // contains generic (protocol-independent) servlet classes import javax.servlet.http.*; // contains HTTP servlet classes public class SimpleServletExample extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("
Welcome to Bradshaw Marina"); out.println("
Welcome to Bradshaw Marina
Current Time : "); out.println(new java.util.Date()); out.println("
"); } }
How to call Servlet from HTML
Call a Simple Servlet Call a Simple Servlet that Generates the Welcome Page
What are Java Server Pages
Java Server Pages (JSP) is a technology that allows you to mix static and dynamic HTML content. (very similar to ASP) Web content rendered by CGI programs are mostly static. Even with servlets you generate the entire web page with your java code. JSPs allow the two to be separated. Presentation is standard HTML and java is written within special tags (<%, %>)
Advantages of Java Server Pages
vs. Active Server Pages (ASP). ASP is a similar technology from Microsoft. The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS-specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers. vs. Pure Servlets. JSP doesn't give you anything that you couldn't in principle do with a servlet. But it is more efficient to write the presentation in HTML than to have many println statements that generate the HTML. You can separate the tasks among different people.
Advantages of Java Server Pages
vs. Server-Side Includes (SSI). SSI is a widely-supported technology for including externally-defined pieces into a static Web page. JSP is better because it lets you use servlets instead of a separate program to generate that dynamic part. SSI is only intended for simple inclusions, not complex tasks that use form data and make database connections. vs. JavaScript. JavaScript can generate HTML dynamically on the client. This is a useful capability, but only handles situations where the dynamic information is based on the client's environment. With the exception of cookies, HTTP and form submission data is not available to JavaScript. And, since it runs on the client, JavaScript can't access server-side resources like databases.
Simple JSP Example
<%@ page import="java.util.*" %>
Welcome to Bradshaw Marina Welcome to Bradshaw Marina
Current Time : <%=new java.util.Date()%>
Deploying Servlets and JSP(s)
Web Servers
Microsoft IIS – servlet/JSP plugin Oracle 9ias – built in servlet/JSP support Weblogic – built in servlet/JSP support Tomcat - built in servlet/JSP support Oracle’s OC4J – built in servlet/JSP support
J2EE Lightweight containers for Java
Additional Resources
http://java.sun.com/ http://java.sun.com/products/servlet/index.html http://java.sun.com/products/jsp/ http://jakarta.apache.org/tomcat/
Demos
SimpleServletExample –display “Welcome” screen with HTML and make a call to the java.util package to get the current date and time. SimpleJSP.jsp - display “Welcome” screen with HTML and make a call to the java.util package to get the current date and time. OracleQueryServlet – displays a list of employees from a specific department (demonstrates both get and post requests) SimpleOracleQueryJSP1.jsp - execute query using statement class. SimpleOracleQueryJSP2.jsp - execute query using preparedStatement class and bind variables. CallSPSample.jsp - execute a stored procedure using the callableStatement class and bind variables.