JSP Questions
JSP Questions
1. What is JSP?
5. What are JSP stands for Java Server Pages. This technology is used to create dynamic web pages in the form of HyperText
some of the Markup Language (HTML). They have embedded Java code pieces in them. They are an extension to the Servlet
advantages Technology and generate Servlet from a page. It is common to use both servlets and JSP pages in the same web
of using JSP? apps.
6. What is Java
Server
Template
Engines?
7. What are
Servlets?
8. Explain the
Life Cycle of 2. How does JSP work?
a servlet.
The JSP container has a special servlet called the page compiler. All HTTP requests with URLs that match the .jsp
9. What are the file extension are forwarded to this page compiler by the configuration of the servlet container. The servlet
types of container is turned into a JSP container with this page compiler. When a .jsp page is first called, the page compiler
elements parses and compiles the .jsp page into a servlet class. The JSP servlet class is loaded into memory on the
with Java successful compilation. For the subsequent calls, the servlet class for that .jsp page is already in memory. Hence,
Server Pages the page compiler servlet will always compare the timestamp of the JSP servlet with the JSP page. If the .jsp page
(JSP)? is more current, recompilation is necessary. With this process, once deployed, JSP pages only go through the
time-consuming compilation process once.
10. What is the
difference 3. How does JSP Initialization take place?
between
When a container loads a JSP, it invokes the jspInit() method before servicing any requests.
JSP and
Javascript?
public void jspInit(){
// Initialization code...
A servlet is loaded when a web server receives a request that should be handled by it. Once a servlet has been
loaded, the same servlet instance (object) is called to process succeeding requests. Eventually, the webserver
needs to shut down the servlet, typically when the web server itself is shut down.
9. What are the types of elements with Java Server Pages (JSP)?
The three types of elements with Java Server Pages (JSP) are directive, action, and scripting elements.
Element Description
<%@ page ... Defines page-dependent attributes, such as scripting language, error page,
%> and buffering requirements.
<%@
include ... Includes a file during the translation phase.
%>
<%@ taglib
Declares a tag library, containing custom actions, used on the page.
... %>
Element Description
This includes the response from a servlet or JSP page during the request
<jsp:include>
processing phase.
This is used to generate HTML that contains the proper client browser-
<jsp:plugin> dependent elements which are used to execute an Applet with Java
Plugin software.
Element Description
<% ...
Scriptlet used to embed scripting code.
%>
<%= ... Expression, used to embed Java expressions when the result shall be added to the
%> response. Also used as runtime action attribute values.
<%! ... Declaration used to declare instance variables and methods in the JSP page
%> implementation class.
+ Addition
* Multiplication
/ or div Division
for(inti=0;i<n;i++)
//block of statements
While(i<n)
//Block of statements
}
Iteration
Conditional logic
Catch exception
URL forward
Redirect, etc.
Following is the syntax to include a tag library:
20. Which methods are used for reading form data using JSP?
JSP is used to handle the form data parsing automatically. It dies so by using the following methods depending on
the situation:
getParameter() − To get the value of a form parameter, call the request.getParameter() method.
getParameterValues() − If a parameter appears more than once and it returns multiple values, call this
method.
getParameterNames() − This method is used if, in the current request, you want a complete list of all
parameters.
getInputStream() − This method is used for reading binary data streams from the client.
21. What is an Exception Object?
The exception object is an instance of a subclass of Throwable (e.g., java.lang. NullPointerException). It is only
available on the error pages. The following table lists out the important methods available in the Throwable
class:
Returns a detailed message about the exception that has occurred. This message is
initialized in the Throwable constructor.
Returns the name of the class concatenated with the result of getMessage().
Prints the result of toString() along with the stack trace to System.err, the error output
stream.
Returns an array containing each element on the stack trace. The element at index 0
represents the top of the call stack, and the last element in the array represents the
method at the bottom of the call stack.
Fills the stack trace of this Throwable object with the current stack trace, adding to any
previous information in the stack trace.
In the translation phase, the JSP container is responsible for converting the JSP page into a servlet and compiling
the servlet. This is used to automatically initiate the translation phase for a page when the first request for the
page is received.
In the “request processing” phase, the JSP container is also responsible for invoking the JSP page implementation
class to process each request and generate the response.
The elements of the page that are not JSP elements are simply called the “template text”. The template text is
commonly HTML, but it could also be any other text.
When a page request of JSP is processed, the template text and the dynamic content generated by the JSP
elements are merged, and the result is sent as the response to the browser.
jsp:forward: This action tag forwards the request and response to another resource.
jsp:include: This action tag is used to include another resource.
jsp:useBean: This action tag is used to create and locates bean objects.
jsp:setProperty: This action tag is used to set the value of the property of the bean.
jsp:getProperty: This action tag is used to print the value of the property of the bean.
jsp:plugin: This action tag is used to embed another component such as the applet.
jsp:param: This action tag is used to set the parameter value. It is used in forward and includes mostly.
jsp:fallback: This action tag can be used to print the message if the plugin is working.
For each request of the client, the service method of the JSP gets invoked hence the code inside the Scriptlet
executes for every request.
Here <%%> tags are scriptlet tags and within it, we can place the java code.
<html>
<body>
<%!
int a=0;
a++;
return a;
%>
<p>Values of a are:</p>
<p><%=getCount()%></p>
</body>
</html>
Conclusion
28. Conclusion
The Java 2 Enterprise Edition (J2EE) takes the task of building an Internet presence and transforms it to the point
where developers can use Java to efficiently create multi-tier, server-side applications. In late 1999, Sun
Microsystems added a new element to the collection of Enterprise Java tools, called the JavaServer Pages (JSP).
The JSP, built on top of Java servlets, is designed to increase the efficiency in which programmers, and even
nonprogrammers, can create web content.
JavaServer Pages helps in developing web pages that include dynamic content. A JSP page can change its content
based on any number of variable items. A JSP page not only contains standard markup language elements like a
regular web page but also contains special JSP elements that allow the server to insert dynamic content in the
page. This combination of standard elements and custom elements allows for the creation of powerful web apps.
References:
2. Which of the scripting of JSP not putting content into the service method of the converted servlet?
Scriptlets
Declarations
Expressions
3. How many JSP implicit objects are there that are created by the web container and are available to all the JSP
pages?
10
4. In JSP Action tags, which of the following tags are used for bean development?
jsp:useBean
jsp:setProperty
jsp:getProperty
jspInit()
jspDestroy()
6. Which of the following is the difference between JavaBeans and taglib directives?
9. In a JSP-based application, the types of beans that are primarily used are?
Value beans
Utility beans
10. Which of the following is the difference between Servlets and JSP?
Syntax
Compilation
Translation
11. The dynamic interception of requests and responses to transform the information is done by which of the
following?
Servlet filter
Servlet config
Servlet context
Servlet container
12. Which of the following is the correct order of phases in the JSP life cycle?
<%!directive%>
<%directive%>
<%=directive%>
<%@directive%>
15. Which of the following packages does a JSP API consist of?
javax.servlet.jsp.tagext
javax.servlet.jsp
java.servlet
16. JDBC is used to invoke SQL commands directly, which means it is a _____.
higher level
middle level
low level
user
useBean
export
import
include
18. Which of the following tags is used to execute java source code in JSP?
Declaration Tag
Scriptlet tag
Expression tag