If you are Java developer, then you must have the good knowledge of servlet. As this is the utmost topic of Java EE. Coupling with this, all the frameworks of web applications such as spring and Strut are built on top of it. There are chances that interviewer asked questions from Servlet. So, equip yourself with these questions.
Servlet interview questions
Servlet interview questions
In general, on the very first request of the client, the server container loads a servlet. But in case, the servlet is too heavy that it takes much time to load, at that time we might want it to load during the application startup. To do this, the user can use WebServlet notation or loadOnStartup variable to stimuli container. Or, we can use a load-on-startup element with a servlet configuration in web.xml file to load the servlet on startup.
<servlet> <servlet-name>coo </servlet-name> <servlet-class>com.coo.servlets.Coo </servlet-class> <load-on-startup>6 </load-on-startup> </servlet>
If you want that the container loads the servlet on client request, then the load-on-startup value should be negative otherwise if it’s positive then the loading will start on application startup.
If there are many servlets with different load-on-startup values, then that servlet will be loaded first which have the lowest value.
There are many ways of login-based authentication. These are:
- HTTP Digest Authentication
- HTTP Basic Authentication
- HTTPS Authentication
- Form-based Authentication
Use the below snippet code to get the information:
Use the written code to get the actual path:
Servlet filters are pluggable java components that are used to process requests before the requests are sent to the servlets and before a container sends the response back to the client.
Some tasks performed with filters are:
- Logging request parameters to log files.
- Authorization of request for services
- The arrangement of request body and header before sending it to a servlet.
- Condense the response data sent to client
- Add cookies, header information etc. In response.
It consists of three methods:
- Public void Init: Container initialize the servlet using this method. It is invoked once during the whole life cycle.
- Public void service: For every request, this method is called once at a time.
- Public void destroys: This is called when a servlet is destroyed from memory.
There are main four phases required in Servet Life Cycle. These are:
- Servlet class Loading: On a client request of a servlet, the container first loads the class in the memory and after that calls its default no argument constructor.
- Servlet Class Initialization: After the first phase of loading class in memory, container initiates the ServletContext object and then invokes the Init method by passing servlet Config for the servlet. This phase transforms the normal class to a servlet.
- Request Handling: It is the phase where a servlet is ready to handle the requests. For each client request, it invokes the method service () by passing request and response object reference.
- Removal from service: when the process stops, the container calls the destroy () method and destroy the servlet.
Safety and security are always the priority for any company. Thus, the single Thread Model interface introduces for this reason. The main purpose of this concept is to check whether two or more threads are executing at the same time. Like many threads may be penetrated by many requests, so to execute them in a manner, it is used. But in servlet 2.3, this concept is disapproved as it can stop the multithreading method.
URL Encoding means the changing of data into CGI. It is done to circumvent any type of issue while it is going over the network. URL coding is schemed by removing the white spaces and put any escape symbols. Thus, the special characters are removed. To encode any string java.net.URLEncoder.encode (String str, String Unicode) is used and to decode the URL java.net.URLDecoder.decode (String str, String Unicode) is used. Let's take an example to simplify it. To encode 'Ravi's data' it is changed into 'Ravi/24d+data'.
To design implement of servlet request and response, these wrapper classes are being used. The users are able to increase these classes to revoke the particular techniques that need to be implemented. There are two wrapper classes that are provided by Servlet HTTP API. These classes are
- Http Servlet Response Wrapper
- Http Servlet Request Wrapper
- Print Writer is referred to a character-stream state while Servlet Output Stream is considered as a byte-stream state.
- Print Writer is used to writing the information that is based on the character for example character design and string to the response. While to write the byte design, Servlet Output Stream is used.
- To arrange the Print Writer the clients have to use the Servlet Response get Writer () while to arrange Servlet Output Stream the client have to use Servlet Response get Output Stream () method.
- GET is a safe method (idempotent) where POST is the non-idempotent method.
- GET is an idempotent method and it is safe while the POST method is non-idempotent.
- GET is the part of the header while POST is the content of the body.
- A limited data can be sent with GET while POST method allowed a large data to send
- GET method can easy to bookmark while POST method can't.
- It is not secure to use the Get method as it is possible to send the same request over and over while the POST method is secure.
- GET method is the default method while the POST method needs to be specified.
Servlet API is very easy to use as it involves only one step that is encoding the URL. So, it is very beneficially in respect of coding. Another benefit of using Servlet API over Http session is that it works only when the cookies are disabled.
Http Servlet Response encodes URL () can also be used for redirecting the request to the different resources and encode Redirect URL () method is used in the case of client wants to give some session knowledge.
A session is a colloquial link between the server and the client. Session handles a lot of requests and supplies the answer within the client and server. As both the HTTP and web server are unsettled, so for the maintenance of a session it is necessary to have some bizarre request that has to be passed between the server and the client.
The various method that is used to manage session in servlets are
- HTML Hidden Field
- URL Rewriting
- User Authentication
- Session Management API
Related Interview Questions
Kotlin Interview Questions
Core Java interview questions
Vaadin interview questions
Scala interview questions
Maven interview questions
OpenXava Interview questions
Java Play interview Questions
Groovy interview questions
Hibernate Interview Questions
Java Grails Interview Questions
Apache Ant Interview questions
Apache Kafka Interview Questions
Gradle Interview Questions
JSF Interview Questions
JSP Interview Questions
JUnit Interview Questions
Spring interview questions
Struts interview questions
Spring Boot Interview questions
Servlet interview questions
JDBC interview questions
Subscribe Our NewsLetter
Never Miss an Articles from us.
- Common Interview Questions
- Python Flask Interview Questions
- NoSQL interview questions
- JQuery Interview Questions
- C programming interview questions
- AngularJS Interview Questions
- Node JS Interview Questions with Express
- Core Java interview questions
- HTML Interview Questions
- Laravel interview questions
- Wordpress Interview Questions
- Core PHP Interview Questions