Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address
Check our LIVE MOCK INTERVIEWS

JSP Interview Questions and Answers

Test your skills through the online practice test: JSP Quiz Online Practice Test

Freshers / Beginner level questions & answers

Ques 1. What is a JSP and what is it used for?

Java Server Pages (JSP) is a platform independent presentation layer technology that comes with SUN s J2EE platform. JSPs are normal HTML pages with Java code pieces embedded in them. JSP pages are saved to *.jsp files. A JSP compiler is used in the background to generate a Servlet from the JSP page.

Is it helpful? Add Comment View Comments
 

Ques 2. What are the two kinds of comments in JSP and what's the difference between them ?

<%-- JSP Comment --%>
<!-- HTML Comment -->

Is it helpful? Add Comment View Comments
 

Ques 3. What is JSP technology?

he JavaServer Pages technology enables you to generate dynamic web content, such as HTML, DHTML, XHTML, and XML files, to include in a Web application. JSP files are one way to implement server-side dynamic page content. JSP files allow a Web server, such as Apache Tomcat, to add content dynamically to your HTML pages before they are sent to a requesting browser.
When you deploy a JSP file to a Web server that provides a servlet engine, it is preprocessed into a servlet that runs on the Web server. This is in contrast with client-side JavaScript™ (within SCRIPT tags), which is run in a browser. A JSP page is ideal for tasks that are better suited to execution on the server, such as accessing databases or calling Enterprise Java™ beans. You can create and edit a JSP file in the HTML editor by adding your own text and images using HTML, JSP tagging, or JavaScript, including Java source code inside of scriptlet tags. Typically, JSP files have the file extension .jsp. Additionally, the JSP specification suggests that JSP fragment files should have file extension .jspf. If this convention is not followed, the JSP validator will treat JSP fragments as regular standalone JSP files, and compilation errors might be reported.

Is it helpful? Add Comment View Comments
 

Ques 4. What is JSP page?

A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.

Is it helpful? Add Comment View Comments
 

Ques 5. What are the implicit objects?

Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:
--request
--response
--pageContext
--session
--application
--out
--config
--page
--exception

Is it helpful? Add Comment View Comments
 

Ques 6. How many JSP scripting elements and what are they?

There are three scripting language elements:
--declarations
--scriptlets
--expressions

Is it helpful? Add Comment View Comments
 

Ques 7. What is a Java package and how is it used?

A Java package is a naming context for classes and interfaces. A package is used to create a separate namespace for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.

Is it helpful? Add Comment View Comments
 

Ques 8. What are the Object and Class classes used for?

The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.

Is it helpful? Add Comment View Comments
 

Ques 9. How many JSP scripting elements are there and what are they?

There are three scripting language elements: declarations, scriptlets, expressions.

Is it helpful? Add Comment View Comments
 

Ques 10. What is the difference between JSP and Servlets?

JSP is used mainly for presentation only. A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP. But a servlet can support any protocol like HTTP, FTP, SMTP etc.

Is it helpful? Add Comment View Comments
 

Ques 11. Difference between GET and POST.

In GET your entire form submission can be encapsulated in one URL, like a hyperlink. query length is limited to 255 characters, not secure, faster, quick and easy. The data is submitted as part of URL.

In POST data is submitted inside body of the HTTP request. The data is not visible on the URL and it is more secure.

Is it helpful? Add Comment View Comments
 

Ques 12. What is a output comment?

A comment that is sent to the client in the viewable page source.The JSP engine handles an output comment as uninterpreted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser.

JSP Syntax:

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

Example 1

<!– This is a commnet sent to client on

<%= (new java.util.Date()).toLocaleString() %>

–>

Is it helpful? Add Comment View Comments
 

Ques 13. What is a Hidden Comment?

A comments that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags. A hidden comment is not sent to the client, either in the displayed JSP page or the HTML page source. The hidden comment is useful when you want to hide or "comment out" part of your JSP page.

You can use any characters in the body of the comment except the closing –%> combination. If you need to use –%> in your comment, you can escape it by typing –%>.

JSP Syntax

<%– comment –%>

Examples

<%@ page %>

<html>

<head><title>A Hidden Comment </title></head>

<body>

<%– This comment will not be visible to the colent in the page source –%>

</body>

</html>

Is it helpful? Add Comment View Comments
 

Ques 14. How do I include static files within a JSP page?

Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.

Is it helpful? Add Comment View Comments
 

Ques 15. What are the two kinds of comments in JSP and what’s the difference between them.

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

Is it helpful? Add Comment View Comments
 

Ques 16. What is a Expression?

An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file. Like

<%= someexpression %>

<%= (new java.util.Date()).toLocaleString() %>

You cannot use a semicolon to end an expression

Is it helpful? Add Comment View Comments
 

Ques 17. What is a Declaration?

A declaration declares one or more variables or methods for use later in the JSP source file.

A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. The declaration must be valid in the scripting language used in the JSP file.

