Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Javaaa

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Servlets

Java Servlets are programs that run on a Web or Application


server and act as a middle layer between a requests 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.

• 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.
• Java Servlets often serve the same purpose as programs
implemented using the Common Gateway Interface (CGI).
But Servlets offer several advantages in comparison with
the CGI.

Applications of Servlet
• Read the explicit data sent by the clients (browsers). This
includes an HTML form on a Web page or it could also come
from an applet or a custom HTTP client program.
• Read the implicit HTTP request data sent by the clients
(browsers). This includes cookies, media types and
compression schemes the browser understands, and so forth.
• Process the data and generate the results. This process may
require talking to a database, executing an RMI or CORBA
call, invoking a Web service, or computing the response
directly.

Servlets Architecture
The following diagram shows the position of Servlets in a Web
Application.
Servlets - Life Cycle
A servlet life cycle can be defined as the entire process from its
creation till the destruction. The following are the paths followed
by a servlet.

• 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.
• Finally, servlet is garbage collected by the garbage collector
of the JVM.

web.xml file
1. <web-app>
2.
3. <servlet>
4. <servlet-name>sonoojaiswal</servlet-name>
5. <servlet-class>DemoServlet</servlet-class>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>sonoojaiswal</servlet-name>
10. <url-pattern>/welcome</url-pattern>
11. </servlet-mapping>
12.
13. </web-app>

Steps to create a servlet example


1. Create a directory structure
2. Create a Servlet
3. Compile the Servlet
4. Create a deployment descriptor
5. Start the server and deploy the project
6. Access the servlet

Servlet – Session Tracking


HTTP is a “stateless” protocol, which means that each time a client
requests a Web page, the client establishes a new connection with the
Web server, and the server does not retain track of prior requests.
• The conversation of a user over a period of time is referred to
as a session. In general, it refers to a certain period of time.
• The recording of the object in session is known as tracking.
• Session tracking is the process of remembering and
documenting customer conversations over time. Session
management is another name for it.
• The term “stateful web application” refers to a web
application that is capable of remembering and recording
client conversations over time.

What is a Framework in Java?


A framework is a set of classes and interfaces which provide a ready-
made architecture. In order to implement a new feature or a class, there
is no need to define a framework. However, an optimal object-oriented
design always includes a framework with a collection of classes such
that all the classes perform the same kind of task.

Ex:- ArrayList
// Java program to demonstrate the

// working of ArrayList

import java.io.*;

import java.util.*;

class GFG {

// Main Method

public static void main(String[] args)

// Declaring the ArrayList with

// initial size n

ArrayList<Integer> al = new ArrayList<Integer>();

// Appending new elements at

// the end of the list

for (int i = 1; i <= 5; i++)

al.add(i);

// Printing elements

System.out.println(al);

// Remove element at index 3

al.remove(3);

// Displaying the ArrayList


// after deletion

System.out.println(al);

// Printing elements one by one

for (int i = 0; i < al.size(); i++)

System.out.print(al.get(i) + " ");

Package in Java is a mechanism to encapsulate a group of classes, sub


packages and interfaces. Packages are used for:
• Preventing naming conflicts. For example there can be two
classes with name Employee in two packages,
college.staff.cse.Employee and college.staff.ee.Employee
• Making searching/locating and usage of classes, interfaces,
enumerations and annotations easier
• Providing controlled access: protected and default have package
level access control. A protected member is accessible by
classes in the same package and its subclasses. A default
member (without any access specifier) is accessible by classes in
the same package only.
• Packages can be considered as data encapsulation (or data-
hiding).
• Types of packages:

You might also like