Top 50 Servlet Interview Questions & Answers . it contains one or more containers such as EJBs, Servlet, JSP containers. Download PDF. 50 Servlet Interview Questions and Answers _ vitecek.info - Download as Java provides web application support through Servlets and JSPs that can run in . Here I am providing a list of 50 servlet interview questions with answers to help . Java provides web application support through Servlets and JSPs that can run .
|Language:||English, Spanish, Portuguese|
|PDF File Size:||9.88 MB|
|Distribution:||Free* [*Regsitration Required]|
Dear readers, these JSP Interview Questions have been designed specially to get you continue based on further discussion and what you answer: JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has. + Java Servlets Interview Questions and Answers, Question1: Can we use the A Servlet and JSP containers are collectively referred to as Web containers. JSP Interview Questions Collection of frequently asked interview questions from JSP. Normally a Jsp page is similar to a HTML page but it is actually a servlet.
How do I implement security for my web application? What happens if i call destroy from init? Why can't a container call constructor having parameters? Why do servlets have an init method? Can't we make use of the servlet constructor for initialization? Explain the life cycle methods of a Servlet. Explain the directory structure of a web application view answer Q.
What are the common mechanisms used for session tracking? What is the difference between Difference between doGet and doPost? What is the difference between ServletContext and ServletConfig? What is the difference between HttpServlet and GenericServlet? Servlet interview questions!!! Exception handling in Servlet and JSP specifications In the previous section, you looked at the general principles in exception handling without a J2EE tilt. In this section, we will cover what servlet specification has to say about exception handling.
Consider the doGet method signature in a HttpServlet. ServletException has overloaded constructors to wrap the actual exception. Servlet specification provides exception-handling support through web.
In web. If the UnhandledException is a subclass of ServletException and none of the error-page declaration containing exception-type fit the class hierarchy of the thrown exception, then the Servlet container gets the wrapped exception using the ServletException. Then the container attempts again to match the error-page declaration.
You have to throw a ServletException or its subclass by wrapping the UnhandledException in it and the servlet container does rest of the magic. There are times when the user cannot see a page due to incorrect access rights or the page simply does not exist. For instance, corresponds to Page not found, corresponds to Internal Server Error and so on.
These can throw runtime exceptions.
JspException is a direct subclass of java. Hence it is a best practice to assign error pages in JSPs with the declarative: We can use getServletConfig method to get the ServletConfig object of the servlet. ServletContext interface provides access to web application parameters to the servlet. The ServletContext is unique object and available to all the servlets in the web application.
When we want some init parameters to be available to multiple or all of the servlets in the web application, we can use ServletContext object and define parameters in web. Servlet containers may also provide context objects that are unique to a group of servlets and which is tied to a specific portion of the URL path namespace of the host.
ServletContext is enhanced in Servlet Specs 3 to introduce methods through which we can programmatically add Listeners and Filters and Servlet to the application. We can also use this to include the content of another resource to the response. This interface is used for inter-servlet communication in the same context.
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.
IllegalStateException at runtime with message as other method has already been called for this response. 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. Read more about deadlock in multithreading at Java Deadlock Example.
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. However SingleThreadModel does not solve all thread safety issues.
For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. Also it takes out all the benefits of multithreading support of servlets, thats why this interface is Deprecated in Servlet 2.
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. 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. Ideally if we have to initialize any resource for servlet, we should override init method where we can access servlet init parameters using ServletConfig object.
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.
But service methods such as doGet or doPost are getting called in every client request and since servlet uses multithreading, we should provide thread safety in these methods. The thread safety mechanisms are similar to thread safety in standalone java application, read more about them at Thread Safety in Java. 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. Servlet attributes are different from init parameters defined in web.
Read more about abstract class. We know that Servlet Container manages the life cycle of Servlet, there are four phases of servlet life cycle. If we have to initialize some resource before we want our servlet to process client requests, we should override init method.
If we override init ServletConfig config method, then the first statement should be super config to make sure superclass init ServletConfig config method is invoked first. We should always utilize this method for overriding init method to avoid any issues as we may forget to add super call in overriding init method with ServletConfig argument.
URL Encoding is the process of converting data into CGI form so that it can travel across the network without any issues. URL Encoding strip the white spaces and replace special characters with escape characters.
We can use java. URL Decoding is the reverse process of encoding and we can use java. Session is a conversional state between client and server and it can consists of multiple request and response between client and server. Since HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session session id is passed between server and client in every request and response.
Read more about these session management approaches in detail at Servlet Session Management Tutorial. We can use HttpSession for session management in servlets but it works with Cookies and we can disable the cookie in client browser. Cookies are text data sent by server to the client and it gets saved at the client local machine. Servlet API provides cookies support through javax. Cookie class that implements Serializable and Cloneable interfaces. HttpServletRequest getCookies method is provided to get the array of Cookies from request, since there is no point of adding Cookie to request, there are no methods to set or add cookie to request.
Similarly HttpServletResponse addCookie Cookie c method is provided to attach cookie in response header, there are no getter methods for cookie. Read more at Cookies in Servlets. If we have to make sure an object gets notified when session is destroyed, the object should implement javax.
HttpSessionBindingListener interface. This interface defines two callback methods — valueBound and valueUnbound that we can define to implement processing logic when the object is added as attribute to the session and when session is destroyed.
Recommended reading Servlet Listener. Servlet Filters are pluggable java components that we can use to intercept and process requests before they are sent to servlets and response after servlet code is finished and before container sends the response back to the client. Read more about filters at Servlet Filter. We know that servlet filters can be used to intercept request between servlet container and servlet, we can utilize it to create authentication filter and check if request contains a valid session or not.
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 deployment descriptor web. What if our application is database oriented and we want to set an attribute in ServletContext for Database Connection.
If you 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. To handle these scenario, servlet API provides Listener interfaces that we can implement and configure to listen to an event and do certain operations. Read more about different types of listeners and example at Servlet Listener.
Same goes with other error codes like , etc. Servlet API provides support for custom Exception and Error Handler servlets that we can configure in deployment descriptor, the whole purpose of these servlets are to handle the Exception or Error raised by application and send HTML response that is useful for the user.
We can provide link to application home page or some details to let user know what went wrong. Read more at Servlet Exception Handling. Servlet container use this file to configure web application servlets, servlet config params, context init params, filters, listeners, welcome pages and error handlers.
With servlet 3. Usually servlet container loads a servlet on the first client request but sometimes when the servlet is heavy and takes time to loads, we might want to load it on application startup.
We can use load-on-startup element with servlet configuration in web. If there are multiple servlets with load-on-startup value such as 0,1,2,3 then lower integer value servlet will be loaded first. We can use below code snippet to get the servlet information in a servlet through servlet context object.
File Upload and Download and common tasks in a java web application.
Please read File Upload Servlet post that provide all the necessary details with example program to upload and download file using servlets. If you work with database connection a lot in your web application, its best to initialize it in a servlet context listener and set it as a context attribute for other servlets to use.
Integrating Log4j is also very easy in web applications, all we need is a log4j configuration XML or property file and then configure it in a servlet context listener. For complete example, please look into Servlet Database and Log4j Example. We can use request. Servlet 3 introduced use of java annotations to define a servlet, filter and listener servlets and init parameters. Read more about them at Servlet 3 annotations.
Web Fragments: Prior to servlet specs 3. So servlet 3 specs introduced web fragments where we can have multiple modules in a single web application, all these modules should have web-fragment. We can include all the elements of web. This helps us in dividing our web application into separate modules that are included as JAR file in the web application lib directory. Adding Web Components dynamically: We can use ServletContext object to add servlets, filters and listeners programmatically.
This helps us in building dynamic system where we are loading a component only if we need it.
These methods are addServlet , addFilter and addListener defined in the servlet context object. Asynchronous Processing: Asynchronous support was added to delegate the request processing to another thread rather than keeping the servlet thread busy. It can increase the throughput performance of the application. This is an advance topic and I recommend to read Async Servlet tutorial.
We can configure our servlet container to use SSL for message communication over the network. To configure SSL on Tomcat, we need a digital certificate that can be created using Java keytool for development environment. For production environment, you should get the digital certificate from SSL certificate providers, for example, Verisign or Entrust. Please share your thoughts in comments and share your love with sharing on Google Plus, Facebook or Twitter.
If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Facebook or Twitter. I would love to hear your thoughts and opinions on my articles directly. Recently I started creating video tutorials too, so do check out my videos on YouTube. Hi Pankaj, I observed a typo mistake possibly. HTTP is not a language as you have mentioned. HI can you add the questions on enterprise java bean EJB tutorial and interview questions.
Please refer the link: In the answer to question no 19 you wrote that we can have constructor of a Servlet.
But as Servlet is an Interface and we cant have constructor declared for an Interface. So I think answer is incorrect. Please do correct me if I am wrong.
Servlet creation will fail if there is no zero arg constructor available in server.