Do you have an interview on JSP to attend? Are you not sure about where and how to start? This blog helps in boosting your confidence level. The JSP Interview Questions and answers given in this blog can help you get familiar with the type of questions posed in a JSP Interview.
The given JSP Interview Questions and Answers are proposed to equip you well with the questions posed during an interview on JSP. These questions have been drafted on the whole subject of JSP. The questions are segregated according to experience, i.e., Beginners and Experienced level, and they help you boost your confidence level and help you prepare well to secure a job.
We have categorized JSP Interview Questions - 2022 (Updated) into 4 levels they are:
|If you want to enrich your career and become a professional in Core Java, then enroll in "Core Java Training". This course will help you to achieve excellence in this domain.|
If you're new to this field, the below-listed questions are the fundamental JSP questions mostly posed to freshers. Preparing these JSP Interview Questions first will help you face the interview confidently.
JSP is Java Server Pages(JSP), and it is a server-side programming language used to create a dynamic web page in HTML(HyperText Markup Language). JSP is an extension of servlet technology. The JSP contains HTML code, and also there is an option to include java code for dynamic content. JSPs are mainly used in web applications.
A scriptlet is a code containing any number of JAVA language statements, variable or method declarations, or expressions that you can use in the page scripting language.
<% code fragment %>
A JSP declaration is a claim on one or more variables or methods used in Java code anytime in the JSP file. You must declare a variable or method before using it in the JSP file.
<%! declaration; [ declaration; ]+ ... %>
A JSP expression is an element containing a scripting language expression that is set, transformed to a String, and inserted wherever the expression appears in the JSP file.
According to the Java Language Specification, the expression element contains any valid expression and does not require a semicolon to end an expression.
<%= expression %>
JSP comments are the statements that the JSP container should ignore and are helpful to hide or "comment out" part of the JSP page.
<%-- This is JSP comment --%>
JSP directives are messages that inform the web container how to translate a JSP page into the corresponding Servlet.
JSP Directive Syntax:
<%@ directive attribute = "value" %>
The following types of directive tags:
JSP actions control the behaviour of the servlet engine. The JSP actions use constructs in XML syntax to dynamically insert a file, direct the user to another page, generate HTML for the Java plugin or reuse JavaBeans components.
<jsp:action_name attribute = "value" />
JSP literals are the values that can be a number or a text string and part of a program code. The following literals are used in JSP expression language:
The page directive provides instructions to the container containing the current JSP page and can be used anywhere on the JSP page.
The various attributes of page directive are.
The buffer attribute is a command specifying buffering characteristics for the server output response object.
The server output is immediately directed to the response output object.
The autoFlush attribute specifies whether the buffered output should be flushed automatically or an exception should be raised to indicate buffer overflow when the buffer is filled.
A default value of true indicates automatic buffer flushing, and false throws an exception.
The extended attribute specifies which superclass the generated Servlet must extend.
The import attribute serves the same as the Java import statement. Its value is the name of the package that is to be imported.
Info attribute helps you describe the JSP.
This attribute explains the programming language used in scripting the JSP page.
This attribute shows the usage of HTTP sessions. The TRUE value indicates that the JSP page has access to a built-in session object, and a FALSE value specifies that the JSP page cannot access the built-in session object.
The include directive is a method or syntax used to include a file in the translation phase. This directive informs the container to merge the content of other external files with the current JSP during the translation phase.
<%@ include file = "relative url" >
Both the attributes are directly related, as the scope attribute determines the lifespan of the object associated with the id.
The scope attribute has four possible values:
This action is used to insert files into the page that is created.
<jsp:include page = "relative URL" flush = "true" />
The include directive is used to insert a file when JSP page translation into a servlet. They include action inserts in the file when the page is requested.
The request object is a javax.servlet.http.HttpServletRequest instance. It also includes techniques for retrieving HTTP header information such as data, cookies, HTTP methods, and so on.
By using the getHeaderNames() method of HttpServletRequest to read the HTTP header information.
The response object is an instance of a javax.servlet.http.HttpServletRequest object that is used to construct an object that provides the response to the client. It also describes the interfaces for adding new HTTP headers.
The implicit object is a javax instance. Servlet.jsp.JspWriter object and is used to send response content.
A JSP Lifecycle consists of the following steps:
1. Compilation: When a browser asks for a JSP, the JSP engine checks if it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page. The compilation process involves three steps:
2. Initialization: When a container loads a JSP, it invokes the initialization method jspInit() before servicing any requests.
3. Execution: Whenever a browser requests a JSP and the page has been loaded and initialised, and the JSP engine invokes the _jspService() method in the JSP. The _jspService() method of a JSP is invoked once per a request and is responsible for generating the response for that request. This method is also responsible for generating responses to all seven of the HTTP methods, ie. GET, POST, DELETE etc.
4. Cleanup: The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container. The jspDestroy() method is the JSP equivalent for the Servlet destroy method.
Most of the methods in the JspWriter object are the same as those in the java.io.PrintWriter class. JspWriter also includes some additional methods for dealing with buffering. Unlike PrintWriter, JspWriter throws IOExceptions.
The session object is an instance of javax.servlet.http.HttpSession. It helps in tracking client sessions between client requests.
An application object is a direct wrapper around the ServletContext object for the created Servlet and, in reality, an instance of a javax.servlet.ServletContext object.
This object is a representation of the JSP page during its entire lifecycle. This object is created when the JSP page is initialised and is removed when the JSP page is deleted using the jspDestroy() function.
The config object is a javax. servlet instantiation. ServletConfig is a wrapper for the created Servlet's ServletConfig object.
This object provides the JSP programmer with access to Servlet or JSP engine initialization parameters such as directories, file locations, and so on.
The pageContext object is a javax.servlet.jsp instance. PageContext is an object. The object pageContext is used to represent the whole JSP page.
For each request, this object keeps a reference to the request and response objects. Accessing the attributes of this object yields the application, config, session, and out objects.
The pageContext object also holds information about the directives that were sent to the JSP page, such as buffering information, the errorPageURL, and page scope.
The page object is a direct reference to the page instance. This object is a representation of the complete JSP page.
The exception object is a wrapper for the exception thrown on the preceding page. This object aids in the generation of a suitable response to the error circumstance.
|Related Article Exception Handling in Java|
The GET method transfers the information encoded to the adjoining page request. The encoded information and the page are separated by the? Character.
The POST method transfers the information in packages just like the GET method, but the information and page are sent in separate messages. This message comes to the backend program in the form of the standard input that can be parsed and processed.
The following methods are used in JSP to handle data parsing depending on the situation:
JSP Filters are Java classes used in JSP Programming. They serve the following purposes:
Filters can be used when deploying a descriptor file web.xml and mapping to servlet or JSP names or URL patterns in the application's deployment descriptor.
The JSP container starts the web application and creates an instance for each filter declared in the deployment descriptor. These filters are then executed in the order declared in the deployment descriptor.
Cookies are text files stored on the client's computer for tracking the information.
Cookies are linked to the HTTP header. The browser is first configured to store cookies and this information until expiry. When the path of any page matches the cookie's domain, it is then reloaded to the server.
Cookies in JSP are set with the following procedure:
To read cookies, an array of javax.servlet.http.Cookie objects are created by calling the getCookies( ) method of HttpServletRequest. The array is then searched for the given cookie using getName() and getValue() methods to access each cookie and associated value.
Deleting cookies is easy, but the following procedure needs to be followed:
You can achieve Session management in JSP in the following manner:
The user's session's data can be deleted using one of the following methods:
To upload a single file one can use a single <input .../> tag with attribute type = "file".To upload multiple files in a single go, include more than one input tag with different values for the name attribute.
These files are stored in the program name, or the directory name could also be added using an external configuration such as a context-param element in web.xml.
Page redirection is used when a document is moved to a new location, and the client needs to be redirected to this new location; page redirection is used. It can also be used when load balancing or randomization.
The <jsp: forward> element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet.
sendRedirect method sets HTTP to redirect the response to the browser temporarily and creates a new request to go to the redirected page.
A hit count is a method for finding out the number of visits on a particular website page.
Application Implicit objects and associated methods getAttribute() and setAttribute() are used to implement a hit counter.
This object is a representation of the JSP page through its whole lifecycle. This object is created when the JSP page is initialized and will be removed when the JSP page is removed by the jspDestroy() method.
The auto-refresh feature in JSP is needed to refresh the webpage automatically and regularly. It is mainly used when a web page displays live game scores, stock market status, or currency exchange ratio. The auto-refresh feature in JSP makes the refreshing job easy by providing a refresh automatically after a given interval.
The refreshing of a webpage is done using the method setIntHeader() of the response object.
public void setIntHeader(String header, int headerValue)
This method sends the header "Refresh" to the browser and an integer value that represents the time interval in seconds.
The Java Server Pages Standard Tag Library (JSTL) is a collection of useful JSP tags that brief down the core functionality common to many JSP applications.
JSTL supports common structural tasks such as iteration and conditionals tags that manipulate XML documents and SQL tags. JSTL provides a framework for integrating existing custom tags with JSTL tags.
Various types of JSTL tags in use are:
The <c:set > tag is used for setProperty action. The tag helps evaluate an expression and use the results to set the value of a java.util.Map object.
The <c: remove > tag removes a variable from a specified scope.
The <c: choose> acts like a Java switch statement that lets you choose from several alternatives. Just as the switch statement has case statements, the <c:choose> tag has <c:when> tags. As the switch statement has a default clause to specify a default action, <c:choose> has <otherwise> as default clause.
The <c:redirect > tag is used to redirect the browser to an alternate URL by providing URL rewriting, it supports context-relative URLs, and <c:param> tag.
The <c:url> tag converts a URL into a string and stores it into a variable. This tag automatically performs URL rewriting when necessary.
The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Websites.
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, MySQL, or Microsoft SQL Server.
<%@ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>
The JSTL XML tags provide a JSP-related way to create and manipulate XML documents.
<%@ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>
JSP Expression Language (EL) are the statements that make it easy to access application data stored in JavaBeans components. It allows the creation of expressions both (a) arithmetically and (b) logically.
You can use the following implicit objects in JSP
Checked exceptions: Checked exception is an exception that occurs when there is a user error or a problem that the programmer might have neglected. For example, an exception occurs if a file is to be opened but cannot find the file. You can ignore these exceptions at the time of compilation.
Runtime exceptions: A runtime exception is an exception that occurs but could have been avoided by the programmer. Unlike checked exceptions, runtime exceptions are ignored at the time of compilation.
Errors are not exceptions but problems that arise beyond the user's control or the programmer's. Errors are typically ignored in the code and are unavoidable. For example, if a stack overflow occurs, an error occurs. They are also ignored at the time of compilation.
When a website is permitted to provide different versions of content translated into the visitor's language or nationality, it is called Internationalization.
Localization is adding resources to a website to adapt it to a particular geographical or cultural region, for example, Hindi translation to a website.
Locale is one particular cultural or geographical region. It is usually referred to as a language symbol followed by a country symbol and is separated by an underscore.
""en_US" represents an English locale for the US.
<%-- comment --%> is a JSP comment ignored by the JSP engine.
<!-- comment --> is an HTML comment ignored by the browser.
YES, JSP technology is extensible with the development of custom actions, or tags, encapsulated in tag libraries.
Here in this blog, almost all of the topics related to JSP are covered to help you get yourself prepared. And these JSP Interview Questions and answers can come to your aid when attending an interview. Prepare well until you feel confident. All the best!
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
|Core Java Training||Jun 28 to Jul 13|
|Core Java Training||Jul 02 to Jul 17|
|Core Java Training||Jul 05 to Jul 20|
|Core Java Training||Jul 09 to Jul 24|
Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .
Copyright © 2013 - 2022 MindMajix Technologies