<%! somedeclarations %>

<%! int i = 0; %>

<%! int a, b, c; %>

Is it helpful? Add Comment View Comments
 

Ques 18. What is a Scriptlet?

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

1.Declare variables or methods to use later in the file (see also Declaration).

2.Write expressions valid in the page scripting language (see also Expression).

3.Use any of the JSP implicit objects or any object declared with a <jsp:useBean> tag.

You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet.

Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it.

Is it helpful? Add Comment View Comments
 

Ques 19. Why are JSP pages the preferred API for creating a web-based client program?

Because no plug-ins or security policy files are needed on the client systems(applet does). Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.

Is it helpful? Add Comment View Comments
 

Ques 20. Difference between forward and sendRedirect?

When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.

Is it helpful? Add Comment View Comments
 

Ques 21. What are the different scope valiues for the <jsp:useBean>?

The different scope values for <jsp:useBean> are

1. page
2. request
3.session
4.application

Is it helpful? Add Comment View Comments
 

Ques 22. Explain the life-cycle mehtods in JSP?

THe generated servlet class for a JSP page implements the HttpJspPage interface of the javax.servlet.jsp package. Hte HttpJspPage interface extends the JspPage interface which inturn extends the Servlet interface of the javax.servlet package. the generated servlet class thus implements all the methods of the these three interfaces. The JspPage interface declares only two mehtods – jspInit() and jspDestroy() that must be implemented by all JSP pages regardless of the client-server protocol. However the JSP specification has provided the HttpJspPage interfaec specifically for the JSp pages serving HTTP requests. This interface declares one method _jspService().

The jspInit()- The container calls the jspInit() to initialize te servlet instance.It is called before any other method, and is called only once for a servlet instance.

The _jspservice()- The container calls the _jspservice() for each request, passing it the request and the response objects.

The jspDestroy()- The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance.

Is it helpful? Add Comment View Comments
 

Ques 23. How do I prevent the output of my JSP or Servlet pages from being cached by the browser?

You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.

<%

response.setHeader("Cache-Control","no-store"); //HTTP 1.1

response.setHeader("Pragma","no-cache"); //HTTP 1.0

response.setDateHeader ("Expires", 0); //prevents caching at the proxy server

%>

Is it helpful? Add Comment View Comments
 

Ques 24. What is the difference between variable declared inside a declaration part and variable declared in scriplet part?

Variable declared inside declaration part is treated as a global variable.that means after convertion jsp file into servlet that variable will be in outside of service method or it will be declared as instance variable.And the scope is available to complete jsp and to complete in the converted servlet class.where as if u declare a variable inside a scriplet that variable will be declared inside a service method and the scope is with in the service method.

Is it helpful? Add Comment View Comments
 

Ques 25. Is there a way to execute a JSP from the comandline or from my own application?

There is a little tool called JSPExecutor that allows you to do just that.

Is it helpful? Add Comment View Comments
 

Ques 26. How does JSP handle run-time exceptions?

You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example:

<%@ page errorPage="error.jsp" %> redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, if you indicate that it is an error-processing page, via the directive: <%@ page isErrorPage="true" %> Throwable object describing the exception may be accessed within the error page via the exception implicit object. Note: You must always use a relative URL as the value for the errorPage attribute.

Is it helpful? Add Comment View Comments
 

Ques 27. How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?

You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" %> within your JSP page. With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine. More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition. SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use <%! %>. You should try really hard to make them thread-safe the old fashioned way: by making them thread-safe .

Is it helpful? Add Comment View Comments
 

Ques 28. How can I prevent the word “null” from appearing in my HTML input text fields when I populate them with a resultset that has null values?

You could make a simple wrapper function, like

<%!

String blanknull(String s) {

return (s == null) ? ”” : s;

}

%>

then use it inside your JSP form, like

<input type=”text” name=”lastName” value=”<%=blanknull(lastName)% >” >

Is it helpful? Add Comment View Comments
 

Ques 29. What is the difference between variable declared inside a declaration and variable declared in scriplet?

Variable declared inside declaration part is treated as a instance variable and will be placed directly at class level in the generated servlet.
<%! int k = 10; %>
Variable declared in a scriptlet will be placed inside _jspService() method of generated servlet.It acts as local variable.
<%
int k = 10;
%>

Is it helpful? Add Comment View Comments
 

Ques 30. What is the difference between ServletContext and PageContext?

ServletContext: Gives the information about the container and it represents an application.

PageContext: Gives the information about the Request and it can provide all other implicit JSP objects.

Is it helpful? Add Comment View Comments
 

Intermediate / 1 to 5 years experienced level questions & answers

Ques 31. What is difference between custom JSP tags and beans?

  • A custom JSP tag has to be interpreted and run inside a JSP; a Java Bean does not.
  • A custom JSP tag has to extend the javax.servlet.jsp.tagext.Tag interface; a Java Bean does not.
  • A custom JSP tag might use other Java Beans to do its work, but the reverse is not true.

