Presentation is loading. Please wait.

Presentation is loading. Please wait.

Servlets.

Similar presentations


Presentation on theme: "Servlets."— Presentation transcript:

1 Servlets

2 Introduction programs that run on a Web or Application server
technology is used to create web application (resides at server side and generates dynamic web page). Servlet is a web component that is deployed on the server to create dynamic web page. act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically. Servlet is a class that extend the capabilities of the servers and respond to the incoming request. It can respond to any type of requests.

3 CGI(Commmon Gateway Interface)
enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process. There are many problems in CGI technology: If number of clients increases, it takes more time for sending response. For each request, it starts a process and Web server is limited to start processes. It uses platform dependent language e.g. C, C++, perl.

4 Advantage of Servlet The web container creates threads for handling the multiple requests to the servlet. better performance: because it creates a thread for each request not process. Portability: because it uses java language. Robust: Servlets are managed by JVM so we don't need to worry about memory leak, garbage collection etc. Secure: because it uses java language..

5

6

7 HTTP Requests

8 GET- It requests the data from a specified resource
GET and POST Two common methods for the request-response between a server and client are: GET- It requests the data from a specified resource POST- It submits the processed data to a specified resource GET POST 1) In case of Get request, only limited amount of data can be sent because data is sent in header. In case of post request, large amount of data can be sent because data is sent in body. 2) Get request is not secured because data is exposed in URL bar. Post request is secured because data is not exposed in URL bar. 3) Get request can be bookmarked. Post request cannot be bookmarked. 4) Get request is idempotent . It means second request will be ignored until response of first request is delivered Post request is non-idempotent.

9 Servlets Tasks Read the explicit data sent by the clients (browsers).
Read the implicit HTTP request data sent by the clients (browsers). Process the data and generate the results. Send the explicit data (i.e., the document) to the clients (browsers). Send the implicit HTTP response to the clients (browsers).

10 Servlets Packages javax.servlet- contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol. javax.servlet.http - contains interfaces and classes that are responsible for http requests only.

11 The servlet is initialized by calling the init () method.
Servlets - Life Cycle The servlet is initialized by calling the init () method. The servlet calls service() method to process a client's request. The servlet is terminated by calling the destroy() method. init() method called only once It is called when the servlet is first created, and not called again for each user request When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost service() method main method to perform the actual task. handle requests coming from the client( browsers) and to write the formatted response back to the client. checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete

12 destroy() method called only once at the end of the life cycle of a servlet chance to close database connections, halt background threads

13 provides common behaviour to all the servlets.
Servlet Interface provides common behaviour to all the servlets. Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to destroy the servlet Methods of Servlet interface Method Description public void init(ServletConfig config) initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once. public void service(ServletRequest request,ServletResponse response) provides response for the incoming request. It is invoked at each request by the web container. public void destroy() is invoked only once and indicates that servlet is being destroyed.

14 GenericServlet class implements Servlet, ServletConfig and Serializable interfaces protocol-independent.

15 HttpServlet class extends the GenericServlet class and implements Serializable interface provides http specific methods such as doGet, doPost, doHead, doTrace

16 Reading Form Data using Servlet:
getParameter(): You call request.getParameter() method to get the value of a form parameter. getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getParameterNames(): Call this method if you want a complete list of all parameters in the current request.

17 GET method sends the encoded user information appended to the page request The page and the encoded information are separated by the ? character as follows: default method to pass information from browser to web server it produces a long string that appears in your browser's Location:box POST method more reliable method of passing information to a backend program instead of sending it as a text string after a ? in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing. Servlet handles this type of requests using doPost() method.


Download ppt "Servlets."

Similar presentations


Ads by Google