51 Java Servlet Interview Questions And Answers – Servlet Java Interview Questions For Experienced from Codingcompiler. Test your Java servlet knowledge by answering these tricky interview questions on Java servlets. Let’s start learning Java servlet interview questions and prepare for Java interviews. All the best for your future and happy learning.
Java Servlet Interview Questions
- What is a servlet?
- What is the structure of a web project?
- What is a servlet container?
- What are the tasks, functionality of a servlet container?
- What do you know about servlet filters?
- Why are servlet listeners needed?
- When will you use filters, and when will listeners?
- How to handle exceptions thrown by another servlet in an application?
- What is a deployment descriptor?
- How to implement the launch of the servlet with the launch of the application?
- What is the ServletConfig object?
- What is the ServletContext object?
- What are the differences between ServletContext and ServletConfig?
- What is Request Dispatcher?
- How can I create a deadlock in a servlet?
- How to get the servlet address on the server?
- How to get server information from servlet?
- How to get the client’s ip address on the server?
- What do you know about servlet wrappers?
- What is the life cycle of a servlet and when are some methods called?
- What methods need to be determined when creating servlets?
- In which case will you override the service () method?
- Does it make sense to define a constructor for the servlet, how best to initialize the data?
- What are the differences between GenericServlet and HttpServlet?
- How to call another servlet from the same servlet of the same application?
- What do you know and what are the differences between the methods forward () and sendRedirect ()?
- Should I worry about “multithreaded security” when working with servlets?
- What is the difference between a web server and an application server?
- Which HTTP method is not immutable?
- Why is the HttpServlet class declared as abstract?
- What is the difference between GET and POST methods?
- What is the MIME type?
- What are the advantages of Servlet over CGI?
- What are the most common tasks performed in a servlet container?
- What is the difference between PrintWriter and ServletOutputStream?
- Can we get the PrintWriter and ServletOutputStream simultaneously in a servlet?
- Tell us about the SingleThreadModel interface.
- What are the attributes of servlets and what is their scope?
- Why is it necessary to override only the init () method with no arguments?
- What does URL encoding mean? Why do you need java.net.URLEncoder.encode () and decode () methods?
- Why are the methods encodeUrl () and encodeRedirectUrl () needed and how are they different?
- What are the different methods of managing a session in servlets?
- What does URL Rewriting mean?
- How are cookies used in servlets?
- How to notify an object in a session that a session is invalid or has expired?
- What is the effective way to make sure that all servlets are accessible only to a user with a valid session?
- How can we provide transport layer security for our web application?
- How to organize a connection to the base melon and provide log4j logging in the servlet?
- What important features exist in Servlet 3?
- What are the different ways to authenticate a servlet?
- Write a servlet that implements the file upload to the server.
Related Java Interview Questions
- Java IO Interview Questions
- Java String Interview Questions
- Java Collections Interview Questions
- Java Exceptions Interview Questions
- Java OOPS Interview Questions
- Core Java Interview Questions
- JSF Interview Questions
- JSP Interview Questions
- JPA Interview Questions
- Spring Framework Interview Questions
- Spring Boot Interview Questions
- Core Java Multiple Choice Questions
- 60 Java MCQ Questions And Answers
- Aricent Java Interview Questions
- Accenture Java Interview Questions
- Advanced Java Interview Questions For 5 8 10 Years Experienced
- Core Java Interview Questions For Experienced
Java Servlet Interview Questions And Answers
1. What is a servlet?
The servlet is a Java interface whose implementation extends the functionality of the server. The servlet interacts with clients through a request-response principle.
Although servlets can serve any requests, they are usually used to extend web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes. The javax.servlet and javax.servlet.http packages provide interfaces and classes for creating servlets.
2. What is the structure of a web project?
src / main / java | Application / Library sources |
src / main / resources | Application / Library resources |
src / main / filters | Resource filter files |
src / main / webapp | Web application sources |
src / test / java | Test sources |
src / test / resources | Test resources |
src / test / filters | Test resource filter files |
src / it | Integration Tests (primarily for plugins) |
src / assembly | Assembly descriptors |
src / site | Site |
LICENSE.txt | Project’s license |
NOTICE.txt | Project depends on |
README.txt | Project’s readme |
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
3. What is a servlet container?
A servlet container is a program that represents a server that is engaged in system support for servlets and ensures their life cycle in accordance with the rules defined in the specifications.
Known implementations: Apache Tomcat, Jetty, JBoss, GlassFish, IBM WebSphere, Oracle Weblogic.
4. What are the tasks, functionality of a servlet container?
A servlet container can work as a fully-fledged stand-alone web server, be a provider of pages for another web server, such as Apache, or integrate into a Java EE application server. It provides data exchange between the servlet and clients, undertakes such functions as creating a software environment for a functioning servlet, identifying and authorizing clients, and organizing a session for each of them.
5. What do you know about servlet filters?
The servlet filter, according to the specification, is a reusable Java code that allows you to translate the content of HTTP requests, HTTP responses, and information contained in HTTP headers. A servlet filter is involved in preprocessing a request before it enters a servlet, and / or in the subsequent processing of a response coming from a servlet. Servlet filters can:
– intercept the servlet initialization before the servlet is initiated;
– determine the content of the request before the servlet is initiated;
– modify the headers and request data into which the incoming request is packaged;
– modify the headers and response data in which the resulting response is packaged;
– intercept the servlet initialization after accessing the servlet.
A servlet filter can be configured to work with a single servlet or group of servlets. The basis for the formation of filters is the javax.servlet.Filterinterface , which implements three methods:
void init (FilterConfig config) throws ServletException;
void destroy ();
void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;
The init () method is called before the filter starts working and configures the filter configuration object. The doFilter method performs the filter directly. Thus, the server calls init () once to start the filter into operation, and then calls doFilter () as many times as many requests are made directly to this filter. After the filter finishes its work, the destroy () method is called.
6. Why are servlet listeners needed?
Context and session listeners are classes that can keep track of when the context or session has been initialized, or track the time when they should be destroyed, and when attributes have been added or removed from the context or session.
Servlet 2.4 extends the listeners model of the query, allowing you to track how the query is created and destroyed, and how attributes are added and removed from the servlet. The following classes are added to Servlet 2.4:
-
- ServletRequestListener
-
- ServletRequestEvent
-
- ServletRequestAttributeListener
- ServletRequestAttributeEvent
7. When will you use filters, and when will listeners?
Servlet Listener Example: https://www.journaldev.com/1945/servlet-listener-example-servletcontextlistener-httpsessionlistener-and-servletrequestlistener
8. How to handle exceptions thrown by another servlet in an application?
Since the browser only understands HTTP, then when the application throws an exception, the servlet container will handle the exception and create an HTTP response.
This is similar to what happens with error codes like 404, 403, etc. The Servlet API provides native servlet support for handling exceptions and errors that we can specify in the deployment descriptor.
The main task of such servlets is to handle the error or exception and send a clear HTTP response to the user. For example, you can provide a link to the main page, as well as a description of some details about the error.
1
2 3 4 5 6 7 8 9 |
<error-page>
<error-code>404</error-code> <location>/AppExceptionHandler</location> </error-page> <error-page> <exception-type>javax.servlet.ServletException</exception-type> <location>/AppExceptionHandler</location> </error-page> |
9. What is a deployment descriptor?
A deployment descriptor is an artifact configuration file that will be deployed in a servlet container. In the Java Platform, Enterprise Edition specification, the deployment descriptor describes how a component, module or application (such as a web application or an enterprise application) should be deployed.
This configuration file specifies deployment options for a module or application with specific settings, security settings, and describes specific configuration requirements. The syntax for the deployment descriptor files is XML.
10. How to implement the launch of the servlet with the launch of the application?
A servlet container usually loads a servlet when the client first requests it, but sometimes it is necessary to load the servlet right at the start of the application (for example, if the servlet is large and will take a long time to load). To do this, you must use the load-on-startup element in the descriptor (or the loadOnStartup annotation ), which will indicate the need to load the servlet at startup.
1
2 3 4 5 |
<servlet>
<servlet-name>foo</servlet-name> <servlet-class>com.foo.servlets.Foo</servlet-class> <load-on-startup>5</load-on-startup> </servlet> |
The value must be an int . If the value is negative, the servlet will be loaded when the client requests, and if 0 and further, it will be loaded at the start of the application. The smaller the number, the earlier the servlet will be in the download queue.
Servlets in Java Interview Questions Answers
11. What is the ServletConfig object?
The javax.servlet.ServletConfig interface is used to transfer configuration information to the servlet. Each servlet has its own ServletConfig object, the creation of which is the responsibility of the servlet container. To set configuration parameters, init parameters are used in the web.xml (or WebInitParam annotations). To get the ServletConfig object of this servlet, use the getServletConfig () method .
12. What is the ServletContext object?
The javax.servlet.ServletContext interface provides access to the web application parameters to the servlet. The ServletContext object is unique and accessible to all servlets of the web application. We can use the ServletContext object when we need to provide access to one or more servlets to the initialized parameters of a web application. To do this, use the <context-param> element in web.xml . You can get the ServletContext object using the getServletContext () method from the ServletConfig interface .
Servlet containers can also provide context objects unique to a servlet group. Each group will be associated with its own set of host path URLs.
The ServletContext has been expanded into the Servlet 3 specification and provides programmatic addition of listeners and filters to the application. Also, this interface has many useful methods like getMimeType () , getResourceAsStream () , etc.
13. What are the differences between ServletContext and ServletConfig?
-
- ServletConfig is a unique object for each servlet, while a ServletContext is unique for the entire application.
-
- ServletConfig is used to provide initialization parameters to the servlet, and ServletContext to provide application initialization parameters for all servlets.
- We do not have the ability to set attributes in a ServletConfig object , while it is possible to set attributes in a ServletContext object that will be available to other servlets.
14. What is Request Dispatcher?
The RequestDispatcher interface is used to send a request to another resource (it can be HTML, JSP, or another servlet in the same application). We can use this to add content from another resource to the response. This interface is used for internal communication between servlets in the same context. The interface implements two methods:
void forward (ServletRequest var1, ServletResponse var2) – sends a request from a servlet to another resource (servlet, JSP or HTML file) on the server.
void include (ServletRequest var1, ServletResponse var2) – includes the content of the resource (servlet, JSP or HTML page) in the response.
The interface can be accessed using the ServletContext method getRequestDispatcher (String s). The path must begin with / , which will be interpreted relative to the current root path of the context.
15. How can I create a deadlock in a servlet?
Dedlock can be obtained by implementing a looped method call, for example, by calling the doPost () method in the doGet () method and calling doGet () in the doPost () method .
16. How to get the servlet address on the server?
To get the actual servlet path on the server, you can use this construct: getServletContext (). GetRealPath (request.getServletPath ())
17. How to get server information from servlet?
Server information can be obtained using the ServletContext object using the getServerInfo () method . Those. getServletContext (). getServerInfo () .
18. How to get the client’s ip address on the server?
Use request.getRemoteAddr () to get the client’s ip in the servlet.
19. What do you know about servlet wrappers?
The Servlet HTTP API provides two wrapper classes – HttpServletRequestWrapper and HttpServletResponseWrapper . They help developers implement their own implementation of the servlet’s request and response types . We can extend these classes and override only the necessary methods to implement our own types of response and query objects. These classes are not used in standard servlet programming.
20. What is the life cycle of a servlet and when are some methods called?
A servlet container manages four phases of a servlet’s life cycle:
-
- Servlet class loading – when a container receives a request for a servlet, the servlet class is loaded into memory and a constructor without parameters is called.
-
- Initialization of the servlet class – after the class is loaded, the container initializes the ServletConfig object for this servlet and injects it through the init () method. This is where the servlet class is converted from a regular class into a servlet.
-
- Request Processing – After initialization, the servlet is ready to process requests. For each client request, the servlet container spawns a new thread (stream) and calls the service () method by passing a reference to the response and request object.
- Deletion from Service — when a container stops or stops an application, the servlet container destroys the servlet classes by calling the destroy () method.
It can be described as a sequence of calling methods: init () , service () , destroy () .
-
- public void init (ServletConfig config) – used by the container to initialize the servlet. Called once per servlet lifetime.
-
- public void service (ServletRequest request, ServletResponse response) – called for each request. The method cannot be called before the init () method is executed .
- public void destroy () – called to destroy a servlet (once during the life of the servlet).
Advanced Java Servlet Interview Questions and Answers
21. What methods need to be determined when creating servlets?
To create a servlet, you must describe the servlet with:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<servlet-mapping>
<servlet-name>MyOwnDefaultServlet</servlet-name> <url-pattern>/myservlet/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>HelloWorld2</servlet-name> <servlet-class>examples.servlets.HelloWorld2</servlet-class> <init-param> <param-name>greeting</param-name> <param-value>Welcome</param-value> </init-param> <init-param> <param-name>person</param-name> <param-value>WebLogic Developer</param-value> </init-param> </servlet> |
Then extend the servlet class from the HttpServlet class .
Implement the service () or doGet () , doPost () method (or the first or second).
22. In which case will you override the service () method?
The service () method is redefined when we want the servlet to handle both GET and POST requests in the same method. When the servlet container receives a client request, the service () method is called , which in turn calls the doGet () , doPost () methods based on the HTTP request method. It is believed that the service () method does not redefine much, except for the case of using one method for two types of requests specified at the beginning.
23. Does it make sense to define a constructor for the servlet, how best to initialize the data?
This possibility is, but is considered meaningless. It is better to initialize the data by overriding the init () method , in which you can access the initialization parameters of the servlet through the use of the ServletConfig object .
24. What are the differences between GenericServlet and HttpServlet?
Abstract class GenericServlet is a protocol -independent implementation of the Servlet interface . HttpServlet , as the name implies , is a servlet interface implementation for the HTTP protocol. It should be noted that HttpServlet extends GenericServlet.
25. How to call another servlet from the same servlet of the same application?
If it is necessary to invoke a servlet from the same application, then it is necessary to use the internal servlet communication mechanism (inter-servlet communication mechanisms).
We can call another servlet using the RequestDispatcher forward () and include () methods to access additional attributes in the request for use in another servlet. The forward () method is used to transfer the processing request to another servlet. The include () method is used if we want to attach the result of another servlet to the returned response.
If it is necessary to call a servlet from another application, then RequestDispatcher will not work (determined for the application). Therefore, you can use the ServletResponse sendRedirect () method and provide the full URL from another servlet. To transfer data, you can use cookies as part of the servlet’s response, and then use them in our servlet.
26. What do you know and what are the differences between the methods forward () and sendRedirect ()?
-
- RequestDispatcher forward () is used to send the same request to another resource, while ServletResponse sendRedirect () is a two-step method. In the second method, the web application returns a response to the client with status code 302 (redirect) with a link to send a request. The request sends a completely new request.
-
- forward () is processed inside the container, and sendRedirect () is processed by the browser.
-
- It is necessary to use forward () to organize access within the same application, since it is faster than sendRedirect () , which requires additional networking.
-
- In the forward () method, the browser does not know about the resource being actually processed and the URL in the string remains the same. In the sendRedirect () method, the URL is changed to the forwarding resource.
- In the forward () method, you cannot use to embed a servlet into another context. For this you can use only sendRedirect () .
27. Should I worry about “multithreaded security” when working with servlets?
The methods of the class HTTPServlet init () and destroy () are called once during the life cycle of the servlet – so there’s no need to worry about them. The doGet () , doPost () methods are called for each client request and since servlets use multithreading, then you need to think about thread-safe work.
If there are local variables in these methods, there is no need to think about multi-threaded security, since they will be created separately for each thread. But if global resources are used, then you need to use synchronization as in any multithreaded Java application.
28. What is the difference between a web server and an application server?
A web server is required to process an HTTP request from a client browser and respond to a client using an HTTP response. The web server understands the HTTP language and runs over the HTTP protocol. An example of a web server is an implementation from Apache – Tomcat.
The application server provides additional features, such as support for JavaBeans, JMS Messaging, Transaction Management, etc. You can say that the application server is a web server with additional features that help develop enterprise applications.
29. Which HTTP method is not immutable?
An HTTP method is called immutable if it always returns the same result. HTTP methods GET, PUT, DELETE, HEAD, OPTIONS are immutable. You must implement the application so that these methods return the same result. Variable methods include the HTTP POST method. Post method is used to implement something that changes with each request.
For example, to access an HTML page or image, you must use the GET method, since it returns the same result. But if we need to save the order information in the database, then we need to use the POST method. Immutable methods are also known as safe methods and there is no need to worry about repeated requests from the client for these methods.
30. Why is the HttpServlet class declared as abstract?
The HTTPServlet class provides the HTTP implementation of the servlet protocol (and therefore helps avoid constant coding of duplicate information), but it does not have the implemented methods doGet () and doPost () (they have HTTP 405 Method Not Implemented error by default), and therefore declared abstract. The implementation of these methods is passed on to the developer.
Java Servlet Interview Questions And Answers For Experienced
31. What is the difference between GET and POST methods?
-
- The GET method is immutable, while POST is mutable.
-
- Using the GET method, you can send a limited number of data that will be sent in the URL header. In the case of the POST method, we can send large amounts of data, because they will be in the body of the method.
-
- GET method data is transmitted in clear text, which can be used for malicious purposes. POST data is transmitted in the request body and hidden from the user.
-
- The GET method is the default HTTP method, and the POST method must be specified explicitly to send a request.
- GET method is used by hyperlinks on the page.
32. What is the MIME type?
MIME (pronounced “maym”, English. Multipurpose Internet Mail Extensions – multi-purpose Internet mail extensions) – a standard that describes the transmission of various types of data via e-mail, as well as, in general, the specification for encoding information and formatting messages in such a way so that they can be sent over the Internet. The Content-Type response header is the MIME type.
The server sends the MIME type to the client so that it understands what type of data is being sent. This helps to display the data correctly on the client. The most commonly used MIME types are: text / html , text / xml , application / xml, and many others.
In the ServletContext, there is a getMimeType () method to get the correct MIME file type and then use this information to specify the type of content in the response.
33. What are the advantages of Servlet over CGI?
Servlet technology was created to overcome the weaknesses of the Common Gateway Interface (common gateway interface). The following advantages of servlets over CGI can be highlighted:
-
- Servlets provide better performance in terms of processing requests, better memory usage by using the advantage of multithreading (a new thread is created for each request, which is faster than allocating memory for a new object for each request, as in CGI).
-
- Servlets, platform and system are independent. Thus, a web application written using servlets can be run in any servlet container that implements the standard and in any operating system.
-
- Servlet usage improves program reliability, because the servlet container itself takes care of the servlet life cycle (and therefore memory leaks), security, and garbage collection.
- Servlets are relatively easy to learn and maintain, so the developer only needs to care about the business logic of the application, and not the internal implementation of web technologies.
34. What are the most common tasks performed in a servlet container?
-
- Support data exchange . A servlet container provides an easy way to exchange data between a web client (browser) and a servlet. Thanks to the container, there is no need to create a socket listener on the server to track requests from the client, as well as parse the request and generate a response. All these important and complex tasks are solved using the container and the developer can focus on the business logic of the application.
-
- Servlet and resource lifecycle management . Starting from loading the servlet into memory, initializing, implementing methods and ending with the destruction of the servlet. The container also provides additional utilities, such as JNDI, for managing a pool of resources.
-
- Multithreading support . The container independently creates a new thread for each request and provides it with a request and response for processing. Thus, the servlet is not reinitialized for each request and thus saves memory and reduces the time to process the request.
-
- JSP support . JSP classes are not similar to standard Java classes, but the servlet container converts each JSP to a servlet and is then managed by the container as a regular servlet.
- Various tasks . The servlet container manages the resource pool, application memory, and garbage collector. Provides security settings and more.
35. What is the difference between PrintWriter and ServletOutputStream?
PrintWriter is a class for working with a character stream, and ServletOutputStream is a class for working as a byte stream. PrintWriter is used to write character-based information, such as an array of characters or a string in the response, while ServletOutputStream is used to write a byte array to the response.
To get an instance of ServletOutputStream, use the ServletResponse getOutputStream () method , and for PrintWriter , use the ServletResponse getWriter () method .
36. Can we get the PrintWriter and ServletOutputStream simultaneously in a servlet?
We cannot create two objects of these classes in one servlet. If we try to embed both getWriter () and getOutputStream () methods in the response, we will get a java.lang.IllegalStateException exception with the message that a different method has already been called for this answer.
37. Tell us about the interface SingleThreadModel.
The interface was created to guarantee thread safety and the impossibility of creating two threads in the servlet method service. However, the SingleThreadModel interface does not solve all thread safety issues.
For example, session attributes or static variables can be accessed by different requests in different threads at the same time (even when using this interface). In general, it killed all the multithreading profit and the interface was declared deprecated since Servlet 2.4.
38. What are the attributes of servlets and what is their scope?
Servlet attributes are used for internal servlet communication. We can use the attributes set, get, remove in a web application. There are three attribute scopes – request scope , session scope , application scope .
The ServletRequest , HttpSession, and ServletContext interfaces provide methods for the get () , set () , remove () attributes from the request scope, session scope, application scope, respectively.
39. Why is it necessary to override only the init () method with no arguments?
If we need to initialize some resources before the servlet starts processing requests, then we need to override the init () method . If you override the init method (ServletConfig config ), the super (config) method must be called first , which will call the init method (ServletConfig config) of the superclass.
That is why GenericServlet provides another init () method without parameters, which will be called at the end of the init method (ServletConfig config) . The developer should use the override init () method without parameters to initialize the variables to avoid any problems, for example without specifying a callsuper () in the overridden init method (ServletConfig config) .
40. What does URL encoding mean? Why do we need methods encode () and decode ()?
URL Encoding is the process of converting data into a Common Gateway Interface (CGI) form that will allow you to surf the net without problems. URL Encoding separates spaces and replaces special characters with escape characters.
For example, the java.net.URLEncoder.encode (String str, String unicode) method is used to encode a string . The reverse operation of decoding is possible thanks to the java.net.URLDecoder.decode method (String str, String unicode) .
Top 51 Java Servlet Interview Questions And Answers
41. Why are the methods encodeUrl () and encodeRedirectUrl () needed and how are they different?
HttpServletResponse provides methods for converting URLs to HTML hyperlinks with the conversion of special characters and spaces, as well as adding session id to the URL. This behavior is similar to URLEncoder encode () , but with the addition of the additional parameter jsessionid at the end of the URL.
The HttpServletResponse method encodeRedirectUrl () is used to convert the redirect URL in the response. Thus, when providing URL rewriting support for HTML hyperlinks, you must use encodeURL () , and for redirect URLs, use encodeRedirectUrl () .
42. What are the different methods of managing a session in servlets?
A session is a normal state of interaction between the server and the client, and may contain many requests and responses from the client / server. Since HTTP and web server do not remember state (stateless), the only way to maintain the session is to send unique information (session id) in each request and response between the client and the server.
There are several common ways to manage servlet sessions:
-
- User authentication
-
- HTML hidden field
-
- Cookies
-
- URL Rewriting
- Session management API
43. What does URL Rewriting mean?
We can use HTTPSession to manage a session in servlets , but it works with cookies, and they are sometimes disabled. For this case, the servlet provides URL Rewriting. From a programming point of view, only one action is needed – URL coding. Another advantage is that this method is like a backup and is turned on only when the cookies are turned off.
Using the HttpServletResponse encodeURL () method, we can encode the URL. If redirecting to another resource is necessary, then the encodeRedirectURL () method is used to provide information about the session .
44. How are cookies used in servlets?
Cookies (cookies) are used in client-server interaction and they are not specific to Java. Servlet API provides cookies support through the javax.servlet.http.Cookie class implements Serializable, Cloneable . To get an array of cookies from the request, you must use the HttpServletRequest getCookies () method . There are no methods for adding cookies to the request.
Similar to HttpServletResponse addCookie (Cookie c) – can add cookies in the response header, but there is no getter for this type of data transfer.
45. How to notify an object in a session that a session is invalid or has expired?
To be sure that the object has been notified about the session termination, the object must implement the javax.servlet.http.HttpSessionBindingListener interface . Two methods of this interface: valueBound () and valueUnbound () are used to implement logic when adding an object as an attribute to a session and when a session is destroyed.
46. What is the effective way to make sure that all servlets are accessible only to a user with a valid session?
Servlet filters are used to intercept all requests between the servlet container and the servlet. Therefore, it is logical to use a filter to check the necessary information (for example, the validity of the session) in the request.
47. How can we provide transport layer security for our web application?
For this, you must configure SSL for your servlet container. How to do this is described in the manuals for the specific implementation of the container.
48. How to organize a connection to the database and provide log4j logging in the servlet?
When working with a large number of database connections, it is recommended to initialize them in the servlet context listener and set the context attribute to be used by other servlets. Log4j logging is connected using an XML configuration (or property file) and further this information is used when configuring the context listener.
49. What important features exist in Servlet 3?
-
- Servlet Annotations . Before Servlet 3, the entire mapping was in web.xml, which led to errors and was trivially inconvenient with a large number of servlets. Examples of annotations: @WebServlet , @WebInitParam , @WebFilter , @WebListener .
-
- Web Fragments. With the advent of web fragments, we can contain a lot of modules in a single-page web application. All modules are written in the fragment.xml in the META-INF directory. This allows you to split a web application into separate modules included as JAR files in a separate lib directory.
-
- Dynamic addition of web components . Using the ServletContext object, we can programmatically add filters and listeners. This helps to build a dynamic system in which the required object will be called only when necessary. To do this, use the addServlet () , addFilter () , addListener () methods .
- Asynchronous execution . Support for asynchronous processing allows you to transfer the execution of the request to another thread without keeping the entire server busy.
50. What are the different ways to authenticate a servlet?
A servlet container provides various authentication methods:
-
- HTTP Basic Authentication
-
- HTTP Digest Authentication
-
- HTTPS Authentication
- Form Based Login
51. Write a servlet that implements the file upload to the server.
Servlet Upload File and Download File Example: https://www.journaldev.com/1964/servlet-upload-file-and-download-file-example
Must Read Java Interview Questions Books
RELATED INTERVIEW QUESTIONS
- Java Web Interview Questions
- Java Multithreading Interview Questions
- Java IO Interview Questions
- Java String Interview Questions
- Java Collections Interview Questions
- Java Exceptions Interview Questions
- Java OOPS Interview Questions
- Core Java Interview Questions
- JSF Interview Questions
- JSP Interview Questions
- JPA Interview Questions
- Spring Framework Interview Questions
- Spring Boot Interview Questions
- Core Java Multiple Choice Questions
- 60 Java MCQ Questions And Answers
- Aricent Java Interview Questions
- Accenture Java Interview Questions
- Advanced Java Interview Questions For 5 8 10 Years Experienced
- Core Java Interview Questions For Experienced
- GIT Interview Questions And Answers
- Network Security Interview Questions
- CheckPoint Interview Questions
- Page Object Model Interview Questions
- Apache Pig Interview Questions
- Python Interview Questions And Answers
- Peoplesoft Integration Broker Interview Questions
- PeopleSoft Application Engine Interview Questions
- RSA enVision Interview Questions
- RSA SecurID Interview Questions
- Archer GRC Interview Questions
- RSA Archer Interview Questions
- Blockchain Interview Questions