Blog

Blog

Top 50+ JSP Interview Questions You Need to Know in 2023

JSP Interview Questions

JSP Interview Questions

1) Explain JSP and tell its uses.

JavaServer

Pages (JSP) is a technology that allows developers to create dynamic web pages based on HTML, XML, or other document types. It is a Java-based technology that enables developers to create web pages that can display dynamic content by including JSP tags and elements in the HTML code of the page.

JSP is typically used to create web applications that require dynamic content, such as interacting with databases or displaying real-time data. It is often used in combination with Java servlets, which handle the back-end processing of the web application.

One of the main advantages of JSP is that it allows developers to easily mix static and dynamic content within the same web page. This can make it easier to create web applications that have a consistent look and feel, while still providing the ability to display dynamic content.

Some other uses of JSP include:

  • Creating online forms and surveys
  • Displaying dynamic lists or tables of data
  • Creating shopping carts and online stores
  • Displaying real-time data or updates
  • Interacting with databases to retrieve and display information

Overall, JSP is a useful tool for creating dynamic web applications that require the ability to display and interact with data in real-time.


2) What is the requirement of a tag library?

A collection of custom tags is called a Tag Library. Recurring tasks are handled more easily and reused across multiple applications to increase productivity. They are used by Web Application designers who focus on presentation rather than accessing database or other services. Some popular libraries are String tag library and Apache display tag library.

3) Explain JSP Technology.

JavaServer Pages (JSP) is a technology that allows developers to create dynamic web pages based on HTML, XML, or other document types. It is a Java-based technology that enables developers to create web pages that can display dynamic content by including JSP tags and elements in the HTML code of the page.

JSP pages are compiled into Java servlets by a JSP compiler and run on a Java-enabled web server. The output of a JSP page is typically HTML, but it can also be XML or other types of documents.

JSP technology includes several features that make it easy for developers to create dynamic web pages, including:

  • JSP tags and elements: These allow developers to include dynamic content in the HTML code of a page. For example, a developer can use a JSP tag to retrieve data from a database and display it in a table on the page.
  • Expression Language (EL): This allows developers to access and manipulate data stored in JavaBeans or other Java objects. It provides a simple syntax for accessing and manipulating data, making it easy for developers to include dynamic content in a page.
  • Custom tags: Developers can create their own custom JSP tags to encapsulate complex behavior or reuse code. This can make it easier to create and maintain large, complex web applications.

Overall, JSP technology provides a powerful and flexible way for developers to create dynamic web pages and web applications. It is widely used in the development of enterprise-level applications and is an important part of the Java platform for web development.

4) Explain Implicit objects in JSP.

Objects created by web container and contain information regarding a particular request, application or page are called Implicit Objects. They are :

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

5) How can multiple submits due to refresh button clicks be prevented?

Using a Post/Redirect/Get or a PRG pattern, this problem can be solved.

1) A form filled by the user is submitted to the server using POST or GET method. The state in the database and business model are updated.

2) A redirect response is used to reply by the servlet for a view page.

3) A view is loaded by the browser using the GET command and no user data is sent. This is safe from multiple submits as it is a separate JSP page.

6) Is JSP technology extensible?

Yes, JavaServer Pages (JSP) technology is extensible. This means that developers can create custom JSP tags and elements to encapsulate complex behavior or reuse code, making it easier to create and maintain large, complex web applications.

Custom JSP tags are defined in tag libraries, which are collections of custom tags that can be used in JSP pages. A tag library is defined using a tag library descriptor (TLD) file, which specifies the tags in the library and how they should be used.

Developers can create custom JSP tags using Java code and the JSP API. The JSP API provides a set of classes and interfaces that can be used to create custom tags and elements.

Overall, the ability to create custom JSP tags and elements makes JSP technology highly extensible and enables developers to create web applications that are tailored to their specific needs.

7) Differentiate between response.sendRedirect(url) and <jsp:forward page = …> .

<jsp.forward> element forwards the request object from 1 JSP file to another. Target file can be HTML, servlet or another JSP file, but it should be in the same application context as forwarding JSP file.

sendRedirect send HTTP temporary redirect response to the browser. The browser then creates a new request for the redirected page. It kills the session variables.

8) Can a subsequent request be accessed with one’s servlet code, if a request attribute is already sent in his JSP?

The request goes out of scope, thus, it cannot be accessed. However, if a request attribute is set in one’s servlet, then it can be accessed in his JSP.

A JSP is a server side component and the page in translated to a Java servlet, and then executed. Only HTML code is given as output.

9) How to include static files in a JSP page?

Static pages are always included using JSP include directive. This way the inclusion is performed in the translation phase once. Note that a relative URL must be supplied for file attribute. Although static resources may be included, it is not preferred as each request requires inclusion.

10) Why is it that J Component have add() and remove() methods but Component doesn’t?

J Component is a subclass of Container. It contains other Components and JComponents.

JSP Interview Questions for Experienced Professionals

Below are the JSP interview questions and answers for experienced candidates:

11) How can a thread safe JSP page be implemented?

It can be done by having them implemented by the SingleThreadModel Interface. Add <%@page is ThreadSafe=”false” %> directive in the JSP page.

12) How can the output of JSP or servlet page be prevented from being cached by the browser?

Using appropriate HTTP header attributes to prevent the dynamic content output by a JSP page from being cached by the browser.

13) How to restrict page errors display in a JSP page?

By setting up an “ErrorPage” attribute of PAGE directory to the name of the error page in the JSP page, and then in the error jsp page set “isErrorpage=”TRUE”, Errors can be stopped from getting displayed.

14) What are JSP Actions?

They are XML tags, which direct the server to using existing components or control behavior of JSP Engine. They consist of a typical prefix of “jsp:” and action name.

<jsp:include/>
<jsp:getProperty/>
<jsp:forward/>
<jsp:setProperty/>
<jsp:usebean/>
<jsp:plugin/>

15) Differentiate between <jsp:include page=…> and <%@include file=…>.

Both these tags include information from 1 page to another.

The first tag acts as a function call between two Jsp’s. It is executed each time client page is accessed by the client. It is useful to modularize the web application. New content is included in the output.

The second tag content of file is textually embedded having similar directive. The changed content is not included in the output. It is helpful when code from one jsp is required by several jsp’s.

16) Can constructor be used instead of init(), to initialize servlet?

Yes, it is possible. But it is not preferred because init() was developed because earlier Java versions could not invoke constructors with arguments dynamically. So they could not assign a servletConfig. Today, however, servlet containers still call only no-arg constructor. So there is no access to servletContext or servletConfig.

17) Explain lifecycle methods.

1) jsplnit(): The container calls this to initialize servlet instance. It is called only once for the servlet instance and preceded every other method.

2) _jspService(): The container calls this for each request and passes it on to the objects.

3) jspDestroy(): It is called by the container just before destruction of the instance.

18) Explain JSP Output comments?

They are comments that can be viewed in HTML Source File.

19) Define Expression

Expression tag is used to insert Java values directly in the output. Its syntax is

<%=expression%>

It contains a scripting language expression that is evaluated, then converted to a string, and then inserted where the expression comes in JSP file.

20) Define Composition.

Composition has a stronger relationship with the object than Aggregation.

21) Define JSP Scriptlet.

It a JSP tag that encloses Java code in JSP pages. Their syntax is <% %>. Code written in scriptlet executes every time the program is run.

22) How can information from one JSP be passed to another JSP?

The tag <Jsp:param> allows us to pass information between multiple Jsp’s.

23) Explain the uses of <jsp:usebean> tag.

<jsp:useBean>


id="beanInstName"


scope= "page | application"


class="ABC.class"  type="ABC.class"


</jsp:useBe

This tag creates an instance of java bean. It firstly tries to find if bean instance already exist and assign stores a reference in the variable. Type is also specified; otherwise it instantiates from the specified class storing a reference in the new variable.

24) Explain handling of runtime exceptions.

Errorpage attribute is used to uncatch the run-time exceptions forwarded automatically to an error processing page.

It redirects the browser to JSP page error.jsp if any uncaught exception is faces during request handling. It is an error processing page.

25) Why does _jspService() start with an ‘_’ but other lifecycle methods do not?

Whatever content made in a jsp page goes inside the _jspService() method by the container. If it is override, the compiler gives an error, but the other 2 lifecycles can be easily override. So ‘_’ shows that we cannot override this method.


26) Explain the various scope values for <jsp:useBean> tag.

<jsp:useBean> tag is used to use any java object in the jsp page. Some scope values are :

1)application

2)request

3)page

4)session

27) Show the 2 types of comments in JSP.

The 2 types are :

<%–JSP Comment–%>
<!–HTML comment–>

28) Can Static method be Override?

We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.

29) Explain JSP directives.

JSP Directives are messages to JSP Engine. They serve as a message from page to container and control the processing of the entire page. They can set global values like class declaration. They do not produce output and are enclosed in <%@….%>

30) Explain pge Directives.

Page Directives inform the JSP Engine about headers and facilities that the page receives from the environment. It is found at the top of all JSP pages. Its syntax is <%@ page attribute=”value”>

31) Show attributes of page directives.

  1. Session: It shows if a session data is available to the page.
  2. Import: it shows packages that are imported.
  3. isELIgnored: It shows whether EL expressions are ignored when JSP translates into a servlet.
  4. contentType: it allows the user to specify the content type of page.

32) What is Include directive?

The include directive statically inserts the contents of a resource into the current JSP. It helps in the reuse of code without duplication. and includes contents of the file at translation time. Its syntax is as follows <%@ include file=”Filename”%>.

33) What are standard actions in JSP?

They affect overall runtime behaviour of a page and response sent to the client. They are used to include a file at request time, to instantiate a JavaBean or find one. They are also used to generate a browser-specific code or forward a request to a new page.

34) Explain the jsp:setProperty action.

It is used to give values to properties of beans that have been referenced beforehand.

<jsp:useBean id=”ABC”…/>…

<jsp:setProperty name=”ABC” property=”myProperty”…

jsp:setproperty is executed even if a new bean is instantiated or existing bean is found.

By adding </jsp.useBean> at the end of the code, the condition for execution is inverted i.e. It is not executed if existing object was found and only if a new object was instantiated.

35) Define Static Block.

It is used to start the static data member. It is executed before class loading.

36) Explain jsp:plugin action.

This action helps in insertion of a specific object in the browser or embed the element needed to specify the running of applet using Java plugin.

37) Explain client and server side validation.

Javascript is used for the client-side validation. It takes place within the browser. Javascript is used to submit the form data if validation is successful. Validation errors require no extra network trip because form cannot be submitted.

Validation is also carried out in the server after submission. If validation fails, extra network trip is required to resend the form to the client.

38) What is Translation Phase?

JSP engine translates and compiles a JSP file to a servlet. This servlet moves to the execution phase where requests and responses are handled. They are compiled for the first time they are accessed unless manually compiled ahead of time. The manual or explicit compilation is useful for long and convoluted programs.

39) Perform a Browser Redirection from a JSP Page.

<% response.sendRedirect(URL); %>

or we can change the location of the HTTP header attribute as follows:

<% response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader(URL); %>

40) Give uses of Object Cloning.

Object cloning is used to create an exact copy of an object by typing the same code or using various other techniques.

41) How to forward a request to another source.

<jsp:forward page="/Page2.jsp" />

42) How can Automatic creation of session be prevented in a JSP page?

JSP page automatically create sessions for requests. By typing the following, it can be avoided.

<%@ page session=”false”  %>

43) How can you avoid scriptlet code in JSP?

JavaBeans or Custom Tags can be used instead of scriptlet code.

44) Explain the jspDestroy() method.

Whenever a JSP page is about to be destroyed, the container invokes the jspDestroy() method from the javax.servlet.jsp.JspPage interface. Servlets destroy methods are similar to it. It can be easily overridden to perform cleanup, like when closing a database connection.

45) Explain the <jsp:param> action.

It is an action used with include or forward standard actions. It helps in passing the parameter names and values to a resource.

46) Explain static method.

A static method is of the class and not the object of a class. It can be invoked without instance of a class. Static members can also access the static data and change its value.

47) How to disable scripting?

Scripting can be easily disabled by setting scripting-invalid element of the deployment descriptor to true. It is a sub-element of property group. Its can be false as well.

48) Define JSP Declaration.

JSP Declaration are tags used in declaring variables. They are enclosed in <%!%> tag. They are used in declaring functions and variables.

<%@page contentType=”text/html” %>


<html>
<body>
<%!
	int a=0;
	private int getCount(){
	a++;
return a;
}%>
<p>Values of a are:</p>
<p><%=getCount()%></p>
</body>
</html>

49) How can HTML Output be prevented from being cached?

<%
response.setHeader("Cache-Control", "no=store");
response.setDateHeader("Expires", 0);
%>

50) How is JSP better than Servlet technology?

JSP is a technology on the server’s side to make content generation simple. They are document centric, whereas servlets are programs. A Java server page can contain fragments of Java program, which execute and instantiate Java classes. However, they occur inside HTML template file. It provides the framework for development of a Web Application.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!