You are on page 1of 11

Java Server Pages (JSP) &

Servlets
Web Development
TPM
Class Project
April 22, 2010
In the beginning… – Serving Static Pages
HTTP Request
GET
…..
…..

<HTML>
<HEAD>
<HTML>
</HEAD>
<HEAD>
<HTML>
</HEAD>
<HEAD>
<BODY></HEAD>
HTTP Response ……..
<BODY>
</BODY>
……..
<BODY>
</HTML>
</BODY>
……..
<HTML> </HTML>
</BODY>
<HEAD> </HTML>
</HEAD>
Client Web Server
<BODY>
……..
</BODY>
</HTML>
The web server gets the
A web browser lets user
request, finds the resource,
request a resource
and returns it to user.
(HTML page, picture, sound)

• This architecture sufficed for providing “static” data, but inability to provide
“dynamic” data based on input provided by user (client)
But what if you want Dynamic Content
& Ability to Save Data on the Server?
Web Server
parameters

HTTP header info <HTML>


<HEAD>
<HTML> </HEAD>
<HEAD>
</HEAD>

<BODY>
web server <BODY>
…….. helper
</BODY>
……..
</BODY>
app </HTML> app
</HTML>

• Need some kind of “helper” app that can work with the server
• Helper app is what does the work of generating dynamic content
• The first “helper apps” were “CGI” scripts/programs (Perl, C) – initially, was main
technology used to generate dynamic content
– Limitations: platform dependence, lack of scalability
• To address limitations, Java servlet technology developed:
– JSP / Servlets (Java)
 Other server side technologies in use that generate dynamic content:
– ASP (Microsoft)
– PHP
What’s a Servlet?
• Servlets are the “helper” app written in the Java
programming language
– Servlets are server side Java programs which extend the
functionality of the web server; used for generating a
response dynamically based on the user request
– A servlet is usually (though not always) executed in
response to an invocation from an HTML page. It process
the request dynamically and generates a response.
What is JSP (Java Server Page)?
• JSP is also java based technology used <HTML>
Class TimeIs {
<BODY>
for generating dynamic content The current time
is: XX:XX
void check() {

}
x = new Date();
</BODY>
– JSP is based on functionality provided by </HTML>
}

Servlet specification
• JSP = HTML + Java (sort of) <HTML>
<BODY>
– JSP written in HTML with a little Java The current time is:
<%= new java.util.Date() %>

thrown in </BODY>
</HTML>

– Java is enclosed in special tags, <%...%>


• Every JSP page is translated into a Java
servlet
So what’s the difference between
Servlet and JSP?
SERVLETS JSP
• Servlets are pure java • JSP page more like an
programs with class and HTML document, with
method definitions Java mixed in
• With servlets, • With JSP, page can be
developer must write designed like static web
code to output the page, with JSP tags used
HTML markup for the dynamic content
• Servlets well suited for • JSP pages well suited as
executing business logic views
How are Servlets Run? Container

Web Server
HTTP request
GET GET GET
….. ….. …..
….. ….. …..

<HTML> <HTML>
<HEAD> <HEAD>
HTTP header info </HEAD> </HEAD>
<HTML>
<HEAD>
web server <BODY>
web container <BODY> servlet
</HEAD> …….. ……..
</BODY> </BODY>
<BODY> app </HTML> app </HTML>
……..
</BODY>
</HTML>

What is a container?
• Servlets don’t have a main() method. They’re under control of another Java
application called a Container.
• Container manages and controls servlet; handles threading, security, networking
• Web server app (e.g Apache) hands the request to the Container (e.g. Tomcat),
container gives request to the servlet and calls the servlet’s methods
How are Servlets Run?
Web Server
request
HTTP request
GET
GET GET
…..
….. …..
…..
….. ….. servlet

<HTML>
<HEAD>
HTTP header info </HEAD>
<HTML>
<HEAD>
web server <BODY>
web container thread
</HEAD> ……..
</BODY>
<BODY> app </HTML> app <HTML>
<HEAD>
…….. </HEAD>
</BODY>
</HTML> <BODY> Service()
……..
</BODY>
</HTML>

HTTP response doGet()


response

• The container sees that the request is for a servlet, so container creates HttpServletRequest
and HTTPServletResponse objects to handle the HTTP request and HTTP response.
• Container finds correct servlet based on URL in request, creates thread for the request, and
passes the request and response objects to the servlet thread.
• Container calls servlet’s service() method, which calls either doGet() or doPost() mehtod,
which generates the dynamic page, and sends it back in the response object.
How does the Container find the
Servlet? (DD)
• The URL that comes in as part of the
request from the client is mapped to a
specific servlet on the server.
• Mapping is done by a file called the
Deployment Descriptor (DD)
• DD is XML document <web-app xmlns=http://java.sun.com/xml/ns/j2ee

– Maps internal name to fully qualified xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee/web-
app_2_4.xsd
class name version="2.4">

– Maps internal name to public URL name


<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.example.web.TestSelect</servlet-class>
</servlet>

• Benefits of Mapping: Improves <servlet-mapping>


<servlet-name>Test</servlet-name>

flexibility and security


<url-pattern>/SelectTest.do</url-pattern>
</servlet-mapping>

</web-app>
Development Environment
• Many vendors offer Integrated Development Environments
which contain servlet wizards that make it easy to create
and compile servlets almost automatically.
• Development Environments:
– Eclipse
• Free, open-source IDE
– IBM's WebSphere Studio IDE (based on Eclipse)
– Sun Java Studio Creator (built on NetBeans IDE)
• Or NetBeans IDE, which is free
• Or the way I do it:
– A smart editor that recognizes programming language syntax
(Notepad++) , and command line options (javac) in DOS window
References
• http://java.sun.com/products/servlet/articles/tutorial/
• http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html
• http://www.apl.jhu.edu/~hall/java/IDEs.html

You might also like