Register Login

JSP Interview Questions and Answer for Experienced

Updated Jul 16, 2019

What is JSP and what does JSP stand for?

JSP stands for Java Server Pages, it helps the developer to create dynamic and data-driven web pages for XML and HTML. It helps them to control the appearance of the website through small programs called servlets. These programs are executed on the Web server that allows the developer to modify the page before sending it to the user.

What are some advantages of JSP?

The advantages of JSP are as follows:

  • Using JSP different static templates including HTML and XML fragments can be combined to create dynamic web pages
  • If a JSP program is modified, the underlying server can recognize it automatically and web page application does not need to be reloaded. This improves the web page’s performance and speed
  • It allows developers to separate business logic (Java code) from presentation logic (HTML code)
  • Provides built-in tags that enhance the code readability

How to run the JSP file?

In a Windows environment, JSP programs are run usually in the Apache Tomcat web server. The steps to execute a JSP program in the server are:

  • Write a JSP program and save it with an extension of .jsp. If the program is requested by the web browser, the JSP code will be converted into a servlet program. The output will be in the form of an HTML script that will be dispatched to the browser
  • Copy the JSP file to the Tomcat root folder, for example, c:/Tomcat/webapps/ROOT
  • Initialize the Tomcat server
  • Start the web browser
  • In the address bar, type http://localhost:8080/file.jsp and Hit enter
  • The output will be displayed on the web page

How to comment in JSP?

In a JSP program, the comments can be placed inside the <%-----%> tags.

How to call a servlet from JSP?

To call a servlet from a JSP page, you can use the following code:

<jsp:include page="/servletURL" />

When you call the servlet, it further calls the JSP program to show the results. The servlet can also be called by the URL that matches the <url-pattern> in the web.xml.

How to use session in JSP?

A session in  JSP is an implicit object that is of HttpSession. The object can be used for obtaining the session information, to remove or set an attribute. It is mainly used for obtaining all the user information. It can be used in a form for storing the user details like:

<form action= session.jsp>

What is div in JSP?

A <div> tag is used to describe a specific section on the web page. The tag can also be used as a container to group two or more elements and style them. For JSP, the tag can be used to display specific data or text on the web page.

It works as a container that encapsulates page elements.

How to pass a session object to the JSP page?

In a JSP page, a session object can be stored and fetched using two methods:

  • setAttribute (String, object) – Using this method, the session object is saved by assigning a string to it. As long as the session is active, the object can be accessed using the String
  • getAttribute (String name) – The object stored by the setAttribute can be obtained by this method

What is the scope of implicit objects in JSP?

The scope of an implicit object is defined by its scope. The JSP implicit object scope is divided into four categories:

  • Page – This means the object can be accessed within the page where it was created. This is the default scope
  • Request – This scope defines the availability of the session object by any page that serves the page request
  • Session – Here, the object is accessed by pages belonging to the session where the object was initially created
  • Application – The objects having application scope can be accessed from any page within the application

What is the difference between static include and dynamic include in JSP?

Static include in JSP is defined by the tag <%@include>. Here the contents of the file will be included in the compilation phase. They are faster than dynamic includes.

Dynamic include is used to include the output of the pages. When the jsp: include tag is used, the file is processed and incorporated within the final response during execution time.

What is a scriptlet tag in JSP?

The scriptlet tags are used to write Java code in the JSP page. When the servlet is generated from JSP, the statements are moved to the in _jspservice() method. For every service request, the service method is invoked.

The <% %> is scriptlet tags where the Java code can be written that is executed every time the JSP is called.

How to use form variables to a scriptlet in JSP?

The scriptlet can be used for obtaining the user information in a JSP page and displaying that information in another page. For example, the first page is called index.html, where the code for accepting user input is:

<form action="welcome.jsp">  

<input type="text" name="username">  

<input type="submit" value="go">

Then the scriptlet in the JSP page called hello.jsp, the following code displays the user’s name:

<%  

String name=request.getParameter("username");  

out.print("Hello "+name);  

%>

How to redirect to another page in JSP?  

Page redirection is used when a specific document is moved to a new location and the user has to be sent to this location. The sendDirect() is used to redirect a request to a new page. The method is used on the response object. The syntax is:

public void response.sendRedirect(String location)

This method sends the response back to the browser with the new location and the status code. apart from this method, the setStatus() and the setHeader() methods can also be used for redirection.

What is div in JSP?

A <div> tag is used to describe a specific section on the web page. The tag can also be used as a container to group two or more elements and style them. For JSP, the tag can be used to display specific data or text on the web page.

It works as a container that encapsulates page elements.

What is JSP client arrests?

Mention some important action tags used in JSP?

Some important action tags used in JSP are as follows:

  • forward – The tag is used for forwarding the response and request to the resource.
  • useBean – The tag is used to locate or create a bean object
  • include – Used for including resources in a web page
  • setProperty – Used for setting the property value in a bean object
  • getProperty – Used for printing the bean object’s property value

Explain Expression language in JSP?

The JSP expression language makes it easier for developers to access data within Java Bean components. Using this language both logical and arithmetic expressions can be created. Integers, strings, floating-point numbers, Boolean values, and null values can be used with JSP expression language.

How to set and delete cookies in JSP?

The steps to set a cookie in JSP are:

  • Call the Cookie constructor by passing the name and value to create a cookie object
  • The name and the value must not have white spaces or any of these characters -  [ ] ( ) = , " / ? @ : ;
  • Use setMaxAge() to mention out the validity of the cookie
  • Use the response.addCookie to add it to the HTTP response header

Steps to delete cookies are:

  • Read the cookie and store it in the cookie object
  • Use setMaxAge() to set the value to zero and delete it
  • Add it again to the HTTP header

What is a custom tag in JSP?

A custom tag is a user-defined tag. When it is translated into a servlet, it gets converted into a tag handler that is used for performing operations on an object.

How to create a custom tag in JSP?

Steps to create a custom tag are:

  • The Tag Handler Class is used to specify the tag operations in the JSP page
  • The Tag Library Descriptor file is used for mentioning the tag name, attributes and handler class
  • Then the JSP file is created that will use the custom tag


×