Docsity

Servlets vs. CGI Scripts: A Comparison of Web Technologies, Slides of Java Programming

Partial preview of the text

Download Servlets vs. CGI Scripts: A Comparison of Web Technologies and more Slides Java Programming in PDF only on Docsity!

1

ServletsLecture#4-

2

CGI Scripts

• CGI stands for “Common Gateway Interface”Client sends a request to serverServer starts a CGI scriptScript computes a result for serverand quitsAnother client sends a request

client

server

client

script

Server starts the CGI script againEtc.

script

Server returns response to client

4

Servlets vs. CGI scripts

•^ Advantages:

– Running a servlet doesn’t require creating a separateprocess each time– A servlet stays in memory, so it doesn’t have to bereloaded each time– There is only one instance handling multiple requests,not a separate instance for every request

•^ Disadvantage:

– Less choice of languages (CGI scripts can be in anylanguage)

5

What is a Servlet

• Java technology for Common GatewayInterface (CGI)• Servlets are Java programs that serve as anintermediating layer between an HTTPrequest of a client and applications in theWeb server

7

Using Servlets

•^ Reading the data that the user sent (HTTP request)•^ Receiving information from the HTTP request•^ Running an application with respect to the giveninput•^ The application creates a document for theresponse (e.g., HTML document)•^ Parameters are defined for the HTTP response•^ The created document is sent to the user

8

A Java Servlet

Sending arequest andreceiving aresponse

Servlets Request FlowReceive Request for

Servlet

S

Is^ S

loaded?

Is^ S

current?

(re)Load

S

Forward Request

to^ S

Servlets die whenServlet Server dies

Loaded when first used,

or after modified no

no

yes yes

11

Working with Servlets

13

Architecture^ YourOwnServlet

Servlet Generic Servlet HttpServlet

14

Creating a Servlet

Extend

HTTPServlet

Implement

doGet

Implement

doPost

The methods

should get an input (the HTTP request)Should create an output (the HTTP response)

16

Hello WorldExample

import

java.io.; import*

javax.servlet.; import*

javax.servlet.http.; public*

class

HelloWorld

extends HttpServlet

{ public

void

doGet(HttpServletRequest

req,

HttpServletResponse res)

throws

ServletException,

IOException{

res.setContentType("text/html");PrintWriter

out

=^ res.getWriter();

out.println

("");**

out.println("Hello

World");

out.println(“Hello

World

");

out.close(); } }

17

Hello WorldExample

import

java.io.; import*

javax.servlet.; import*

javax.servlet.http.; public*

class

HelloWorld

extends HttpServlet

{ public

void

doGet(HttpServletRequest

req,

HttpServletResponse res)

throws

ServletException,

IOException{

res.setContentType("text/html");PrintWriter

out

=^ res.getWriter();

out.println

("");**

out.println("Hello

World");

out.println(“Hello

World

");

out.close(); } }

19

Servlet Life Cycle

• No

main()

method!

• The server loads and initializes the servlet• The servlet handles client requests• The server can remove the servlet• The servlet can remain loaded to handleadditional requests• Incur startup costs only once

20

Life Cycle Schema