JSP(Java servelate pages) Interview questions and answers

What is JSP and why is it needed?

JSP is JavaServer Pages. JSP is a server technology for creating dynamic web pages. JSP extends servlet technology to help developers create dynamic pages with HTML-like syntax.

Creation of representations is supported, and in servlets, but in that case, the code looks awful and is subject to errors. It was also noticed that most of the elements of the web page are static and therefore the JSP page is more suited to web pages. If possible, you should avoid the business logic on the JSP page and use the page only as a view. Therefore, it is recommended to use JSP actions or JSTL tags instead of writing JSP scripts.

Another advantage of JSP is the hot deployment. We can replace the old page with another one in the container and users will see a new JSP page. Thus, there is no need to compile the entire project or restart the server to update part of the page.

Tell us about the phases (phases) of the life cycle of JSP.

If you look at the code inside the generated JSP page, it will look like HTML and will not look like a java class. Converting JSP pages into HTML code is handled by a container that also creates a servlet for use in a web application. The life cycle of a JSP consists of several phases:

Translation – The JSP container checks the JSP code of the page, parse it to create the servlet code. For example, in Tomcat, you can find the servlet classes in the TOMCAT / work / Catalina / localhost / web app / org / apache / JSP directory. If the JSP page is called home.jsp, then the created servlet will usually have the name home_jsp, and the file name is home_jsp.java.

Compilation – The JSP container compiles the source code of the JSP class and creates a class in this phase.

Class Loading – the container loads classes into memory at this phase.

Instantiation – the introduction of constructors without the parameters of the created classes to initialize in the memory classes.

Initialization – the init method of the JSP class object is invoked in the container, and the servlet configuration is initialized with the init parameters specified in the deployment descriptor (web.xml). After this phase, the JSP can handle client requests. Typically, these phases occur after the first client request (that is, lazy loading), but you can configure the loading and initialization of the JSP at application startup, similar to servlets.

Request Processing – a long life cycle for processing JSP client requests by a page. Processing is multithreaded and similar to servlets – for each request, a new thread is created, ServletRequest and ServletResponse objects are created, and the JSP methods service is implemented.

Destroy is the last phase of the JSP’s life cycle in which the JSP class is removed from memory. Usually, this happens when the server is turned off, or an application is running.

Tell us about the methods of the life cycle of JSP.

Life Cycles of JSP Methods:

jspInit () – The method is declared in the JSP page and implemented using container implementations. This method is called once in the JSP lifecycle to initialize the configuration parameters specified in the deployment descriptor. This method can be overridden by defining the JSP scripting element and specifying the necessary parameters for initialization.

_jspService () – This JSP method is injected by the JSP container for each client request by sending a request and response object. Note that the method name begins with a lower underscore and differs from other life-cycle methods in that it cannot be overridden. All JSP code goes through this method, and it is overridden by default. This method is defined in the HttpJspPage interface.

jspDestroy () – the method is called by the JSP container to remove the object from memory (in the last phase of the JSP’s life cycle – Destroy). The method is called only once, and we can override it to clean up any resources that were created in the JSP init method.

What can JSP lifecycle methods be redefined?

We can override jspInit () and jspDestroy () methods using JSP scripts. The jspInit () method is overridden to create the shared resources that we would like to use in the JSP service method, and the jspDestroy () method is overridden to release shared resources at the kill phase.

Why are implicit objects not available in a regular JSP page?

The implicit JSP exception object is not available in regular JSP pages and is used on JSP error pages only to intercept the exception thrown by the JSP page and then provide any useful information to the client.

What do you know about PageContext and what are the benefits of using it?

The implicit JSP object of the pageContext is an instance of the implementation of the abstract class javax .servlet.jsp.PageContext . We can use the pageContext object to retrieve and set attributes with a different scope and to throw requests to another resource. This object also has a reference to another implicit object. This is the only object that is represented in JSP implicit objects and JSP EL implicit objects.

How to configure init parameters for JSP?

We can set initialization parameters for JSPs in the same way as servlets in the web.xml file. We need to configure the init JSP parameters with the servlet and servlet-mapping elements. The only difference is the location of the JSP page.

Why is it not recommended to use script elements in JSP?

JSP pages are mainly used for display purposes, and all business logic and models must be implemented in servlets or class models. We need to pass parameters to the JSP page through attributes and then use them to create an HTML response on the JSP page. Most of the JSP contains HTML code, and to help designers understand the JSP code of the page and develop them, they provide action elements, JSP EL, JSP Standart Tag Library. These elements should be used instead of scriptlets to create a bridge between JSP HTML and JSP java parts.

What is the difference between the include and JSP: include action?

The difference between the JSP includes and JSP: include action directives is that for the include directive, the content for another resource will be added to the created servlet at the translation stage of the JSP (the Translation phase), while the JSP: include action runs in runtime.

Another difference between JSP includes action is that we can pass parameters for an attachment using the JSP: params command, while the JSP include directive cannot pass parameters.

Use the JSP include directive for static resources such as header, footer, image file to improve performance. If we need dynamics, passing parameters for processing, then we need to use the JSP: include action tag.

What do you know about the language of expressions JSP (JSP Expression Language – EL)?

In most cases, we use JSP for viewing purposes, and all business logic is present in the servlet or model classes. After receiving the client request, it is processed in the servlet, and then add the attributes in the request/ session/context scope, which must be extracted in the JSP code. To create a response of views, you also use request, headers, cookies, and init parameters.

We can use the scriptlets in JSP expressions to get the attributes and parameters in the JSP with the Java code and use it for the views. The problem is that web designers usually do not know java code, which is why JSP 2.0 introduced an expression language (EL), through which we can get attributes and parameters using HTML tags.

The syntax of expression language looks like $ {name}, and we can use EL implicit objects and expression language operators to extract attributes from different scopes and use them in the JSP page.

Which categories can you separate the JSTL tags from, give examples?

JSTL tags are divided into five categories according to their functionality:

Core Tags – Core tags provide iterations, exception handling, URL, forward and redirect response, etc.

Formatting and Localization Tags – provide opportunities for Formatting Numbers, Dates and support for i18n localization and resource bundles.

SQL Tags – JSTL SQL Tags support for working with databases like MySQL, Oracle, etc.

XML Tags – used to work with XML documents. For example, for parsing XML, converting XML data, and executing XPath expressions.

JSTL Functions Tags – provides a set of functions that allow you to perform various operations with strings, etc. For example, by concatenating or splitting strings.

What do you know about writing custom JSP tags?

JSP allows you to create your own tags with the necessary functionality. We can add the tag library to the JSP page using the namespace specification. To create your tag, we can use the following components:

  • JSP Custom Tag Handler
  • Creating a Tag Library Descriptor (TLD) file
  • Deployment Descriptor Configuration for TLDs
Close

About The Author

Ruchi Singla
An enthusiast writer with years of experience, who loves to create beautiful thoughts into words. She writes with a zeal and enjoys reading vogue with a good cup of coffee when not writing. Ruchi is working as a content writer for many businesses, helping them gain an extra edge over their competitors. As a content writer, she is responsible for enhancing engagement and traffic on the blog by brainstorming exciting content ideas.

This site uses Akismet to reduce spam. Learn how your comment data is processed.