1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

28
1 Introduction to Servlets

Transcript of 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Page 1: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

1

Introduction to Servlets

Page 2: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Topics

• Web Applications and the Java Server.

• HTTP protocol.

• Servlets

2

Page 3: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Web Applications

• Two Types of Web Applications:– Presentation oriented: Interactive web applications with

dynamic content usually provided with DHTML, XML, servlets or java server pages.

– Service oriented: Web applications that utilize presentation oriented applications as clients. They consist of web services, SOAP protocol services, RMI etc.

3

Page 4: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Web Applications

» For example an Aplication server or an RMI server that processes Queries to the databse server. It receives the results of the query formats it and passes it over to the presentation oriented application so that it can be sent to the requestor (client of the presentation application)

4

Page 5: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Web Applications

• Web Components: Servlets, JSP (Java Server Pages).

• Web Components are supported by a web container.

• For instance: Tomcat has a servlet engine and a JSP engine. Those engines are the web containers.

5

Page 6: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Web Applications

• Web Containers provide to web components:– Activation

– Initialization

– Configuration

– Translation from http to a servlet request object and

– Translation from a servlet response object to http.

6

Page 7: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Web Applications

• Web Components can communicate directly with a database (via JDBC) or

• They can communicate with a database via Java Beans components or

• They can communicate with other service oriented applications such as a RMI server, or web services, or an application server.

7

Page 8: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Web Applications

8

Client

HTTP Request

HTTP Response

Containers

Web Components

Java Bean Components

HTTPServletRequestObject

HTTPServletResponseObject

RMIServer

Database

DatabaseWeb

Services

Page 9: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

HTTP Protocol

• Request/ Response packets.– Client initiates a request via a TCP connection to a port

(8080 or 80).– Server sends a response packet to client

• Protocol provides methods such as:– GET

– POST

– PUT

– DELETE

– HEAD

– And others9

Page 10: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

HTTP Protocol

• If GET is used by client then:– Data can be part of the URL http://www.cs.iit.edu/index.html?firtsname=“fm”&lastname=“ln”

– Data is not cached by the browser application.

– Data is URL encoded i.e. spaces are converted to + signs, nonalphanumeric characters are converted to % signs followed by the two hex digits representing the character etc.

– Use it when the data is small.

10

Page 11: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

HTTP protocol

• If client uses POST:– The data is part of the body of the HTTP

packet.– Data is cached.– Use it if data is larger and the interaction with

the server changes the state of the resource in the server.

11

Page 12: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

HTTP Protocol

• Error messages can be returned by the server in the response packet.

• Error messages are coded:– 400 means bad request.– 401 means unauthorized request.– 500 Internal server error.

Etc.

12

Page 13: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• A servlet is a java program that executes on the server side.

• It extends the capabilities of a server.• Two types of servlets:

– Generic type

– HTTP type

Note: nevertheless other protocols can be supported such as FTP, POP, SMTP

• We will concentrate on HTTP type. 13

Page 14: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• HTTP type servlets– Extend the web server by executing requests

received in HTTP (from a client) protocol packets and by returning responses that are translated back to HTTP protocol packets.

14

Page 15: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• Clients to Servlets– Client side programs that sent requests can be:

• Html files (forms).

• Applets.

• JavaScript.

• XML.

• Java application programs.

• Active Server pages (ASP) programs.

• Other.15

Page 16: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• Execution environment:– Web Server needs the servlet engine

(container).– The interface between the servlet engine and

the servlet program is defined by the Servlet API.

16

Page 17: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

– The servlet container is responsible for:• Creating an instance of the servlet program.

• Calling the servlet’ s init() method to initialize the servlet.

• Configures the servlet according to preset properties.

• Calling the servlet’ s service method.

• Creating the stream to send to the servlet a request object that encapsulates an y data the client sent as part of the request.

• Creating the stream to receive the response object from the servlet that encapsulates the data generated by the servlet as a result of the request.

• Can handle multiple service calls that be made simultineously (multiple requests arriving at the same time).

• Destroys the servlet by calling destroy() method.

17

Page 18: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• Servlet applications provide:– Processing of a Get or Post type of a request via individual

methods that need to be overridden. • The servlet engine reads the request type off the arriving HTTP

packets, calls the service method of the servlet, the service method will call either a doGet or doPost servlet method.

– Handle many requests at the same time.

– Provide session capability with a client so that multiple request/response pairs can be serviced without loosingtrack of the particular client.

– Can communicate with other servlets forming a chain.

18

Page 19: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

– Can generate a response in Html tags. The servelt engine will extract the html code from the java code and form send it to the client.

• This facilitates the generation of dynamically formed responses based on the request.

– Can execute JDBC code to connect to a database and retrieve information. The response object will then encapsulate the data.

– Can act as clients to a Remote Method Invocation Servers in a distributed objects network.

– Can communicate with Java Beans.19

Page 20: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• Servlet API– javax.servlet– javax.servlet.http

20

Servlet interface Implemented by abstract classImplemented by abstract class

GenericServlet HttpServlet

Page 21: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• The abstract classes provide default implementation for methods:– void init(ServletConfig config)

– ServletConfig getServletConfig()

– void service(ServletRequest requ, ServletResponse resp)

– String getServletInfo()

– void destroy()

– In addition, the HttpServlet class has methods:• doGet (HTTPServletRequest requ, HttpServletResponse resp)

• doPost (HTTPServletRequest requ, HttpServletResponse resp)

These methods have to be overridden by the servlet class that the developer provides.

21

Page 22: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets

• Example:– Client receives an HTML form.– The form in its action tag calls a servlet. – The servlet creates html code and sends back

to the client for display by the browser.

22

Page 23: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets Example-File helloworld,html (simplified)

<html>

<head>

<title>Untitled Document</title>

</head>

<body bgcolor="#FFFFFF">

<p><a href="servlet/HelloWorldExample"></p>

</body>

</head>

</html>

23

Page 24: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets Example – file HelloWorld.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>"); out.println("<head>");

out.println("<title>Hello World!</title>");

out.println("</head>"); out.println("<body>");

out.println("<h1>Hello World!</h1>");

out.println("</body>");

out.println("</html>"); } }

24

Page 25: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets Example – file HelloWorld.java

• The Browser receives the html code:

<html>

<head>

<title>Hello World!</title>

</head>

<body>

<h1>Hello World!</h1>

</body>

</html>

25

Page 26: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets-Another example

<html>

<body>

<Form action=http://localhost:8080/ExampleSite/TechSupport” method=“POST”>

<table><tr><td>

<Input types=“Text” Name=“firstName”></input></td></tr></table>

</form>

</body>

</html>

26

Page 27: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets-Another example TechSupport.java file

import javax.servlet.*;

import javax.servlet.http.*

import java.io.*;

import java.util.*;

public class TechSupport extends HttpServlet

{ protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException

{ res.setContextType(“text/html”);

String myparam=req.getParameter(“firstName”);

PrintWriter out = res.getWriter();

out.println(“<HTML>”);

out.println(“<HEAD>”);

out.println(“<BODY>”);

27

Page 28: 1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.

Servlets-Another example

if(myparam.equals(“MyName”))

{

out.println(“<p”>);

out.println(“You can now login”);

out.println(“</p>”);

}

else

{ out.println(“<p”>);

out.println(“I don’t recognize you”);

out.println(“</p>”);

}

out.println(“</BODY>”);

out.println(“</HEAD>”);

out.println(“<HTML>);

out.close();

}28