Introduction To JSP Interview Questions And Answers
JSP or java server pages is a technology which is used to dynamically generate web pages based on web technologies such as HTML, CSS, XML etc. JSP was created by Sun Microsystem in 1999. JSP is similar to ASP or PHP, the only difference is that it uses Java programming language. Apache Tomcat or Jetty or any other compatible web server with a servlet container are used to do deployment and running the Java-Server Pages.JSP can also be regarded as a high-level abstraction of servlets concepts of Java.
JSPs code is converted into servlets at runtime and hence JSP is regarded as a Servlet. Caching is done on JSP servlet and it is re-used till the time original JSP code is changed.JSP architecture is either independent or as an MVC component. Usually, Java Beans is used as the model and Java servlets as the controller. With new frameworks such as Spring or JSF, JSP is not probably used that much these days, but it is still quite important for upcoming developers and students.
Now, if you are looking for a job that is related to JSP then you need to prepare for the 2020 JSP Interview Questions. It is true that every interview is different as per the different job profiles. Here, we have prepared the important JSP Interview Questions and Answers which will help you get success in your interview.
Hence, In this 2020 JSP Interview Questions article, we provide the top 10 important JSP Interview Questions which are widely asked in Interviews.
Part 1 – JSP Interview Questions (Basic)
This first part covers basic Interview Questions and Answers
1. Explain What do you understand by the JSP lifecycle phases?
Answer:
JSP belongs to Java group, but JSP code looks more like HTML than Java. It is a JSP container that translates JSP pages and creates a servlet class that is then used in web applications.
JSP lifecycle phases are described below:
- Translation – JSP page code is checked by JSP container and parsed by it to generate the servlet source code.
- Compilation – Next, the JSP container compiles the JSP class source code. After that, it produces a class file.
- ClassLoading – In this phase, the container loads the class in memory.
- Instantiation – First, a no-args constructor of a generated class is invoked by a container and loaded into memory, after that instantiation is done.
- Initialization – The Container, in this phase invokes the init method of the JSP class object. After that servlet configuration with init parameter is configured. Now, JSP can handle client requests. Most often translation and initialization in JSP happen as soon as a first request for JSP comes.
- Request Processing – It is the most time-consuming phase of the JSP page. Here JSP page processes the requests from a client. The request processing phase is multi-threaded. For every request, a new thread is used and Servlet-request and Servlet-response object is created. After this, the JSP service method is called.
- Destroy – JSP lifecycle’s last phase is destroyed. In this phase, the JSP class is unloaded from memory. This is done when the application is undeployed. It can also be done when a server is down.
2. Elaborate on JSP lifecycle methods?
Answer:
JSP lifecycle methods are described below:
- jspInit(): Declared in JSP Page implemented by JSP, this method is called only once in the JSP lifecycle. This is used to initialize config params in a deployment descriptor. This method can also be overridden using a JSP declaration scripting element. Override is done to initialize those resources that can be used in JSP page.
- _jspService(): Invoked by JSP container for each client request, this method passes request and response object. This method cannot be overridden and hence it starts with an underscore. When JSP code goes within this method, it gets overridden by default. Overriding it using JSP declaration scripting element is not suggested. As a standard practice, this method is defined and declared in the HttpJspPage interface.
- jspDestroy(): Used for shutting down application or container, this method is called when JSP is unloaded from memory. This method is also called only once in the JSP lifecycle. Overriding of this method should be done only to release resources that were created in JSP init method.
3. Mention some JSP lifecycle methods that can be overridden?
Answer:
As described above, jspInit() and jspDestroy() methods can be overridden in JSP using JSP declaration scripting element. jspInit() methods are overridden to create common resources that can be used in JSP service method and jspDestroy() method should be overridden to release the common resources which were created using jspInit().
4.5 (5,402 ratings)
View Course
4. Explain What do you understand by JSP declarations?
Answer:
This is the basic JSP Interview Questions asked in an interview. A declaration is used to declare one or several variables and also methods that are used in Java code in the JSP file. Variable or method declaration is a must before it can be used in the JSP file.
5. Explain JSP expressions?
Answer:
JSP expression is nothing but an element that contains a scripting language expression and is used to evaluate String conversion and insertion at the place where expression appears in the JSP file.
The expression element defined above can contain any expression in general. The only requirement is that it should be valid according to the Java Language Specification. A semicolon cannot be used to end a JSP expression.
Part 2 –JSP Interview Questions (Advanced)
Let us now have a look at the advanced Interview Questions.
6. Describe how JSP is used in the MVC model?
Answer:
JSP can be used for presentations in the MVC pattern. It plays the role of the view in MVC architecture. Calling the model and the business classes are done by Controller. Data is obtained by the model and this data is then sent to the JSP to render on a client.
7. Mention What consists of JSP literals?
Answer:
This is the frequently asked JSP Interview Questions in an interview. JSP literal contains the following types of data formats: –
- Boolean
- Integer
- String
- Floating point
- Null
8.Mention the different scope values for the given tag. <jsp:useBean>
Answer:
There are 4 values in the above-given tag:
- page
- request
- a session, and application
9.Explain How can we configure init params in JSP?
Answer:
init params can be configured in JSP similar to as it is done in a servlet. The configuration is done via init-params with servlet and servlet-mapping element, the only difference being, in servlet we do not need to provide the JSP page location while in a jsp-file element we need that.
10. Explain what do you understand by JSP Action Elements or JSP Action Tags?
Answer:
JSP action elements or action tags are nothing but tags that are similar to HTML tags. They provide many useful functionalities like working with Java Bean, request forwarding, generating dynamic XML elements and including a resource etc. The convention is that the JSP action elements start with jsp: It can be used in JSP page directly and there is no need to import any tag libraries for it. No need to import any other configuration changes, too. Some of the most important action elements in JSP are jsp:useBean, jsp:setProperty, jsp:getProperty, jsp:forward and jsp:include.
Recommended Article
This has been a guide to List Of JSP Interview Questions and Answers so that the candidate can crackdown these Interview Questions easily. Here in this post, we have studied about top JSP Interview Questions which are often asked in interviews. You may also look at the following articles to learn more –