Is it helpful? Add Comment View Comments
 

Ques 32. What is the difference between a Scrollbar and a ScrollPane in JSP?

A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.

Is it helpful? Add Comment View Comments
 

Ques 33. How do I use a scriptlet to initialize a newly instantiated bean?

A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.

The following example shows the “today” property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the jsp:setProperty action.

<jsp:useBean id=”foo” class=”com.Bar.Foo” >

<jsp:setProperty name=”foo” property=”today”

value=”<%=java.text.DateFormat.getDateInstance().format(new java.util.Date()) %>” / >

<%– scriptlets calling bean setter methods go here –%>

</jsp:useBean >

Is it helpful? Add Comment View Comments
 

Ques 34. What's a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization?

Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. If you anticipate your users to increase in the future, you may be better off implementing explicit synchronization for your shared data. The key however, is to effectively minimize the amount of code that is synchronized so that you take maximum advantage of multithreading.

Also, note that SingleThreadModel is pretty resource intensive from the server’s perspective. The most serious issue however is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the unserviced requests are queued until something becomes free – which results in poor performance. Since the usage is non-deterministic, it may not help much even if you did add more memory and increased the size of the instance pool.

Is it helpful? Add Comment View Comments
 

Ques 35. How is scripting disabled?

Scripting is disabled by setting the scripting-invalid element of the deployment descriptor to true. It is a subelement of jsp-property-group. Its valid values are true and false. The syntax for disabling scripting is as follows:

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

Is it helpful? Add Comment View Comments
 

Experienced / Expert level questions & answers

Ques 36. Why are JSP pages the preferred API for creating a web-based client program?

Because no plug-ins or security policy files are needed on the client systems(applet does). Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.

Is it helpful? Add Comment View Comments
 

Ques 37. Can we use the constructor, instead of init(), to initialize servlet?

Yes , of course you can use the constructor instead of init(). There?s nothing to stop you. But you shouldn?t. The original reason for init() was that ancient versions of Java couldn?t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won?t have access to a ServletConfig or ServletContext.

Is it helpful? Add Comment View Comments
 

Ques 38. How can a servlet refresh automatically if some new data has entered the database?

You can use a client-side Refresh or Server Push.

Is it helpful? Add Comment View Comments
 

Ques 39. Is JSP technology extensible?

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

Is it helpful? Add Comment View Comments
 

Ques 40. What is the ResourceBundle class in JSP?

The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program's appearance to the particular locale in which it is being run.

Is it helpful? Add Comment View Comments
 

Ques 41. How can a servlet refresh automatically if some new data has entered the database?

You can use a client-side Refresh or Server Push.

Is it helpful? Add Comment View Comments
 

Ques 42. The code in a finally clause will never fail to execute, right?

Using System.exit(1); in try block will not allow finally code to execute.

Is it helpful? Add Comment View Comments
 

Ques 43. In the Servlet 2.4 specification SingleThreadModel has been deprecated, why?

Because it is not practical to have such model. Whether you set isThreadSafe to true or false, you should take care of concurrent client requests to the JSP page by synchronizing access to any shared objects defined at the page level.

Is it helpful? Add Comment View Comments
 

Ques 44. How do I include static files within a JSP page?

Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.

Is it helpful? Add Comment View Comments
 

Ques 45. Why does JComponent have add() and remove() methods but Component does not? because JComponent is a subclass of Container, and can contain other components and jcomponents. How can I implement a thread-safe JSP page?

You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive



<%@ page isThreadSafe="false" % > within your JSP page.

Is it helpful? Add Comment View Comments
 

Ques 46. How can I enable session tracking for JSP pages if the browser has disabled cookies?

We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. URL rewriting essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input.
Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie. Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other.

Is it helpful? Add Comment View Comments
 

Ques 47. How do I prevent the output of my JSP or Servlet pages from being cached by the browser?

You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.

Is it helpful? Add Comment View Comments
 

Ques 48. How do you restrict page errors display in the JSP page?

You first set "Errorpage" attribute of PAGE directory to the name of the error page (ie Errorpage="error.jsp")in your jsp page .Then in the error jsp page set "isErrorpage=TRUE". When an error occur in your jsp page it will automatically call the error page.

Is it helpful? Add Comment View Comments
 

Ques 49. What is JSP Compiler and where are compiled JSP Java files?

JSP are automatically compiled at runtime.

The compiled JSP files are by default available in the /work folder of the Tomcat environment. There should be a subfolder Catalina which in turn has a subfolder representing the domain name which defaults to localhost. There in turn should be the project folder which in turn contains package hierarchy org.apache.jsp with therein the compiled JSP files.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related differences

JSP vs ASPPHP vs JSP

Related interview subjects

JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
Java 17 interview questions and answers - Total 20 questions
Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
Java 21 interview questions and answers - Total 21 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
©2023 WithoutBook