1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Java Servlet Technology in Java EE 7

Servlet Request

Contents

keyboard_tab
Java Servlet Technology
1
Servlet
PREVIEW13m 5s
2
Servlet Request
PREVIEW8m 54s
7
MVC
10m 27s

The course is part of this learning path

Servlet Request
Difficulty
Intermediate
Duration
1h 15m
Students
3
Description

In this course, we will learn the concepts of Java EE 7 with a focus on Servlet Technology.

Learning Objectives

  • Java Servlet Technology with Request, Filter and Listeners

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers

Prerequisites

  • Have at least 2 years of Java development experience 
Transcript

Hello dear friends. In this video, we will examine the servlet request and HTTP protocols. Let's begin. The request object encapsulates all information from the client request. In the HTTP protocol, this information is transmitted from the client to the server in the HTTP headers in the message body of the request. Servlet request defines an object to provide client request information to a servlet. The servlet container creates a servlet request object and passes it as an argument to the servlet service method. A servlet request object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend servlet request can provide additional protocol-specific data. The request has many methods. However, I'd like to share only the most useful methods. These include getAttribute, setAttribute, removeAttribute, getAttributeName, getContentLength or Type, getServletContext, isSecure, getServerName, etc. With setAttribute, we can hold any attribute in request. With remove we can delete from request and with get we can use holder data. The getAttribute method is the most practical method in the request. Content length returns the length in bytes of the request body and made available by the input stream. isSecure returns a boolean indicating whether this request was made using a secure channel such as HTTPS. getServletContext gets the servlet context to which this servlet request was last dispatched. We'll use some of these methods in a small project, but for now ,let's move on to the ServletContext. What is ServletContext?

The ServletContext interface defines a servlet's view of the web application within which the servlet is running. The container provider is responsible for providing an implementation of the ServletContext interface in the ServletContainer. Using the ServletContextObject, a servlet can log events, obtain URL references to resources and set and store attributes that other servlets in the context can access. A ServletContext is rooted at a known path within a web server. For example, a ServletContext could be located at http://example.com/product. All requests that begin with this /product request path, known as the context path, are routed to the web application associated with the ServletContext. There is one instance object of the ServletContext interface associated with each web application deployed into a container. In cases where the container is distributed over many virtual machines, a web application will have an instance of the ServletContext for each JVM. Servlets in a container that were not deployed as part of a web application are implicitly part of a default web application and have a default ServletContext. In a distributed container, the default ServletContext is non distributable and must only exit in one JVM. Advantage of the ServletCcontext. Is easy to maintain if any information is shared to all the servlet. It is better to make it available for all the servlet. We provide this information from the web XML file, so if the information has changed, we don't need to modify the servlet. Thus it removes maintenance problem. Basically, the object of ServletContext provides an interface between the container and the servlet. The ServletContext object can be used to get configuration information from the web.xml file. The ServletContext object can be used to set get or remove attributes from the web.xml file. The ServletContext object can be used to provide inter application communication. The most commonly used ServletContext interface methods are getInitParameter, getInitParameterNames, setAttribute, getAttribute, removeAttribute. Once we have the ServletContext object, we can set the attributes of the ServletContext object by using the setAttribute method. Since the ServletContext object is available to all the servlet of the web application, other servlets can retrieve the attribute from the ServletContext object by using the getAttribute method. GetInitParameter returns the parameter value for the specified parameter name. GetInitParameterNames returns the names of the context initialization parameters. removeAttribute removes the attribute with the given name from the ServletContext. We will use some of these methods when creating our servlet project. Now let's talk about response. A servlet can use this object to help it provide a response to the client. A servlet response object is created by the servletContainer and passed as an argument to the servlet service function. The response object encapsulates all information to be returned from the server to the client. In the HTTP protocol, this information is transmitted from the server to the client either by HTTP headers or the message body of the request. The ServletContainer is connected to the web server that receives HTTP requests from client on a certain port. When a client sends a request to the web server, the servletContainer creates HTTP servlet request and HTTP servlet response objects and passes them as an argument to the servlet service method. The response object allows you to format and send the response back to the client. There are many methods in the servlet response interface. Let's look at commonly used methods. FlushBuffer force any context in the buffer to be written to the client. GetBuffer size returns the actual buffer size used for the response. GetCharacterEncoding returns the name of the character encoding used for the body sent in this response. GetContentType returns the content type used for the mind body sent in this response. GetLocale returns the locale specified for this response using the set locale method. SetLocale sets the locale of the response if the response has not been committed yet. GetOutputStream returns a servlet output stream suitable for writing binary data in the response. GetWriter returns a print writer object that can send character text to the client. IsCommitted, returns a boolean indicating if the response has been committed. Reset clears any data that exists in the buffer as well as the status code and headers. ResetBuffer clears the content of the underlying buffer in the response without clearing headers or status codes. setBufferSize sets the preferred buffer size for the body of the response. SetCharacterEncoding sets the character encoding of the response being sent to the client, for example, u tf-8. SetContentLength sets the content of the content body in the response. In HTTP servlet, this method sets the HTTP content length header. SetContentType sets the content type of the response being sent to the client if the response has not been committed yet. I believe that covers the ServleContext. In our upcoming project, we will use some of these methods. Let's take a break here. Next video, we will examine the filters and listeners in servlet. See you in the next video.

 

About the Author
Students
1300
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics