JSP Interview Questions

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 - 2023 (Updated) into 4 levels they are:

Top 10 Frequently Asked JSP Interview Questions and Answers

  1. What is JSP?
  2. When are JSP declarations made?
  3. What are JSP literals?
  4. What is an include directive?
  5. What is the difference between JspWriter and PrintWriter?
  6. What is a pageContext Object?
  7. How do you set cookies in the JSP?
  8. How is Session Management done in JSP?
  9. What is JSTL?
  10. What are the implicit EL objects that are used in JSP?
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.

JSP Interview Questions For Freshers

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.

1. What is JSP?

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.

Related Article: Thymeleaf vs JSP 

2. What do you mean by a scriptlet in JSP? And what is its syntax?

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.

Syntax:  

<% code fragment %>

3. When are JSP declarations made?

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.

Syntax:

<%! declaration; [ declaration; ]+ ... %>

4. What is the use of JSP expressions?

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.

Syntax:

<%= expression %>

5. What are JSP comments?

JSP comments are the statements that the JSP container should ignore and are helpful to hide or "comment out" part of the JSP page.

Syntax:

<%-- This is JSP comment --%>

6. What are JSP Directives?

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" %> 

7. What are the types of JSP directives?

The following types of directive tags:

  1. Page Directive: This tag defines the page-dependent attributes like a scripting language, error page, and buffering requirements.
  2. Include Directive: This tag is used to include a file during the translation phase.
  3. Taglib Directive: This tag is used when declaring a tag library containing custom actions used on the page.

MindMajix Youtube Channel

8. When are JSP actions required?

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. 

Syntax:

<jsp:action_name attribute = "value" />

9. What are the most used JSP actions?

  • jsp:include
  • jsp:element
  • jsp:text
  • jsp:getProperty 
  • jsp:forward
  • jsp:plugin
  • jsp:useBean 
  • jsp:attribute 
  • jsp:body
  • jsp:setProperty

10. What are JSP literals?

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:

  • Boolean − true/false
  • Integer − Same as Java
  • Floating point − Same as Java
  • String − Can be with single and double quotes; ""is escaped as \"", ''is escaped as \'', and \ is escaped as \\.
  • Null − null

11. Where are page directives used?

The page directive provides instructions to the container containing the current JSP page and can be used anywhere on the JSP page.

12. Mention various attributes used in page directive?

The various attributes of page directive are.

  • language
  • extends
  • import
  • session
  • isThreadSafe
  • info
  • errorPage
  • isErrorpage
  • contentType
  • isELIgnored
  • buffer
  • autoFlush
  • isScriptingEnabled

13. What is a buffer attribute?

The buffer attribute is a command specifying buffering characteristics for the server output response object.

14. What happens when the buffer is set to "none"?

The server output is immediately directed to the response output object.

15. What does the autoFlush attribute do?

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.

16. What is an extended attribute?

The extended attribute specifies which superclass the generated Servlet must extend.

17. What is the import attribute?

The import attribute serves the same as the Java import statement. Its value is the name of the package that is to be imported.

18. What is the info attribute?

Info attribute helps you describe the JSP.

19. What is a language attribute?

This attribute explains the programming language used in scripting the JSP page.

20. What is a session attribute?

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.

Advanced Level JSP Interview Questions

21. What is an include directive?

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. 

Syntax:

<%@ include file = "relative url" >

22. What does the id and scope attribute mean in the action elements?

  • Id attribute − This identifies the Action element and sees the action to be referenced is taken inside the JSP page. 
  • Scope attribute − This attribute is used in identifying the Action element's lifecycle.

Both the attributes are directly related, as the scope attribute determines the lifespan of the object associated with the id. 

23. What are the values of a scope attribute?

The scope attribute has four possible values: 

  • application
  • request 
  • session 
  • page

24. Explain the usage of <jsp:include> action?

This action is used to insert files into the page that is created. 

Syntax:

<jsp:include page = "relative URL" flush = "true" />

25. What is the difference between include action and include directive?

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.

26. Which JSP supports implicit objects?

  • exception
  • request
  • response
  • session
  • application
  • config
  • pageContext
  • page

27. What exactly is a request object?

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.

28. How can you read a request header information?

By using the getHeaderNames() method of HttpServletRequest to read the HTTP header information. 

29. What is a response object?

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.

30. What is the implicit object?

The implicit object is a javax instance. Servlet.jsp.JspWriter object and is used to send response content.

31. Explain the life cycle of a JSP

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:

  • Parsing the JSP.
  • Turning the JSP into a servlet.
  • Compiling the servlet.

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.

32. What is the difference between JspWriter and PrintWriter?

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.

33. What is the session object?

The session object is an instance of javax.servlet.http.HttpSession. It helps in tracking client sessions between client requests.

34. What is an application object?

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.

35. What is a config object?

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. 

36. What is a pageContext object?

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.

37. What is a page object?

The page object is a direct reference to the page instance. This object is a representation of the complete JSP page.

38. What is an exception object?

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

39. Explain the difference between the GET and POST methods in HTTP protocol.

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.

40. What are the various methods used to read data using JSP?

The following methods are used in JSP to handle data parsing depending on the situation:

  1. getParameter(): This method is used to get the value from a parameter.
  2. getParameterValues(): This method is used when the parameter appears more than once and returns multiple values, for example, checkbox.
  3. getParameterNames(): This method can be used when a need is to call a complete list of all parameters in the current request.
  4. getInputStream(): This method can be used when reading a binary data stream received from the client.

JSP Interview Questions For Experienced

41. What are filters?

JSP Filters are Java classes used in JSP Programming. They serve the following purposes:

  • Intercept requests from a client before accessing a resource at the back end.
  • To change responses from the server before sending them back to the client.

