Introduction Servlets and JSP Celsina Bignoli bignolic@smccd.net
Dynamic web pages Content may change based on identity of the user user’s browser type information provided by the user selections made by the user
CGI CGI (Common Gateway Inteface) CGI scripting is not efficient outlines how a web server communicates with a program. Typically written in Pearl (or C) CGI scripting is not efficient for every request the web server has to create a new process load an interpreter run the program and dispose of the process once done.
CGI improvements Fast-CGI mod_perl, NSAPI, ISAPI runs program in a external process or pool of processes mod_perl, NSAPI, ISAPI run server-side programs in the same process as the web server available only on some web servers
Servlets Server side Java programs Solve scalability issue serlvets are run on threads of execution not separate processes Solve portability issue runs on every platform that supports Java supported by all most popular web servers Issue html tags are embedded in java programs within out.print() statements
JSP server-side technology separate dynamic content from static content of a page Java scriptlets embedded into html-like page Separate the work of java programmers page authors Servlets are server-side Java programs that extends the functionality of a server and can be used to generate dynamic web pages. Compared to competing technologies, such as CGI scripts they offer several advantages: Being written in Java, Servlets are portable across operating systems. In traditional CGI, for every request, the web server has to create a new operating-system process, load a Perl interpreter, load the script code, execute the script, and then terminate the process once it is done. Servlets use Java multithreading so that N simultaneous requests are handle by N concurrent threads of execution within a single process. Servlet remains in memory after servicing a request making it possible to store data between requests. However Servlet technology suffer from a problem: since Servlets are Java programs, HTML tags must be embedded inside the Java code. If you've ever looked at the code for a servlet, you've probably seen endless calls to out.println( ) that contain scores of HTML tags. This approach makes writing and maintaining Servlet rather tedious and error prone. JSP was desined to solve this issue. JSP is an abstraction over the concept of a Servlet that allows the generation of dynamic web content but keeping separated the java code that produces the content form the HTML code that defines its presentation.
JSP relationship to Servlets A JSP is converted into a Servlet What is the relationship between JSP and Servlets? Behind the scenes aJSP page is transformed into a Servlet. From a technical point of view JSP does not provide new core technology - everything that can be done with a JSP page, can also be done by writing a servlet.
Servlet Example embedded HTML import java.text.*; import java.util.*; public class DateServlet extends HttpServlet{ public void doGet(HTTPServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(“text/html’); PrintWriter out = response.getWriter(); out.println(“<html>”); out.println(“<head><title><Date Example</title></head>”); out.println(“<body>”); out.println(“Today is: “); Date d = new Date(); out.println(“<em>” + DateFormat.getInstance().format(d)+ “</em>”); out.println(“</html>”); } embedded HTML The example shows a simple JSP that prints out the current date. You notice it consists of a mix of HTML elements and JSP elements some of which are directives like the import on the first line other are called scriptlets and are used to embed Java code. A JSP page is stored in its textual form on the web server, in a way much similar to HTML pages.
JSP Example Static: HTML/XML elements Dynamic: scriptlets JSP tag <%@ page import=“java.text.*’, java.util.*”%> <html> HTML <head><title><Date Example</title></head> <body> <% Date d=new Date(); String today = DateFormat.getInstance().format(d); %> Scriptlet Today is: <em><%=today%></em> </body> </html> The example shows a simple JSP that prints out the current date. You notice it consists of a mix of HTML elements and JSP elements some of which are directives like the import on the first line other are called scriptlets and are used to embed Java code. A JSP page is stored in its textual form on the web server, in a way much similar to HTML pages. Example.jsp Static: HTML/XML elements Dynamic: scriptlets Additional: special JSP elements
JSP alternatives Active Server Pages PHP ColdFusion allows you to add VBScript or JScript code to the page primarily a solution for te Windows platform PHP open source web-scripting language. extensive set of predifined functions to access databases, LDAP directories, mail servers etc… widely supported ColdFusion ColdFusion Markup Language(CFML) accessing databases, files, mail servers etc… custom elements can be developed in C++ or Java
JSP Unique Features JSP is a specification, not a product competing implementations integral part of J2EE access all features of the Java language
How to get Started Install JDK 5.0 Download Apache Tomcat 5.5 from: www.apache.org Use a regular editor or an IDE that supports JSP development Eclipse JBuilder JCreator Oracle JDeveloper NetBeans Macromedia DreamWeaver