Wednesday, October 17, 2012
Top 10 Servlet JSP Interview Questions for Java J2EE programmer - FAQ
Servlets and JSP Interview Questions are core of any J2EE Interview. Its expected from candidate to have good knowledge of Servlet, JSP, EJB , Struts or Spring in any J2EE interview. Consequently Servlets and JSP Interview Questions are very common in any J2EE Interviews along with some questions from core Java, Some interview Questions from Spring and Some EJB interview questions. Since J2EE is main platform for building enterprise and web application using Java programming language, its important to prepare well for J2EE interview, especially If you are in application development space. Servlet and JSP forms web layer of J2EE framework where Java Server pages is main view technology and Servlets are main Controller component of standard MVC architecture. In this article we will see couple of most common or frequently asked JSP and Servlet Interview questions and answers, which can help you on your J2EE interview preparation and understanding some key concept of Java web applications.
JSP Servlet Interview Questions Answers
Here is my list of frequently asked JSP Servlet Interview questions to any Java programmer. Most of these Servlet JSP questions has appeared in various level of J2EE interviews including 2 to 3 and 2 to 4 years experience level. None of these questions require special question and neither of them are tricky questions, they all are focused on fundamentals which is key to clear any interview.
Question 1: What is JSESSIONID in Java? When does JSESSIONID gets created ?
One of my favourite Servlet JSP Interview question for 2 to 4 years experience programmers on web development. JSESSION id is a cookie which is used to manage session in Java web application. JSESSIONID is created by Web Container whenever a new session is created. See What is JSESSIONID in Servlet JSP for more details.
Question 2: What is difference between include action and include directive in JSP?
Another very popular JSP Interview questions, mostly asked to 2 to 3 years experienced J2EE programmer. There are couple of differences, most important of them is that include action is request time inclusion while include directive is translation time inclusion of another resource e.g. JSP or html pages. This questions is also asked as difference between file include and page include. See Include action vs Include directive for more differences.
Question 3: How do you define application wide error page in JSP?
Almost in every Servlet JSP interview you will see question from error handling. You can define two kinds of error pages in Java web application, one is using tag <error-page> in web.xml and other is by using error page JSP which uses isErrorpage to declare that this jsp page can be used as error page. Other JSP uses that page by using attribute errorpage="error.jsp". Whenever you get an unhandled exception in JSP, request will be routed to error page. See How to use error page in JSP for detailed answer of this question.
Question 4: Difference between sendredirect and forward in Servlet ?
One of the classical Interview Question from Servlet and JSP. This question is as old as Vector vs ArrayList in core Java. Anyway see difference between sendredirect and forward to answer this Servlet Interview question.
Question 5: How do remove variable using <c:set> tag from JSTL ?
This is one of the tricky Servlet JSP question. Many people assumes that <c:set> can only add or set variables in a particular scope but you can also remove any variable from any scope using JSTL <c:set> tag. See How to use <c:set> JSTL tag in JSP for exact way to remove any variable from any scope in JSP page.
Question 6: What is difference between Web Server and Application Server ?
This is rather simple Servlet JSP question to answer. If you have used EJB then you should know that , Web Server doesn't contain EJB container and EJB can not be deployed on that. Application Server is used to deploy and run EJB in J2EE environment. See 5 difference between Application and Web Server to see more differences.
Question 7: What is difference between URL Encoding and URL rewriting ?
URL Encoding and URL rewriting is general web concept irrespective of Java programming language. URL Encoding refers to encoding URL e.g. replacing space with %20, you can see this when browser sends request to Server. On the other hand URL Rewriting is way to manage Session in web application. See URL Encoding vs URL Rewriting for more differences.
Question 8: How do you get ServletContext reference inside Servlet ?
ServletContext is very important object in Java Web application. ServletContext object is one per web application and serves as application scope and use to store common config and things which doesn't change on per session. Its easy to get reference of ServletContext in jsp using application implicit variable but in Servlet is not available in HttpServletRequest until version 3.0. You need HttpSession object to retrieve ServletContext in any Servlet. A good follow-up questions is How do you get ServletContext in Struts Action class or Spring Controller classes. See How to retrieve ServletContext in Servlet, Spring and Struts for more details.
Question 9: What is difference between ServletContext and ServletConfig in Java ?
One of those classical Servlet Interview Questions which you can't afford to miss. Again ServletContext is used to provide application wide configure while ServletConfig is used to configure and provide initialization parameter to one Servlet. See ServletContext vs ServletConfig for more differences.
Question 10: Which open source tag library have you used ?
This is an interesting Servlet JSP questions and gives an opportunity to show how many tag library you are familiar with and which ones have you used. Most J2EE programmer answer this question with saying JSTL core tag library, Struts tag library , Spring tag library or display tag, which is quite popular tag library to display tabular data and provides lot of feature out of box e.g. paging, sorting and export functionality.
Question 11: What is difference between GET and POST method in HTTP protocol?
Another classical web interview question, not specific to Servlet or JSP but very important in context of web development which is based on HTTP protocol. There several differences between GET and POST method including length of data required to Send to Server. GET is less secure and can only send limited data hence not useful to transfer sensitive information. See GET vs POST HTTP method for more differences.
Question 12: What does load-on-start-up element in web.xml do?
One of the tough Servlet JSP Interview question especially to 2 years experience guy, who may not be exposed to all tags of web.xml. load-on-startup is related to loading of Servlet. See What is load-on-startup tag in web.xml for more details.
These were some Servlet and JSP Interview questions and answer you can look for quick revision purpose. If you have faced any interesting Servlet JSP question or if you are looking answer for any Servlet JSP question than please share.
Other Java Interview Questions articles