Docsity

Understanding Servlets and CGI Scripts: Apache, Tomcat, and Java, Slides of Computer Science

Partial preview of the text

Download Understanding Servlets and CGI Scripts: Apache, Tomcat, and Java and more Slides Computer Science in PDF only on Docsity!

Servlets

Servers

  • A server is a computer that responds to requests from a client
    • Typical requests: provide a web page, upload or download a file, send email
  • A server is also the software that responds to these requests; a client could be the browser or other software making these requests
  • Typically, your little computer is the client, and someone else’s big computer is the server - However, any computer can be a server - It is not unusual to have server software and client software running on the same computer

Ports

  • A port is a connection between a server and a client
    • Ports are identified by positive integers
    • A port is a software notion, not a hardware notion, so there may be very many of them
  • A service is associated with a specific port
    • Typical port numbers:
      • 21 —FTP, File Transfer Protocol
      • 22 —SSH, Secure Shell
      • 25 —SMTP, Simple Mail Transfer Protocol
      • 53 —DNS, Domain Name Service
      • 80 —HTTP, Hypertext Transfer Protocol
      • 8080 —HTTP (used for testing HTTP)
      • 7648 , 7649 —CU-SeeMe
      • 27960 —Quake III

These are the ports of most interest to us

Ports II

  • My UPenn Web page is: http://www.cis.upenn.edu/~matuszek
  • But it is also: http://www.cis.upenn.edu:80/~matuszek
  • The http: at the beginning signifies a particular protocol (communication language), the Hypertext Transfer Protocol
  • The :80 specifies a port
  • By default, the Web server listens to port 80
    • The Web server could listen to any port it chose
    • This could lead to problems if the port was in use by some other server
    • For testing servlets, we typically have the server listen to port 8080
  • In the second URL above, I explicitly sent my request to port 80
    • If I had sent it to some other port, say, 99 , my request would either go unheard, or would (probably) not be understood

Servlets

• A servlet is like an applet, but on the server side

Client sends a request to server Server starts a servlet Servlet computes a result for server and does not quit

Another client sends a request

client server

client servlet

Server calls the servlet again Etc.

Server returns response to client

Servlets vs. CGI scripts

• Advantages:

  • Running a servlet doesn’t require creating a

separate process each time

  • A servlet stays in memory, so it doesn’t have to be

reloaded each time

  • There is only one instance handling multiple

requests, not a separate instance for every

request

  • Untrusted servlets can be run in a “sandbox”

• Disadvantage:

Servlets

• A servlet is any class that implements the

javax.servlet.Servlet interface

  • In practice, most servlets extend the

javax.servlet.http.HttpServlet class

  • Some servlets extend

javax.servlet.GenericServlet instead

• Servlets, like applets, usually lack a main

method, but must implement or override

certain other methods

Important servlet methods, I

  • When a servlet is first started up, its init(ServletConfig config ) method is called - init should perform any necessary initializations - init is called only once, and does not need to be thread-safe
  • Every servlet request results in a call to service(ServletRequest request , ServletResponse response ) - service calls another method depending on the type of service requested - Usually you would override the called methods of interest, not service itself - service handles multiple simultaneous requests, so it and the methods it calls must be thread safe
  • When the servlet is shut down, destroy() is called
    • destroy is called only once, but must be thread safe (because other threads may still be running) Docsity.com

Important servlet methods, II

  • The service method dispatches the following kinds of requests: DELETE, GET, HEAD, OPTIONS, POST, PUT, and TRACE - A GET request is dispatched to the doGet(HttpServletRequest request, HttpServletResponse response) method - A POST request is dispatched to the doPost(HttpServletRequest request, HttpServletResponse response) method - These are the two methods you will usually override - doGet and doPost typically do the same thing, so usually you do the real work in one, and have the other just call it - public void doGet(HttpServletRequest request, HttpServletResponse response) { doPost(request, response); }

A “Hello World” servlet

(from the Tomcat installation documentation) public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "\n"; out.println(docType + "\n" + "Hello\n" + "\n" + "Hello World\n" + ""); } } Don’t worry, we’ll take this a little at a time!

The doGet method

  • public void doGet(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
  • This method services a GET request
  • The method uses request to get the information that was sent to it
  • The method does not return a value; instead, it uses response to get an I/O stream, and outputs its response
  • Since the method does I/O, it can throw an IOException
  • Any other type of exception should be encapsulated as a ServletException
  • The doPost method works exactly the same way

Parameters to doGet

• Input is from the HttpServletRequest parameter

  • Our first example doesn’t get any input, so we’ll

discuss this a bit later

• Output is via the HttpServletResponse object,

which we have named response

  • I/O in Java is very flexible but also quite complex,

so this object acts as an “assistant”

Using the PrintWriter

  • From here on, it’s just a matter of using our PrintWriter, named out, to produce the Web page
  • First we create a header string: String docType = "\n";
  • This line is technically required by the HTML spec
  • Browsers mostly don’t care, but HTML validators do care
  • Then use the println method of out one or more times out.println(docType + "\n" + " ... ");
  • And we’re done!

Input to a servlet

• A GET request supplies parameters in the

form

URL? name = value & name = value & name = value

  • (Illegal spaces added to make it more legible)
  • Actual spaces in the parameter values are

encoded by + signs

  • Other special characters are encoded in hex; for

example, an ampersand is represented by %

• Parameter names can occur more than once,

with different values

• A POST request supplies parameters in the