Top 75+ JSP Interview Questions and Answers In 2020

Last updated on by Digamber

JSP is the abbreviation for Java server pages. If you are a programmer, you would have heard about this word a lot of times lately. You might probably know this subject in good detail. 

Whatever the case may be, we are here with this article to sharpen your knowledge about Java server pages.

You should have a good understanding of JSP if you want to get a job as a java developer, software engineer, or web application developer.

Not just these, but JSP is a subject often utilized in a variety of development tasks. In sum, if you have good knowledge of JSP, you have a promising career ahead waiting for you. 

Best 75+ JSP Interview Questions and Answers 2020 Examples

In this article, I am going to provide you with a lot of JSP questions that are often asked in Interviews along with their answers. Let’s move on to the Q and A tour.

1. Explain Java server pages?
JSP or Java server pages are a presentation layer technology that is a platform-independent. JSP comes along with SUN’s J2EE platform. 

In layman’s terms, Java server pages are ordinary HTML pages with Java codes encompassed in them.

2. Why do we use Java server pages? 
JSP technology is utilized to create dynamically generated web pages. Various other languages and protocols like HTML, XML, SOAP, etc. are also consumed in JSP. It can deliver HTML, XML, and even different sorts of documents using the output stream.

3. When did JSP release?
In 1999, Sun microsystems released JSP.

4. Why is JSP so favorable for dynamic web pages development?
JSP makes use of Java language for server-side scripting; they are secure, platform-independent, and simplifies dynamic web page creation and management.

5. What are the requirements to run a JSP?
To run or deploy Java server pages, the requirement is a web server compatible with servlet containers like apache and tomcat.

6. How JSP works?
JSP technology enables interleaving of java codes and predefined function with static Html content. The page containing these codes and static web content is compiled and executed on the server, and the documents are delivered.

7. What is a JSP scriptlet?
JSP scriptlet is a Java code component that is executed when a request for the page is made.

8. What do you know about Implicit Objects? 
The objects created on the server by a web container are Implicit objects. It incorporates information like:

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

9. How many types of comments can a JSP hold?
A JSP can have a HTML comment of the form <!– HTML Comment –> or a JSP comment of the form <%– JSP Comment –%>.

10. Why JSP API preferred for web-based client programs?
The advantages of using JSP page API for web-based client programs are as follows:

  • There is no need for any sort of security policy files or plugins on the system of the client. 
  • Separating application programs from web design is possible, thus allowing cleaner and more application design.

11. How can we prevent code in the finally clause to execute?
To do so, we would need the following code: System.exit(1); in the try block.

12. How can we make JSP extensible?
We can create custom tags or actions in the tag libraries to make JSP extensible.

13. How Servlet refreshes automatically when some new data entered in the database?
The server could be refreshed when some new data entered in the database by using client-side Refresh or Server Push.

14. What is the way to fetch warnings?
SQL warnings tell about database access warnings. These warnings have nothing to do with the execution of the application, but they indicate something wrong or something not in the scheme as planned. 

Warnings are reportable on Statement object, Connection object, or Resultset object using the getWarnings method. We need to invoke this getWarnings method.

SQLWarning warning = stmt.getWarnings();

if (warning != null){
  while (warning != null)
  {
    System.out.println(\”Message: \” + warning.getMessage());
    System.out.println(\”SQLState: \” + warning.getSQLState());
    System.out.print(\”Vendor error code: \”);
    System.out.println(warning.getErrorCode());
    warning = warning.getNextWarning();
  }
}

15. What are the messaging models that JMS provides for?
Given below are the messaging models JMS provides for:

  • publish-and-subscribe 
  • point-to-point queuing.

16. Why is Class.forname used?
Class.forname is utilized to create and register an instance of the driver with DriverManager while loading the driver. The driver, once loaded, can make connections with the Database management system.

17. What information will you need to create a TCP Socket?
The information required to create a TCP socket is local systems port number and IP address and Remote System’s Port Number and IP Address.

