HTTP Server Programming
HTTP Server Programming
HTTP Fundamentals
Introduction to HTTP
HTTP (Hypertext Transfer Protocol) is the foundation of data
communication on the World Wide Web.
It is a request-response protocol used by web browsers and web
servers to exchange data and information.
HTTP Request and Response
An HTTP request is made by a client (usually a web browser) to
request resources from a web server.
An HTTP response is sent by the server to fulfill the client's request.
HTTP Methods
HTTP requests use methods like GET, POST, PUT, DELETE to
define the type of request.
GET is used to retrieve data, POST to send data to be processed,
PUT to update data, and DELETE to remove data.
Servlet Programming
Introduction to Servlets
A servlet is a Java-based technology for building web
applications.
Servlets run on the server-side and handle client requests and
generate dynamic responses.
Servlet API
The Java Servlet API provides a framework for developing
servlets.
It includes classes and interfaces for handling servlet requests
and responses.
Creating a Servlet
To create a servlet, you need to extend the
javax.servlet.http.HttpServlet class and override its doGet or
doPost method.
EXAMPLE
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
The Life Cycle of a
Servlet; Using Tomcat for Servlet Development
Initialization
When a servlet is first loaded, its init method is called.
You can perform one-time initialization tasks in this method.
Request Handling
For each client request, the servlet's service method is called.
The service method dispatches the request to the appropriate HTTP
method (doGet, doPost, etc.).
Destruction
When the servlet container decides to unload the servlet, the destroy
method is called.
You can perform cleanup tasks in this method.
Request and Response Objects
Servlets receive two important objects: HttpServletRequest for the request
and HttpServletResponse for the response.
You can use these objects to access client data and send responses.
Using Tomcat for Servlet Development
Apache Tomcat
Apache Tomcat is an open-source web server and servlet container.
It is widely used for running Java-based web applications, including servlets.
Setting up Tomcat
Download and install Tomcat from the Apache Tomcat website.
Configure your web application by placing servlet classes and the web.xml file in the
appropriate directories.
Start Tomcat and deploy your web application.
Accessing Servlets
Servlets are accessed using a URL that maps to their URL pattern defined in
web.xml.
For example, if a servlet is mapped to /myservlet, you can access it at
http://localhost:8080/yourapp/myservlet.
Tomcat Manager
Tomcat provides a web-based manager application that allows you to deploy,
undeploy, and manage your web applications.
You can access it at http://localhost:8080/manager/html.
Servlets - Examples
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
Above entries to be created inside <web-app>...</web-
app> tags available in web.xml file. There could be
various entries in this table already available, but never
mind.
You are almost done, now let us start tomcat server using
<Tomcat-installationdirectory>\bin\startup.bat (on
Windows) or <Tomcat-
installationdirectory>/bin/startup.sh (on Linux/Solaris
etc.) and finally
type http://localhost:8080/HelloWorld in the
browser's address box. If everything goes fine, you would
get the following result
The Javax.servlet Package
Classes Description
GenericServlet Defines a generic, protocol-independent
servlet.
ServletInputStream Provides an input stream for reading
binary data from a client request,
including an efficient readLine method
for reading data one line at a time.
ServletOutputStream Provides an output stream for sending
binary data to the client.
ServletContextEvent This is the event class for notifications
about changes to the servlet context of a
web application.
ServletContextAttributeEvent This is the event class for notifications
about changes to the attributes of the
servlet context of a web application.
ServletRequestEvent Events of this kind indicate lifecycle
events for a ServletRequest.
ServletRequestAttributeEvent This is the event class for notifications
of changes to the attributes of the
servlet request in an application.
package
Interface Description
Servlet Defines methods that all servlets must
implement.
ServletConfig A servlet configuration object used by
a servlet container to pass information
to a servlet during initialization.
ServletContext Defines a set of methods that a servlet
uses to communicate with its servlet
container, for example, to get the
MIME type of a file, dispatch requests,
or write to a log file.
ServletRequest Defines an object to provide client
request information to a servlet.
ServletResponse Defines an object to assist a servlet in
sending a response to the client.
RequestDispatcher Defines an object that receives
requests from the client and sends
them to any resource (such as a
servlet, HTML file, or JSP file) on the
server.
javax.servlet.http
Classes Description
Cookie Creates a cookie, a small amount of information sent by a servlet to a Web
browser, saved by the browser, and later sent back to the server.
HttpSessionBindingEvent Events of this type are either sent to an object that implements
HttpSessionBindingListener when it is bound or unbound from a session, or
to a HttpSessionAttributeListener that has been configured in the
deployment descriptor when any attribute is bound, unbound or replaced in a
session.
HttpSessionEvent This is the class representing event notifications for changes to sessions
within a web application.
package
Interface Description
HttpServletRequest Extends the ServletRequest interface to provide
request information for HTTP servlets.
HttpServletResponse Extends the ServletResponse interface to provide
HTTP-specific functionality in sending a response.
HttpSession Provides a way to identify a user across more than
one page request or visit to a Web site and to store
information about that user.
HttpSessionActivationListener Objects that are bound to a session may listen to
container events notifying them that sessions will
be passivated and that session will be activated.
Get
Its main job is to get some resource From server based on client request
Total number of characters that get can carry to server is very less/limited.
This appends URL with client request data
One can Cache the requests of this type
Requests parameters remain in browsing history
Because of above limitations, GET is not supposed to used for complex and sensitive
requests
Post
Its main job is to send client data and get the resource from server based on client
request.
Post can handle more characters that get can carry to server is very less/limited.
This does not append client request data to URL
These requests cannot be cached
These do not remain in browsing history
This is more preferred for complex and sensitive requests (like login)
Tracking
Cookies
Cookies are small pieces of information that are
sent in response from the web server to the
client. Cookies are the simplest technique used for
storing client state.
Cookies are stored on client's computer. They have
a lifespan and are destroyed by the client browser at
the end of that lifespan.
Servlet: Cookies API