You use the ServletConfig object to give configuration information to a specific servlet. Each servlet has a unique ServletConfig object and the servlet container instantiates it. You can give servlet init parameters through the WebInitParam annotation. To get the ServletConfig object of a servlet, you’d have to use the getServletConfig() method.
Posted Date:- 2021-08-19 00:04:09
The CGI technology had many shortcomings. Servlets were introduced to overcome the same.
Servlets offer better performance than CGI in terms of utilising memory and processing time. They use the benefits of multithreading, where they create a new thread for every request, enhancing their speed greatly. In contrast, CGI creates a new Object for every request, which is relatively slower than the servlets’ process.
Servlets are system and platform-independent. You can run a servlet-based web application on any standard web container (Glassfish, Tomcat, and JBoss) and operating systems (Unix, Windows, Mac, etc.).
The learning curve for servlets is pretty small as you only need to handle the business logic for the application. Moreover, their container handles the servlet’s life cycle, so there’s no risk of memory leaks, garbage collection, and security.
Posted Date:- 2021-08-19 00:03:25
A web application is a module that runs on the server to provide dynamic and static content to the client browser. The Apache web server supports PHP, and you can create a web application using the same.
Posted Date:- 2021-08-19 00:01:41
The primary difference between the POST and GET methods is that the POST method carries the response parameters in the message body while the GET method carries the response parameters appended in the URL string.
Posted Date:- 2021-08-19 00:00:52
MIME stands for Multipurpose Internet Mail Extension. The MIME type is an HTTP header that gives information about what we’re sending to a browser. It helps the client in data rendering. Common MIME types are text (HTML), text (plain), images (jpeg), application (jar), etc.
To get the correct MIME type of a particular file, you can use the ServletContext getMimeType() method. It comes in handy while downloading a file through servlets from a server.
Posted Date:- 2021-08-19 00:00:12
Session tracking is a technique that servlets use to maintain a record of a series of requests originating from the same user or the same browser across a period of time. These sessions are shared between the servlets that are accessed by a client.
There are 4 techniques to track sessions:
1. Cookies
2. Hidden Fields.
3. URL Rewriting.
4. Session Tracking API.
Posted Date:- 2021-08-18 23:59:09
The container is accountable for writing constructor without arguments in Servlet.
Posted Date:- 2021-08-18 23:29:29
The types of session tracking are:
1. URL rewriting
2. Cookies
3. Secure Socket layer
4. Hidden form fields
Posted Date:- 2021-08-18 23:28:29
We know that using ServletContext, we can create an attribute with application scope that all other servlets can access but we can initialize ServletContext init parameters as String only in the deployment descriptor (web.xml). What if our application is database-oriented and we want to set an attribute in ServletContext for Database Connection.
If your application has a single entry point (user login), then you can do it in the first servlet request but if we have multiple entry points then doing it everywhere will result in a lot of code redundancy. Also if the database is down or not configured properly, we won’t know until the first client request comes to the server. To handle these scenarios, servlet API provides Listener interfaces that we can implement and configure to listen to an event and do certain operations.
Posted Date:- 2021-08-18 23:26:57
URL Encoding is the process of converting data into CGI form so that it can travel across the network without any issues. URL Encoding strips the white spaces and replaces special characters with escape characters. We can use java.net.URLEncoder.encode(String str, String unicode) to encode a String. URL Decoding is the reverse process of encoding and we can use java.net.URLDecoder.decode(String str, String unicode) to decode the encoded string. For example “Rahul’s Data” is encoded to “Rahul%27s+Data”.
Posted Date:- 2021-08-18 23:21:33
HttpServlet class provide HTTP protocol implementation of servlet but it’s left abstract because there is no implementation logic in service methods such as doGet() and doPost() and we should override at least one of the service methods. That’s why there is no point in having an instance of HttpServlet and is declared abstract class.
Posted Date:- 2021-08-18 23:17:12
We can’t use RequestDispatcher to invoke servlet from another application because it’s specific for the application. If we have to forward the request to a resource in another application, we can use the ServletResponse sendRedirect() method and provide the complete URL of another servlet. This sends the response to the client with the response code as 302 to forward the request to another URL. If we have to send some data also, we can use cookies that will be part of the servlet response and sent in the request to another servlet.
Posted Date:- 2021-08-18 23:16:08
We can use RequestDispatcher forward() method to forward the processing of a request to another servlet. If we want to include the another servlet output to the response, we can use RequestDispatcher include() method.
Posted Date:- 2021-08-18 23:15:28
Servlet attributes are used for inter-servlet communication, we can set, get and remove attributes in web application. There are three scopes for servlet attributes – request scope, session scope and application scope.
ServletRequest, HttpSession, and ServletContext interfaces provide methods to get/set/remove attributes from request, session anServlet attributes are different from init parameters defined in web.xml for ServletConfig or ServletContext.d application scope respectively.
Posted Date:- 2021-08-18 23:14:54
When we want to invoke another servlet from a servlet service methods, we use inter-servlet communication mechanisms. We can invoke another servlet using RequestDispatcher forward() and include() methods and provide additional attributes in request for other servlet use.
Posted Date:- 2021-08-18 23:13:24
We can define a constructor for servlet but I don’t think it’s of any use because we won’t be having access to the ServletConfig object until unless servlet is initialized by the container. Ideally, if we have to initialize any resource for the servlet, we should override init() method where we can access servlet init parameters using ServletConfig object.
Posted Date:- 2021-08-18 23:12:47
When servlet container receives client request, it invokes the service() method which in turn invokes the doGet(), doPost() methods based on the HTTP method of request. I don’t see any use case where we would like to override the service() method. The whole purpose of service() method is to forward to request to corresponding HTTP method implementations. If we have to do some pre-processing of request, we can always use servlet filters and listeners.
Posted Date:- 2021-08-18 23:11:08
Servlet HTTP API provides two wrapper classes – HttpServletRequestWrapper and HttpServletResponseWrapper. These wrapper classes are provided to help developers with custom implementation of servlet request and response types. We can extend these classes and override only specific methods we need to implement for custom request and response objects. These classes are not used in normal servlet programming.
Posted Date:- 2021-08-18 23:09:07
We can create deadlock in servlet by making a loop of method invocation, just call doPost() method from doGet() method and doGet() method to doPost() method to create deadlock situation in servlet.
Posted Date:- 2021-08-18 23:07:52
PrintWriter is a character-stream class whereas ServletOutputStream is a byte-stream class. We can use PrintWriter to write character based information such as character array and String to the response whereas we can use ServletOutputStream to write byte array data to the response.
We can use ServletResponse getWriter() to get the PrintWriter instance whereas we can use ServletResponse getOutputStream() method to get the ServletOutputStream object reference.
Posted Date:- 2021-08-18 23:07:19
Following method returns a name for the locale's country that is appropriate for display to the user.
String getDisplayCountry()
Posted Date:- 2021-08-18 23:03:21
This is a particular cultural or geographical region. It is usually referred to as a language symbol followed by a country symbol which is separated by an underscore. For example "en_US" represents english locale for US.
Posted Date:- 2021-08-18 23:02:27
This means adding resources to a web site to adapt it to a particular geographical or cultural region for example Hindi translation to a web site.
Posted Date:- 2021-08-18 23:01:55
Setting cookies with servlet involves three steps:
(1) Creating a Cookie object: You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.
Posted Date:- 2021-08-18 23:00:48
Servlets handles form data parsing automatically using the following methods depending on the situation:
1. getParameter(): You call request.getParameter() method to get the value of a form parameter.
2. getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
3. getParameterNames(): Call this method if you want a complete list of all parameters in the current request.
Posted Date:- 2021-08-18 23:00:11
The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.
Posted Date:- 2021-08-18 22:56:03
Servlets perform the following major tasks:
1. 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.
2. 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.
3. 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.
4. Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
5. Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.
Posted Date:- 2021-08-18 22:53:19
This is one of the servlet initialization and it is initialized when it is called for the first time.
Posted Date:- 2021-08-18 22:49:03
Servlets are used for server side config and it keeps on server. But, Applets are used for client side coding and it runs on client browsers.
Posted Date:- 2021-08-18 22:48:25
Pure servlet is servlet which is used to create java objects that can be implemented from javax.servlet.Servlet interface.
Posted Date:- 2021-08-18 22:45:45
On the client side, Meta http is used for refresh and server push is used for server side refresh.
Posted Date:- 2021-08-18 22:45:01
A server can provide service to the client and it contains one or more containers such as EJBs, Servlet, JSP containers. Containers hold set of objects.
Posted Date:- 2021-08-18 22:44:34
A scriptlet contains any language statements, variables, expressions that can be valid in the page scripting language. Scriptlet is a part of generated servlet service method.
Posted Date:- 2021-08-18 22:43:35
A servlet container which does not initialize at the start up, this is known as servlet lazy loading.
Posted Date:- 2021-08-18 22:42:33
URL rewriting is one of the methods of session tracking in which additional data is appended at the end of each URL. This additional data identifies the session.
Posted Date:- 2021-08-18 22:41:42
Every HTTP request needs to be captured by HTTP protocol and for that, state is captured. Tracking of state is called session tracking.
Posted Date:- 2021-08-18 22:41:16
HttpServlet supports only HTTP and HTTPS protocol.
Posted Date:- 2021-08-18 22:40:25
Following are the features added in Servlet 2.5:
1. Dependency on J2SE 5.0
2. Support for annotations
3. Loading the class
4. Several web.xml
5. Removed restrictions
6. Edge case clarifications
Posted Date:- 2021-08-18 22:39:49
Refresh in Client side and Server Push can be performed to refresh automatically when new data is entered into the database.
Posted Date:- 2021-08-18 22:38:24
A filter is nothing but a piece of code which can be reusable that will be transforming the content of HTTP requests, response and header information.
Posted Date:- 2021-08-18 22:37:59
A web container is also called Servlet container and is used to interact with the Servlet and contains all the Servlet, JSP, XML files in it. Web container manages the life cycle of a servlet and helps to map the URL to a specific servlet. Web container creates the object of a servlet.
Posted Date:- 2021-08-18 22:36:34
There are basically three lifecycle methods of a servlet.
These are:
Init (),
Service (),
Destroy ()
Posted Date:- 2021-08-18 22:36:05
The difference between the Http Servlet and Generic Servlet is
the Generic Servlet can handle all types of requests. As it has a service () method, it is independent, whereas Http Servlet extends the generic servlet and supports the HTTP methods such as doGet (), doPost (), doHead (), doTrace (), etc.
Posted Date:- 2021-08-18 22:34:51
Servlet can be implemented in code by simply extending the Httpservlet or generic servlet class.
Posted Date:- 2021-08-18 22:33:42
The advantages of the servlet are as follows:
1. Servlet creates a thread for each incoming request and not process, thus it is faster.
2 Servlet is platform-independent as it is based on Java Programming Language.
3. As it is based on Java, it is also robust and secure.
Posted Date:- 2021-08-18 22:33:10
A Servlet is unloaded when:
Administrator manually unloads
Server shuts down
Posted Date:- 2021-08-18 22:31:30
Servlet context holds Servlet view of web application in which Servlet will be in a row. By using the context,
1. Set and store attributes
2. Log events
3. Obtain URL references to resources
Posted Date:- 2021-08-18 22:30:44
The important functions of filters are:
1. Logging and auditing
2. Security check
3. Response compression
4. Data compression
5. Modifying the response
Posted Date:- 2021-08-18 22:29:40
Servlet Mapping is a connection mapping between Servlet and a URL pattern. It is used to map Servlet with the needs.
Posted Date:- 2021-08-18 22:26:57
Servlet is a Java program that is managed by a container called servlet engine. It generates active content and interacts with the client through demand and Response.
Posted Date:- 2021-08-18 22:25:07