Java Servlets and JSP
Enterprise Computing
What is J2EE?
◼ Open and standard based platform for
◼ developing, deploying and managing
◼ n-tier, Web-enabled, server-centric, and
component-based enterprise applications
The Java Platform
The Java Platform
What Makes Up J2EE?
◼ API and Technology specifications
◼ Development and Deployment Platform
◼ Standard and production-quality
implementation
◼ Compatibility Test Suite (CTS)
◼ J2EE brand
◼ J2EE Blueprints
◼ Sample codes
Open and Standard Solution
◼ Use "component and container" model
in which container provides system
services in a well-defined and as
industry standard
◼ J2EE is that standard that also provides
portability of code because it is based
on Java technology and standard-based
Java programming APIs
Platform Value to Developers
◼ Can use any J2EE implementation for
development and deployment
◼ Use production-quality standard implementation which
is free for development/deployment
◼ Use high-end commercial J2EE products for scalability
and fault-tolerance
◼ Vast amount of J2EE community resources
◼ Many J2EE related books, articles, tutorials, quality
code you can use, best practice guidelines, design
patterns etc.
◼ Can use off-the-shelf 3rd-party business
components
Platform Value to Vendors
◼ Vendors work together on specifications
and then compete in implementations
◼ In the areas of Scalability, Performance,
Reliability, Availability, Management and
development tools, and so on
◼ Freedom to innovate while maintaining
the portability of applications
◼ Do not have create/maintain their
own proprietary APIs
Platform Value to Business
Customers
◼ Application portability
◼ Many implementation choices are
possible based on various requirements
◼ Price (free to high-end), scalability (single
CPU to clustered model), reliability,
performance, tools, and more
◼ Best of breed of applications and platforms
◼ Large developer pool
J2EE 1.4 APIs &Technologies
◼ J2SE 1.4 (improved) ◼ Servlet 2.4
◼ JAX-RPC (new) ◼ JSP 2.0
◼ Web Service for ◼ EJB 2.1
J2EE ◼ JAXR
◼ J2EE Management ◼ Connector 1.5
◼ J2EE Deployment ◼ JACC
◼ JMX 1.1 ◼ JAXP 1.2
◼ JMS 1.1 ◼ JavaMail 1.3
◼ JTA 1.0 ◼ JAF 1.0
What is a Servlet?
◼ Java objects which extend the
functionality of a HTTP server
◼ Dynamic contents generation
◼ Better alternative to CGI, etc.
◼ Efficient
◼ Platform and server independent
◼ Session management
◼ Java-based
Server-side Java for the web
◼ a servlet is a Java program which outputs an html
page; it is a server-side technology
HTTP Request Server-side Request
Java Servlet
HTTP Response Response Header + Java Server Page
Html file
browser
web server
servlet container
(engine) - Tomcat
First java servlet
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head>");
[Link]("<title>First servlet</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("It works...<hr>");
[Link]("</body>");
[Link]("</html>");
}
}
What is a java servlet ?
◼ a java servlet is a java class extending HTTPServlet
class
◼ a java servlet class implements the doGet(), doPost()
or other equivalent HTTP method and (usually) prints
at the standard output an html file
◼ a java servlet class can contain any kind of java code
the JDK can compile
◼ a java servlet class needs to be compiled prior to using
it; it must use [Link]
Apache Tomcat
◼ the most well known servlet/jsp container
◼ is a web server + implementation of Java Servlet and
JSP (Java Server Pages) APIs
◼ is developed by Apache Software Foundation
◼ available at [Link] under Apache
Software License
Installing Tomcat
1. Download the binary zip distribution (e.g. apache-
[Link]) in a local folder (we will use c:\temp
in the rest of the guide)
2. Set the environment variables
JAVA_HOME=path_to_JDK_installation_folder
CATALINA_HOME=path_to_tomcat_installation_folder
either as Windows system variables or in the files
[Link] and [Link] from the bin directory
Ex. place the following lines in the beginning of [Link]
and [Link]:
set JAVA_HOME=c:\progra~1\java\jdk1.6.0
set CATALINA_HOME=c:\temp\apache-tomcat-6.0.20
Starting/shutting down Tomcat
◼ start Tomcat from a cmd prompter (window):
c:\temp\apache-tomcat-6.0.20\bin\[Link]
◼ verify startup by pointing a browser to the url
[Link]
◼ shutting down Tomcat from a cmd prompter
(window):
c:\temp\apache-tomcat-6.0.20\bin\[Link]
Tomcat standard folders
◼ bin – contains executable files for controlling the server
(start, shut down etc.)
◼ conf – contains configuration files; most important
[Link] for configuring the server and [Link] a
general configuration file for web applications
◼ lib – libraries (jars) used by tomcat and deployed web
applications
◼ logs – log files
◼ temp – temporary files
◼ webapps – contains the web applications deployed
◼ work – contains files created by tomcat during running
(e.g. it crates a servlet from each jsp file)
Format of a web application
◼ web applications are stored in the webapps folder,
either as a folder or as a .war archive
◼ a web application (either a folder or a .war archive)
must contain the following files:
◼ WEB-INF folder
◼ WEB-INF\[Link]: a configuration file
◼ optionally the WEB-INF folder can contain the following
subfolders:
◼ classes: which contain servlets
◼ lib: which contains jars used by the web application
◼ html, jsp and resource files can be placed anywhere in
the web application home folder
◼ servlets must be placed in the folder or subfolders of
WEB-INF\classes
A very simple [Link] example
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation=[Link]
[Link] version="2.5">
</web-app>
Configuring servlets
◼ for JSPs (Java Server Pages) no additional
configuration needs to be done
◼ for java servlets additional lines must be placed in the
[Link] file:
<servlet>
<servlet-name>ServletsName</servlet-name>
<servlet-class>The_Class_Name_of_the_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> ServletsName </servlet-name>
<url-pattern>/URL_of_the_servlet</url-pattern>
</servlet-mapping>
First JSP file
<html>
<body>
<%
[Link](“First JSP… It works<br/>");
%>
</body>
</html>
What is a Java Server Page (JSP)
◼ an html file containing parts of java code; the java code
is placed inside the “<% … %>” tags or some other
related tags
◼ Tomcat will create a servlet from the jsp file (which will
be saved in the work folder)
◼ when the jsp is requested the servlet is executed and
the output of the server is sent back to the client
End for now