Transcript Servlets
Servlets
Our Project
• 3-tier application
• Develop our own multi-threaded server
• Socket level communication
Web Application
• Shift Perspective
• use web server
• Develop servlet to extend web server to allow
dynamic contents
Web Server
• listens on a standard port and handles http protocol.
• A browser requests for documents (mainly html files) +
upload of small amounts of data
• Two most important http protocol elements: (hidden
from you by browser)
– GET (request document, may upload data)
– POST (request document, upload data).
4
Early Web Servers
• Early web server were static, acted more like file
servers:
– Browser requests page
– Server hands over page
– Browser interprets html and displays to user
– Might contain gif or jpeg images or simple
animations
5
Modern Web Servers
• Need to support dynamic page
• E-Commerce became popular
• Need web pages to act more like programs that
could interact with user.
6
Running an Applet
Client Computer
Server Computer
Internet
Web Browser
Request
Web Server
HTML + Applet
Java VM
The client’s web browser sends a request to the server for a web page
with a Java Applet.
The server sends the HTML for the web page and applet class files to the
client.
The client runs the applet using the Java Virtual Machine and displays
its output in the web browser.
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
7
Running A Servlet
Server Computer
Client Computer
Internet
Web Browser
Request
HTML
Web Server
HTML
Servlet
Engine
The client’s web browser sends a request to the server for a web page
that runs a Java servlet.
The web server instructs the Servlet engine to execute the requested servlet,
which consists of running precompiled Java code. The servlet outputs
HTML that is returned to the web server.
The web server sends the servlet’s HTML to the client’s web browser
to be displayed.
Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
8
Servlets
• Java’s answer to CGI,
– very simple
– Relatively high-level
• Requirements: a servlet-enabled web server
• When specified by your web page, web page
passes http requests to java method (assuming
everything is setup properly)
9
What do Servlets do?
• Read data sent by the user (e.g., form data)
• Look up other information about request in the HTTP request
(e.g., headers, cookies, etc.)
• Generate the results (may do this by talking to a database, file
system, etc.)
• Format the results in a document (e.g., make it into HTML
• Set the appropriate HTTP response parameters (e.g., cookies,
content-type, etc.)
• Send the document to the user
10
Supporting Servlets
• The Web server must support servlets (since it must
run the servlets):
– Apache Tomcat
– Sun’s JavaServer Web Development Kit (JSWDK)
– Other web servers
11
Writing Servlets, cont.
• All servlets extend the Servlet class.
• All http servlets (by far most typical) should extend the
HttpServlet class.
• In extending HttpServlet, you typically override the
following methods:
– init, service or doGet/doPost, destroy (very common)
– doPut, doDelete, doOptions, doTrace (rare)
• Note: there is NO main() for Servlets!
12
Main HttpServlet Methods
• init()
– called once when servlet is loaded by server. Contains any
initializations that are common to all requests.
• doGet(HttpServletRequest, HttpServletResponse)
– Called each time the servlet receives an http GET request
posted by a client. Passes two objects, one representing the
information of the request, the other used to configure a
response. We’ll study these methods soon.
13
Main HttpServlet Methods, cont.
• doPost(HttpServletRequest,
HttpServletResponse)
– Same as doGet but for an http POST request.
• destroy()
– Called before servlet is unloaded from memory.
Performs any final cleanup, freeing memory, closing
connections, etc.
14
Service Method
• Important: The method service(HttpServletRequest,
HttpServletResponse)
is also called for each servlet invocation.
• Service() in turn calls doGet and doPost, etc. for an
HttpServlet.
• It is best not to override service even if you want to
handle doGet and doPost identically. Simply have one
call the other.
15
HttpServletRequest Object
• Passed when browser calls doGet and doPost.
• Most import methods for beginning servlet
programming (in HttpServletRequest class):
– String getParameter(String paramName)
– String[] getParameterNames()
– String[] getParameterValues()
• Makes getting data from web pages very simple.
• Many other methods for images, cookies, etc.
16
HttpServletResponse Object
• Passed when browser calls doGet or doPost
• Most import methods for beginning servlet
programming:
– PrintWriter getWriter();
• Get Writer for communicating back to client
– setContentType(String);
• Typically use “text/html”, indicating that html will be sent
back to the browser
17
Examples
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class SimpleServlet extends HttpServlet
{ /*Handle the HTTP GET method by building a simple web page.*/
public void doGet (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{ PrintWriter
out;
String
title = "Simple Servlet Output”;
response.setContentType("text/html"); // set content type
out = response.getWriter(); // then write the data of the response
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY>");
out.println("<H1>" + title + "</H1>");
out.println("<P>This is output from SimpleServlet.");
out.println("</BODY></HTML>");
out.close();
}
}
The Page
<html>
<body>
<head>
<title>Test Simple Servelet Example</title>
</head>
<h3>Test Simple Servelet Example</h3>
<P>
<form action="http://localhost:8080/servlet/SimpleServlet" method=GET>
<input type=submit name=submitButton value="hit me">
</form>
</body>
</html>