18. How can one restrict the display of page errors on the java server page?
The method to restrict page error display on the JSP is to set the “Errorpage” attribute of the page directory as the name of the error page. Once you have done that, now you should set “isErrorpage=TRUE” in error JSP page. The reason for doing so is, whenever any error will occur in the JSP page, an error page will be automatically called.

19. What is the way to incorporate static files in a JSP page?
We can make use of JSP include directive to include static files within a JSP page. We would also require a relative URL for the file attribute. Action can also be utilized to include static files within a JSP page but using JSP include directive will perform inclusion only once at the time of translation. Whereas, using action, the composition will perform again and again, every time a request is made.

20. Name the JSP scripting elements. 
The JSP scripting elements are as follows-

  • declarations
  • scriptlets
  • expressions.

21. What is the JSP lifecycle method that is impossible to override?
It is not possible to override the _jspService() method. 

22. What can JSP lifecycle methods be possibly overridden?
The JSP lifecycle methods we can override are as follows:

  • jspInit()
  • jspDestroy()

23. What are the objectives of overriding jspInit()? 
This method jspInit() in a JSP page is overridden to allocate resources. These resources may be network connections, database connections, etc. for the JSP page.

24. Why will you override jspDestroy()?
This method in a JSP page can be useful to free any allocated resources.

25. Explain stored procedures. 
Stored procedures are simply a set of commands in the database that are pre-compiled, and thus every time a query is run, the database does not require to parse and compile SQL statements. 

26. Why do we make use of stored procedures in the database?
The advantages of stored procedures are speed and data manipulation. 

27. What will you do to restrict a browser caching the output of your JSP or servelet pages?
To prevent a browser from caching the output of a JSP or servlet page, we would need to set appropriate HTTP header attributes. 

28. What is the way to implement a thread-safe JSP page? 
To achieve this, we can add the directive <%@ page isThreadSafe=”false” % > within the JSP page.

29. What do you understand by el in JSP?
EL stands for expression language. EL is utilized in JSP to access data stored in the JavaBeans and requests, sessions, and other implicit objects. EL computes the accessed data using logical and arithmetic expressions. 

30. Name some data types that JSP supports?
Some data types that JSP supports are as follows-

  • integers
  • float
  • string
  • Boolean
  • null.

31. Do you know the syntax for Expression language in JSP?
The syntax is as follows: ${EL expression}. 

32. What is the way to print variables in JSP?
As JSP is java based, the codes utilized are java codes most of the . The way to print variables in JSP is as follows:

<%
  String name=request.getParameter(“Pname”);
  out.print(“welcome “+name);
%>

33. How many JSP tags are there, name them?
The total number of JSP tags are four Which are listed below-

  • Directive tag
  • Declaration tag
  • Scriptlet tag
  • Expression tag

34. What is the use of the directive tag in JSP?
The directive tags are utilized to import packages into the JSP application. Directive tags also define error handling pages and session information. 

35. Tell us the syntax for the directive tags?

<%@ directive attribute=”value” %>

<%@ directive attribute=”value” %>

36. What is the use of a declaration tag in JSP?
All the methods or variables that are consumed in java code are declared using the declaration tag. 

37. Tell us the syntax for the declaration tag?
The syntax for the declaration tag is <%! declaration; %>.

38. Why do we use the JSP param tag?
JSP param tag is utilized to perform specific tasks, and it’s one of the action tags. It provides information in the form of a key/value pair. 

39. What is the syntax for the JSP param action tag?
The syntax for JSP param tag is as follows:

jsp:param name=”paramName” value=”paramValue” /

40. What is the use of the JSP useBean action tag?
The JSP useBean tag locates the bean class. It instantiates the bean object. 

41. What attributes JSP useBean tag has?
Different attributes that JSP useBean tag has are as follows:

  • scope
  •  class
  • beanname.

42. What is the default scope value of the useBean action tag?
The default scope is page. 

43. Explain JSTL?
JSTL is the abbreviation for the JSP tag library. As the name suggests, it is a library of tags used in JSP to build applications. 

44. How many types of JSTL are there? 
JSTL depending upon its functionality are of five types. All five types of JSTL are as follows:

  • Core tags
  • Formatting tags
  • Function tags
  • SQL tags
  • XML tags 

44. Evaluate the use of each type of JSTL tags. 

  • Core tags core libraries and provide support for variables, URL management, and control flow. 
  • Function tags Perform string manipulation tasks. 
  • Formatting tags are used for data and time formatting, message formatting, etc. 
  • XML tags are utilized in order to create or manipulate XML documents. 
  • SQL tags help interact with databases. 

45. What is the way to create a session in JSP?
We can make use of the setAttribute from the session implicit object to create a session. An HTTP session is utilized to create a session for users. The following code illustrates the same:

session.setAttribute(“user”, name);

46. What is the way to remove a session in JSP?

session.removeAttribute(“user”);

This can be utilized to remove the session. If one wishes to remove the entire session, he can make use of session.invalidate();. 

47. What are JSPX?
JSPX are XML format to create JSP pages. It enables the separation of code from the view layer in distinct files. In JSPX, Javascript and XML are in different files. 

48. What will you perform browser redirection from a JSP page?
If we wish to redirect a browser to a different resource as a response, we can make use of response implicit objects. response.sendRedirect(). 

The other way is to change the Location HTTP header attribute. 

49. How in JSP run time exceptions are dealt with?
We can automatically forward run time exceptions to an error processing page using the errorPage attribute of the page directive. 

50. Can we stop the execution of the JSP page while the request is processing?
Yes, it is possible to stop the execution of the JSP page while the request is processing. This could be done by the return statement when the request processing needs to be terminated. This is what we call Preemptive termination of request processing. 

51. Is it possible for a JSP page to process HTML form data?
Yes, a JSP page can process HTML form data. Moreover, unlike servlet, you don’t need any methods like doGet() or doPost() in your JSP page. Request implicit objects can get you the data for the form input. You need to stipulate request implicit object in an expression or scriptlet. 

52. Tell us something about the custom tags in JSP?
User-defined elements in JSP are called custom tags. These custom tags in JSP are restored into operation on an object when the java server page is translated in servlet. 

53. How will you create custom tags in JSP?
The procedure to create custom tags in JSP is given below:

  • Creating a Tag handler class and overriding doStartTag(). 
  • Create a TLD file that includes information about the tag handler class and the tag. 
  • Use the tag created in it after creating a JSP file. 

54. Can we use Servletoutputstream object from within a java server page?
No, it is not possible. We could only make use of the JSPWriter object to reply to the client.  

55. Why _jspservice() Method Start With a ‘_’ while Other Life Cycle Methods Do Not start likewise? 
This method that is _jspservice() is written by the web containers and is not overridden by end-users. Such methods are generally written with a ‘_’. 

56. How can Information be passed From Jsp To Included Jsp?
This could be achieved using <%jsp:param> tag. 

57. Differentiate the use of Request.getrequestdispatcher() And Context.getrequestdispatcher()?

Incase of Request.getrequestdispatcher(), The relative path of the resource is required to create it, but the absolute path of the resources is needed to develop Context.getrequestdispatcher(). 

58. Differentiate between Servletcontext And Pagecontext?
The first one that is Servletcontext provides information about the container while the later one that is Pagecontext provides the information about the request. 

59. What is the way to delete a cookie in JSP?
If one wish to delete a cokkie in JSP, the following code can be utilized: 

Cookie myCookie = new Cookie(“name”,”value”);
response.addCookie(myCookie);
Cookie killmycook = new Cookie(“myCookie”,”value”);
killmycook.setMaxAge(0);
killmycook.setPath(/);
killmycook.addCookie(killmycook);

60. How can one connect to the database from JSP?
We would require to write a code to establish a connection utilizing scriptlets in JSP. This would connect us to a database from JSP. 