42. Where are the filters used in JSP?

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.

43. What are cookies?

Cookies are text files stored on the client's computer for tracking the information.

44. Describe the working procedure of cookies.

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.

45. How do you set cookies in the JSP?

Cookies in JSP are set with the following procedure:

  • Creating an object: Construct a cookie with a name and a value. Both must be strings.
  • Setting the maximum age: Set setMaxAge to specify how long the cookie should be valid. The value is generally in seconds.
  • Sending the cookie into the HTTP response headers: Cookies are added to the HTTP response header using response.addCookie.

46. How are cookies read in JSP?

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.

47. How to delete cookies with JSP?

Deleting cookies is easy, but the following procedure needs to be followed:

  • Read an existing cookie and store it in a Cookie object.
  • Set cookie age to zero using the setMaxAge() method to delete an existing cookie.
  • Add this cookie again to the response header.

48. How is Session Management done in JSP?

You can achieve Session management in JSP in the following manner:

  • Cookies − A web server can assign a unique session ID as a cookie to each web client, and for subsequent requests from the client, you can recognize them using the received cookie.
  • Hidden Form Fields − A web server can send a hidden HTML form field along with a unique session ID as follows −
    <input type = "hidden" name = "sessionid" value = "12345">
  • URL Rewriting − In URL rewriting, extra information is added at the end of each URL that identifies the session. This URL rewriting can be useful when a cookie is disabled.
  • The session Object − JSP uses Servlet provided HttpSession Interface, which provides a way to identify a user across more than one-page request or visit a Web site and store information about that user.

49. How is a session's data deleted?

The user's session's data can be deleted using one of the following methods:

  • Remove a particular attribute: Call the publicvoidremoveAttribute(String name) method to delete the value associated with that particular key.
  • Delete the whole session: Call the publicvoidinvalidate() method to discard an entire session.
  • Setting Session timeout: Call publicvoidsetMaxInactiveInterval(int interval) method to set the timeout for a session individually.
  • Log the user out: The servers can log the client out of the Web server and invalidate all sessions belonging to all the users.
  • web.xml Configuration: When using Tomcat, apart from the methods mentioned above, configure session timeout can also be used in the web.xml file.

50. How can you upload a file using JSP?

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.

51. Where are the uploaded files stored?

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.

52. What is JSP page redirection? When is it used?

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.

53. Explain the difference between <jsp:forward page = ... > and response.sendRedirect(URL).

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.

54. What is a hit count for a Webpage?

A hit count is a method for finding out the number of visits on a particular website page.

55. How is the hit counter implemented in JSP?

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.

56. How is the loss of count data with each restart of the application using a hit counter avoided?

  • The database table is defined with a single count; the hitcount value should be zero.
  • With every hit, read the table to get the value.
  • Update the hitcount value each time by increasing the value of the hitcount by one for every read.
  • Display the new value of hitcount as total page hit counts.
  • To obtain a hitcount for all the pages, the above logic is implemented for all the pages.

57. What is an auto-refresh feature?

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.

58. How to implement the auto-refresh in JSP?

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.

59. What is JSTL?

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.

60. List out the different types of JSTL tags?

Various types of JSTL tags in use are:

  • Core Tags
  • Formatting tags
  • SQL tags
  • XML tags
  • JSTL Functions.

JSP Interview Questions For 2 Years Experienced

61. What is the use of the <c: set > tag?

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.

62. When is the <c:remove > tag used?

The <c: remove > tag removes a variable from a specified scope.

63. What is the use of the <c: choose> tag?

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.

64. What is the use of the <c: redirect > tag?

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.

65. What is the use of the <c:url> 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.

66. What are JSTL formatting tags?

The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Websites. 

Syntax:

<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>

67. What are JSTL SQL tags?

The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, MySQL, or Microsoft SQL Server.

Syntax:

<%@ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>

69. What are JSTL XML tags?

The JSTL XML tags provide a JSP-related way to create and manipulate XML documents. 

Syntax:

<%@ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>

70. What is the JSP Expression Language?

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. 

Syntax:

${expr}

71. What are the implicit EL objects that are used in JSP?

You can use the following implicit objects in JSP

  • param − Request parameters as strings
  • paramValues − Request parameters as collections of strings
  • pageScope − Scoped variables from page scope
  • requestScope − Scoped variables from request scope
  • sessionScope − Scoped variables from session scope
  • applicationScope − Scoped variables from the application scope
  • cookie − Cookie values
  • pageContext − The JSP PageContext object for the current page
  • headerHTTP − request headers as strings
  • headerValues − HTTP request headers as collections of strings
  • initParam − Context-initialization parameters

72. Describe the errors that you encountered in a JSP code?

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.

73. What is Internationalization?

When a website is permitted to provide different versions of content translated into the visitor's language or nationality, it is called Internationalization.

74. What is Localization?

Localization is adding resources to a website to adapt it to a particular geographical or cultural region, for example, Hindi translation to a website.

75. What is the locale?

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.

Example:

""en_US" represents an English locale for the US.

76. Explain the difference between <%-- comment --%> and <!-- comment -->?

<%-- comment --%> is a JSP comment ignored by the JSP engine.

<!-- comment --> is an HTML comment ignored by the browser.

77. Can the JSP technology be extensible?

YES, JSP technology is extensible with the development of custom actions, or tags, encapsulated in tag libraries.

Conclusion

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!

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Core Java TrainingNov 23 to Dec 08View Details
Core Java TrainingNov 26 to Dec 11View Details
Core Java TrainingNov 30 to Dec 15View Details
Core Java TrainingDec 03 to Dec 18View Details
Last updated: 13 May 2023
About Author

 

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 .

read less
  1. Share:
Java Articles