Core Java

Important Interview Questions/Answers of JSP

Java Interview

“In this article, I am going to talk about some most important and most frequently ask JSP(advance java) interview question/answers to crack interview easily. If you want to stand out of thousands of candidates then read this article it will clear your concepts.”

Q1. What is JSP?

JavaServer Pages (JSP) is a technology that helps software developers create dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP and ASP, but it uses the Java programming language.

Q2. What are advantages of using JSP?

JSP pages easily combine static templates, including HTML or XML fragments, with code that generates dynamic content.

JSP pages are compiled dynamically into servlets when requested, so page authors can easily make updates to presentation code. JSP pages can also be precompiled if desired.

JSP tags for invoking JavaBeans components manage these components completely, shielding the page author from the complexity of application logic.

Developers can offer customized JSP tag libraries that page authors access using an XML-like syntax.

Web authors can change and edit the fixed template portions of pages without affecting the application logic. Similarly, developers can make logic changes at the component level without editing the individual pages that use the logic.

Q3. What are the advantages of JSP over Pure Servlets?

It is more convenient to write regular HTML than to have println statements that generate HTML. Allows separation of look from the content. In a JSP web designer can design web page separately and servlet programmers can insert the dynamic content separately.

Q4. Explain lifecycle of a JSP.

A JSP life cycle can be defined as the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet.

The following are the paths followed by a JSP

  • Compilation
  • Initialization
  • Execution
  • Cleanup

Q5. What is a scriptlet in JSP and what is its syntax?

A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.

Following is the syntax of Scriptlet:

<% code fragment %>

Q6. What are JSP Directives?

JSP directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP processing.

A JSP directive affects the overall structure of the servlet class. It usually has the following form:

<%@ directive attribute=”value” %>

Q7. What are the types of directive tags?

1) Page Directive
2) Include Directive
3) TagLib Directive

Q8. What are JSP actions?

JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.

There is only one syntax for the Action element, as it conforms to the XML standard:

<jsp:action_name attribute=”value” />

Q9. What are JSP literals?

The JSP expression language defines the following literals:

  • Boolean: true and false.
  • Integer: as in Java.
  • Floating point: as in Java.
  • String: with single and double quotes; ” is escaped as \”, ‘ is escaped as \’, and \ is escaped as \\.
  • Null: null. Advertisements.

Q10. What is a page directive?

The Page directive defines the attributes specific to the page file for the pageparser and the compiler. The basic syntax of Page directive is: <%@ PageLanguage=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” Trace=”true” %> The attributes of the Page directive are: Attributes.

Q11. What are various attributes Of page directive?

buffer: Specifies a buffering model for the output stream.

autoFlush: Controls the behavior of the servlet output buffer.

contentType: Defines the character encoding scheme.

errorPage: Defines the URL of another JSP that reports on Java unchecked runtime exceptions.

isErrorPage: Indicates if this JSP page is an URL specified by another JSP page’s errorPage attribute.

extends: Specifies a superclass that the generated servlet must extend.

import: Specifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes.

Q12. What is an isThreadSafe attribute?

The isThreadSafe option marks a page as being thread-safe. By default, all JSPs are considered thread-safe. If you set the isThreadSafe option to false, the JSP engine makes sure that only one thread at a time is executing your JSP.

Q13. What is language attribute?

The language attribute indicates the programming language used in scripting the JSP page.

For example, because you usually use Java as the scripting language, your language option looks like this:

<%@ page language=”java” %>

Q14. What is session attribute?

The session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.

Q15. What is the function of <jsp:include> action?

This action lets you insert files into the page being generated. The syntax looks like this:

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

Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested.

Q16. What is <jsp:useBean> action?

The useBean action is quite versatile. It first searches for an existing object utilizing the id and scope variables. If an object is not found, it then tries to create the specified object.

The simplest way to load a bean is as follows:

<jsp:useBean id=”name” class=”package.class” />

Q17. What is <jsp:forward> Action?

The forward action terminates the action of the current page and forwards the request to another resource such as a static page, another JSP page, or a Java Servlet.

The simple syntax of this action is as follows:

<jsp:forward page=”Relative URL” />

Q18. What is <jsp:plugin> Action?

The plugin action is used to insert Java components into a JSP page. It determines the type of browser and inserts the <object> or <embed> tags as needed.

Q19. What are JSP implicit objects?

JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.

Q20. What is a request object?

The request object is an instance of a javax.servlet.http.HttpServletRequest object. Each time a client requests a page the JSP engine creates a new object to represent that request.

The request object provides methods to get HTTP header information including form data, cookies, HTTP methods etc.

Q21. What is a response object?

The response object is an instance of a javax.servlet.http.HttpServletRequest object. Just as the server creates the request object, it also creates an object to represent the response to the client.

The response object also defines the interfaces that deal with creating new HTTP headers. Through this object the JSP programmer can add new cookies or date stamps, HTTP status codes etc.

Q22. What is the difference between JspWriter and PrintWriter?

The JspWriter object contains most of the same methods as the class. However, JspWriter has some additional methods designed to deal with buffering. Unlike the PrintWriter object, JspWriter throws IOExceptions.

Q23.What is the session Object?

The session object is an instance of javax.servlet.http.HttpSession and is used to track client session between client requests.

Q24. What is an application Object?

The application object is direct wrapper around the ServletContext object for the generated Servlet and in reality an instance of a javax.servlet.ServletContext object.

This object is a representation of the JSP page through its entire 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.

Q25. How to read form data using JSP?

JSP handles form data parsing automatically using the following methods depending on the situation:

getParameter():You call request.getParameter() method to get the value of a form parameter.

getParameterValues():Call this method if the parameter appears more than once and returns multiple values, for example checkbox.

getParameterNames():Call this method if you want a complete list of all parameters in the current request.

getInputStream():Call this method to read binary data stream coming from the client.

Q26. How can you delete a session data?

When you are done with a user’s session data, you have several options:

Remove a particular attribute:You can call public void removeAttribute(String name) method to delete the value associated with a particular key.

Delete the whole session:You can call public void invalidate()method to discard an entire session.

Setting Session timeout:You can call public void setMaxInactiveInterval(int interval) method to set the timeout for a session individually.

Log the user out:The servers that support servlets 2.4, you can calllogout to log the client out of the Web server and invalidate all sessions belonging to all the users.

xml Configuration:If you are using Tomcat, apart from the above mentioned methods, you can configure session time out in web.xml file as follows.

Q27. How do you implement hit counter in JSP?

To implement a hit counter you can make use of Application Implicit object and associated methods getAttribute() and setAttribute().

This object is a representation of the JSP page through its entire 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.

Q28. What is auto refresh feature?

Consider a webpage which is displaying live game score or stock market status or currency exchange ration. For all such type of pages, you would need to refresh your web page regularly using refresh or reload button on your browser.

JSP makes this job easy by providing you a mechanism where you can make a webpage in such a way that it would refresh automatically after a given interval.

Q29. How do you implement the auto refresh in JSP?

The simplest way of refreshing a web page is using method setIntHeader() of response object. Following is the signature of this method:

public void setIntHeader(String header, int headerValue)

This method sends back header “Refresh” to the browser along with an integer value which indicates time interval in seconds.