61. Explain Jsp:plugin Action. 
Jsp:plugin Action is useful in inserting a browser-specific object or inserts an element that would specify the browser to run an applet using the java plugin. 

62. How can scripting be disabled?

To disable scripting, we can set the scripting-invaild element of the deployment descriptor to true. 

63. Tell us the syntax for disabling scripting?
The sysntax is given below:

<jsp-property-group>
  <url-pattern>*.jsp</url-pattern>
  <scripting-invalid>true</scripting-invalid>
</jsp-property-group>

64. Define servlet?
Servlet runs inside a web container and it is a java program. 

65. Define the Servlet Context?
Servlet Context contains information about web containers and applications. It is an object that could record incidents, retrieve URL citations to resources, and set and store attributes that other servlets in the context may use.

66. Explain Servlet Mapping?
As the name suggests, Servlet mapping is an association between servlet and a URL pattern. The servlet-mapping can map requests to servlets. 

67. Clarify the discrepancy between the use of GET and POST methods?
When we use GET, the total form of submission can be encapsulated in one URL, like a hyperlink. It is faster, quicker, and easy to use but not secure. 

In POST, data is submitted inside the body of the HTTP request. This is more secure. 

68. Which approach would you prefer to enable thread-safe servlets and JSP’s, Singlethreadmodel Interface Or Synchronization? Give reason. 
I would use SingleThreadModel for low volume sites as it is easy to use. For high volume sites, I would prefer explicit synchronization for shared data. SingleThreadModel is resource-intensive from the server-side, and it may result in poor performance by not handling requests efficiently. 

69. Clarify the distinction in the use of Forward And Sendredirect?
In forward request, the request is forwarded to another server without informing the client. The client doesn’t know that a different server resource is processing the request, and it occurs in the web container. 

When Sendredirectmethod is invoked, the web container returns to the browser to request a new URL. When the browser makes new requests, objects stored in request attributes before the redirect occurs are lost. 

70. Explain hidden comments. 
Hidden comments on the JSP page, but the client doesn’t receive it. The code which is written in hidden tags is ignored and not.processed by the JSP engine. The syntax for hidden comments is below-given:

<!– comment [ <%= expression %> ]>

71. Differentiate between include directive and JSP include. 
In include directive, the file is included at the compile-time, which is static. 
In JSP include, the file is included at runtime, which makes it faster. The output is in the JSP file, and it is dynamic. 

72. What would happen if we statically include a Page In Another Jsp Page?
The inclusion of the content of another file in the current page takes place. Include directive directs the JSP engine to do so. This process is called static include. 

73. Explain Context Initialization Parameters.
Specified in .xml file, context initialization parameters are the initialization parameter for the application and do not depend on JSP or servlet. 

74. Tell us about the translation unit?
The conversion of JSP pages containing other HTML or JSP pages in one servlet class is called a translation unit. 

 

75. Elucidate the JSP lifecycle phases. 
A JSP engine does the following operation on receiving JSP pages-

  • Translation- Page is translated or parsed, and a java file or servlet is created. 
  • Compilation- the file created is compiled in the class file. 
  • Loading- the compiled file is now loaded
  • Instance creation- an instance of the servlet is created. 
  • jspInit() method is called
  • _jspService handles service calls
  • _jspDestroy destroys it when the servlet is not compelled.

Conclusion 

JSP, the technology to create web pages and applications with dynamic content, is essential for you to know. If you are good at Java, you will not find it hard to comprehend JSP concepts. 

Even if you are not so good at java programming, JSP is easy to learn. Moreover, this article would have helped you get a good understanding of JSP and related terminologies. 

Reading this article once will sharpen your JSP knowledge and will help you win at your next interview. 

JSP questions are often asked in java programming, web development, web application development, JSP, and other sorts of interviews; hence, it becomes a must to at least have the basic understanding of JSP. 

Thanks for reading, and I hope it helps.

Avatar
Digamber

Digamber Rawat is from Uttarakhand, located in northwestern part of India. He is a Full-stack developer by profession and a primary author of this blog.