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
Servlet technology was introduced to overcome the limitations of CGI technology. These drawbacks are:
- Servlet technology is better than the CGI in performance with the respect of the timing process. It is possible because Servlet uses the multithreading technology. By this, a new thread has been created at every request which is faster than the loading generating fresh request Object for every request with CGI
- Servlet technology is better than CGI in memory utilization.
- It is efficient to use Servlets as the web application that develops on it can work on a standard web container, for example, JBoss, Tomcat, Glassfish servers.
- It can also run on several operating systems like Linux, Windows, Solaris, Mac, Unix etc.
- The very important point of choosing Servlet is that the users don't have to worry about the security, memory leaks, garbage collection etc. container maintain servlet
- Servlets is easy to use as there is nothing to learn except business logic
- Servlets are manageable and the knowledge trajectory is little.
- 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.
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.
If any HTTP brings the same result every time then it means it is idempotent. We implement application when need the same result at every request. Some of the HTTP methods are PUT, DELETE, HEAD, GET, OPTIONS. While that HTTP which brings the different result at every request is said to be non- idempotent. The POST method is the example of non- idempotent. As opposed to the above, POST method is implanted when there is need of something that changes at every request. Let's take an example to simplify it.
We use the method GET at the time of accessing in an HTML page because it will ever pay the same result. Post method is used when there is need to save customer's information to the database. idempotent methods are referred to as a safe method because it does not give the repetitive request from the user.
Request Dispatcher is used to invoking a specific application. As it is used for a specific application, we can't use this method for every application. In the case of sending a request to the resource in a different application, it is better to use Servlet Response send Redirect () and provides the full URL of another servlet. When it is done, it forwards the clients a response which has a response code to send the invoke to another URL. Cookies are also available to send the data to the different servlet.
Web applications provide the client browser both the unvarying and changing content. PHP is understood by Apache web browser and thus clients are able to create web applications using PHP. In a servlet container, the web application is supported by the Servlets and JSPs. And these web applications are provided by the Java.
Request Dispatcher interface is used as a dispatcher in Servlet. It dispatches the requests to different sources. The source can be of any type it may be a servlet, JSP or HTML. Request Dispatcher interface is also practiced to add the material of resources.
- 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.
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:
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'.
Inter-servlet is used to request different servlet from servlet services techniques. It can be possible with Request Dispatcher forward () and include () programs. It also gives you the more features in a invoke for using a different servlet.
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
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.
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.
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
- Express and Node JS Interview Questions
- Core Java interview questions
- HTML Interview Questions
- Laravel interview questions
- Wordpress Interview Questions
- Core PHP Interview Questions