Java EE: Java Server Pages
-----------------------------------------------------------------------------------------
Java EE: Java Server Pages
-----------------------------------------------------------------------------------------
Java Server Pages Interview Questions & Answers
-----------------------------------------------------------------------------------------
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.
2) What are the two kinds of comments in JSP and what's the difference between them?
<%-- JSP Comment --%>
<!-- HTML Comment -->
3) How can you include the results of another page?
The results of another page can be included by using the following methods:
- Include Directive
- Include Action
4) How can I use JSP in the MVC model?
JSP is used in MVC in the presentation tasks. It is used as a view. The controller calls the model and the business classes that get the data. This data is rendered to the JSP for rendering it to the Client.
5) Mention the scope values for <jsp.useBean> tag.
There are mainly four scope values available for <jsp.useBean> tag.
- page
- request
- application
- session
6) What are the Literals used in JSP?
The Literals used in JSP are as follows:
- Null
- Boolean
- String
- Integer
- Float
7) What is the major difference between ServletContext and PageContext?
The major difference between ServletContect and PageContext is, the ServletContext is designed to provide information about the Container and on the other hand, the PageContext is designed to provide information about the Request.
8) 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.
9) 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
10) How many JSP Scripting Elements are there and what are they?
There are three scripting language elements:
--declarations
--scriptlets
--expressions
11) 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.
12) Is JSP technology extensible?
YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.
13) 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 Servlet Context.
14) How can a Servlet refresh automatically if some new data has entered the database?
You can use a client-side Refresh or Server Push.
15) 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.
16) How many Messaging Models do Jms provide for and what are they?
JMS provide for two messaging models, publish-and-subscribe and point-to-point queuing.
17) Which JSP lifecycle methods can be overridden?
We can override jspInit() and jspDestroy() methods using JSP declaration scripting element. We should override jspInit() methods to create common resources that we would like to use in JSP service method and override jspDestroy() method to release the common resources.
18) How can we avoid direct access of JSP pages from client browser?
We know that anything inside the WEB-INF directory can’t be accessed directly in a web application, so we can place our JSP pages in WEB-INF directory to avoid direct access to JSP page from the client browser. But in this case, we will have to configure it in deployment descriptor just like Servlets.
19) Can we use JSP implicit objects in a method defined in JSP Declaration?
No we can’t because JSP implicit objects are local to service method and added by JSP Container while translating JSP page to servlet source code. JSP Declarations code goes outside the service method and used to create class level variables and methods and hence can’t use JSP implicit objects.
20) How do we configure init params for JSP?
We can configure init params for JSP similar to servlet in web.xml file, we need to configure JSP init params with servlet and servlet-mapping element. The only thing differs from servlet is jsp-file element where we need to provide the JSP page location.
21) How to use JSP EL to get HTTP method name?
We can use pageContext JSP EL implicit object to get the request object reference and use dot operator to get the HTTP method name in JSP page. The JSP EL code for this will be ${pageContext.request.method}.
22) How can we handle exceptions thrown by JSP service method?
To handle exceptions thrown by the JSP page, all we need is an error page and define the error page in JSP using page directive.
To create a JSP error page, we need to set the page directive attribute isErrorPage value to true, then we can access exception implicit object in the JSP and use it to send a customized error message to the client.
23) What is the purpose of <jsp:useBean>?
The jsp:useBean action searches for the existence of the object with specified name. If not found, it creates one.
24) What is the purpose of <jsp:setProperty >?
This action sets the properties of a bean.
25) What is the purpose of <jsp:getProperty >?
This action retrieves the properties of a bean.
26) Give the use of session object.
The object is used between the client requests for the tracking of client sessions.
27) What Information is needed to create a TCP Socket?
The Local Systems IP Address and Port Number. And the Remote System’s IPAddress and Port Number.
28) What Class.forname will do while loading Drivers?
It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.
29) How to retrieve Warnings?
SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object.
30) How many JSP Scripting Elements are there and what are they?
There are three scripting language elements: declarations, scriptlets, expressions.
31) 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.
32) What are Stored Procedures? How are they useful?
A stored procedure is a set of statements/commands which reside in the database. The stored procedure is pre-compiled and saves the database the effort of parsing and compiling sql statements every time a query is run. Each database has its own stored procedure language, usually a variant of C with a SQL preproceesor. Newer versions of db’s support writing stored procedures in Java and Perl too. Before the advent of 3-tier/n-tier architecture it was pretty common for stored procs to implement the business logic( A lot of systems still do it). The biggest advantage is of course speed. Also certain kind of data manipulations are not achieved in SQL. Stored procs provide a mechanism to do these manipulations. Stored procs are also useful when you want to do Batch updates/exports/houseKeeping kind of stuff on the db. The overhead of a JDBC Connection may be significant in these cases.
33) 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.
34) What JSP Lifecycle Methods can I override?
You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy().
The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations.
35) How do I perform Browser Redirection from a JSP Page?
You can use the response implicit object to redirect the browser to a different resource, as: response. send Redirect ; You can also physically alter the Location HTTP header attribute, as shown below: You can also use the:
Also note that you can only use this before any output has been sent to the client. I beleve this is the case with the response.sendRedirect() method as well. If you want to pass any paramateres then you can pass using.
36) How does JSP handle Run-time Exceptions?
You can use the errorPage attribute of the page directive to have uncaught runtime exceptions automatically forwarded to an error processing page.
For example: 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: the Throwable object describing the exception may be accessed within the error page via the exception implicit object.
37) 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.
38) Is there a way to reference the "this" variable within a JSP Page?
Yes, there is. Under JSP 1.0, the page implicit object is equivalent to "this", and returns a reference to the Servlet generated by the JSP page.
39) Can a JSP Page instantiate a Serialized Bean?
No problem! The use Bean action specifies the beanName attribute, which can be used for indicating a serialized bean.
40) Can you disable the caching on the back button of a particular browser?
Yes, The Caching process can be disabled on the back button of the browser. To do so, we can use the following code below.
-----------------------------------------------------------------------------------------