JSP Interview Questions
February 18, 2022 ยท View on GitHub
Q. What is JSP Implicit Object?
- request: This is the HttpServletRequest object associated with the request.
Example: index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<form action="request.jsp">
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>
request.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String username = request.getParameter("username");
out.println("Welcome "+ username);
%>
</body>
</html>
- response: This is the HttpServletResponse object associated with the response to the client.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<%response.setContentType("text/html"); %>
</body>
</html>
- session: This is the HttpSession object associated with the request.
Example: index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% session.setAttribute("user","Pradeep"); %>
<a href="session.jsp">Click here to get user name</a>
</body>
</html>
session.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String name = (String)session.getAttribute("user");
out.println("User Name is " +name);
%>
</body>
</html>
- out: This is the PrintWriter object used to send output to the client.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% int num1=10;int num2=20;
out.println("num1 is " +num1);
out.println("num2 is "+num2);
%>
</body>
</html>
- application: This is the ServletContext object associated with the application context.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% application.getContextPath(); %>
</body>
</html>
- config: This is the ServletConfig object associated with the page.
Example: web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>comingsoon</servlet-name>
<servlet-class>mysite.server.ComingSoonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>comingsoon</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String servletName = config.getServletName();
out.println("Servlet Name is " +servletName);%>
</body>
</html>
-
pageContext: It is used to get, set and remove the attributes from a particular scope.
-
page: Page implicit variable holds the currently executed servlet object for the corresponding jsp. Acts as this object for current jsp page.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% String pageName = page.toString();
out.println("Page Name is " +pageName);
%>
</body>
</html>
- Exception: It is used for exception handling in JSP.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implicit Objects</title>
</head>
<body>
<% int[] num1={1,2,3,4};
out.println(num1[5]);
%>
<%= exception %>
</body>
</html>
Q. How JSP pages are processed, from the request to the server to the response to the user?
When the user page.jsp follows the link to the page , he sends an http request to the server GET /page.jsp. Then, based on this request and the text of the page itself, the server generates a java class, compiles it and executes the resulting servlet, which forms a response to the user in the form of a representation of this page, which the server redirects back to the user.
Q. What are the phases of the JSP life cycle?
The JSP life cycle consists of several phases that are managed by the JSP container:
- Translation - checking and parsing the JSP page code to create the servlet code.
- Compilation - compilation of the servlet source code.
- Class Loading - loading a compiled class into memory.
- Instantiation - the implementation of the constructor without the parameter of the loaded class for initialization in memory.
- Initialization - calling the
init()method of the JSP class object and initializing the servlet configuration with the initial parameters that are specified in the deployment descriptor (web.xml). After this phase, the JSP is able to handle client requests. Typically, these phases occur after the first client request (i.e., lazy loading), but you can configure the loading and initialization of JSP at the start of the application, similar to servlets. - Request Processing - the long life cycle of processing client requests with a JSP page. Processing is multithreaded and similar to servlets - for each request a new thread, objects are created,
ServletRequestand theServletResponseservicemethods are executed. - Destroy is the last phase of the JSP life cycle in which its class is removed from memory. This usually happens when you turn off the server or unload the application.
Q. What are the JSP life cycle methods?
A servlet container (for example, Tomcat, GlassFish) creates a servlet class from a JSP page that inherits interface properties javax.servlet.jsp.HttpJspBase and includes the following methods:
- jspInit()- the method is declared in the JSP page and is implemented using the container. This method is called once in the JSP life cycle in order to initialize the configuration parameters specified in the deployment descriptor. You can override this method by defining a JSP scripting element and specifying the necessary parameters for initialization;
- _jspService()- the method is automatically overridden by the container and corresponds directly to the JSP code described on the page. This method is defined in the interface
HttpJspPage, its name begins with an underscore, and it differs from other life cycle methods in that it cannot be redefined; - jspDestroy()- the method is called by the container to remove the object from memory (at the last phase of the JSP life cycle is Destroy). The method is called only once and is available for redefinition, providing the ability to free resources that were created in
jspInit().
Q. How can I prevent direct access to the JSP page from a browser?
There is no direct access to the directory /WEB-INF/ from the web application. Therefore, JSP pages can be located inside this folder and thereby restrict access to the page from the browser. However, by analogy with the description of servlets, it will be necessary to configure the deployment descriptor:
<servlet>
<servlet-name> Example </servlet-name>
<jsp-file> /WEB-INF/example.jsp </jsp-file>
<init-param>
<param-name> exampleParameter </param-name>
<param-value> parameterValue </param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name> Example </servlet-name>
<url-pattern> /example.jsp </url-pattern>
</servlet-mapping>
Q. What are the main types of JSP tags?
- JSP expression:
<%= expression %>- expression that will be processed with redirecting the result to the output; - JSP Scriptlet:
<% code %>- The code to add to the method service(). - JSP declaration:
<%! code %>- code added to the servlet class body outside the method service(). - JSP page
<%@ page att="value" %>directive: - directives for the servlet container with parameter information. - JSP directive include:
<%@ include file="url" %>- a file on the local system that is included when translating the JSP into the servlet. - JSP Comment:
<%-- comment --%>- Comment; ignored when translating a JSP page to a servlet.
Q. What are the JSP action tags and JSP Action Elements?
Action tag and JSP Action Elements provide methods for working with Java Beans, connecting resources, forwarding queries and creating dynamic XML elements. Such elements always begin with recording jsp:and are used directly inside the JSP page without the need for third-party libraries or additional settings.
The most commonly used JSP Action Elements are:
- jsp:useBean
- jsp:include
- jsp:setProperty
- jsp:getProperty
- jsp:forward
- jsp:plugin
- jsp:attribute
- jsp:body
- jsp:text
- jsp:param
- jsp:attribute
- jsp:output
1. jsp:useBean
This action name is used when we want to use beans in the JSP page. With this tag, we can easily invoke a bean.
<jsp:useBean id="" class="" />
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Action JSP1</title>
</head>
<body>
<jsp:useBean id="name" class="demotest.DemoClass">
</body>
</html>
2. jsp:include
It also used to insert a jsp file into another file, just like include directive. It is added during request processing phase
<jsp:include page="page URL" flush="true/false">
3. jsp:setProperty
This property is used to set the property of the bean. We need to define a bean before setting the property
<jsp:setproperty name="" property="">
4. jsp:getProperty
This property is used to get the property of the bean. It converts into a string and finally inserts into the output.
<jsp:getAttribute name="" property="">
5. jsp:forward
It is used to forward the request to another jsp or any static page. Here the request can be forwarded with no parameters or with parameters.
<jsp:forward page="value">
6. jsp:plugin
It is used to introduce Java components into jsp, i.e., the java components can be either an applet or bean. It detects the browser and adds