JSP
JSP
1. Request
2. Response
3. Application
4. Exception
5. Page
6. Config
7. Session
10. How will you extend JSP code?
We can extend JSP code by using Tag libraries and Custom actions.
11. How will you handle runtime exceptions in JSP?
12. How will you prevent multiple submits of a page that come
by clicking refresh button multiple times?
We can use Post Redirect Get (PRG) pattern to solve the issue of multiple
submission of same data. It works as follows:
First time when a user submits a form to server by POST or GET method, then we
update the state in application database.
Then we load a view by using GET command. There is no data is sent in this. Since
this a new JSP page, it is safe from multiple submits. The code that processes the
request is idempotent. So it does not do same action twice for same request.
13. How will you implement a thread safe JSP page?
We can use include directive of JSP to include a Static page in JSP. In this
approach, we use translation phase to include a static page. We have to specify the
URL of the resource to be included as file attribute in this directive.
1. jspInit(): This method is invoked when the JSP is called for the first
time. We can do initial setup for servicing a request in this method.
2. Fast: Since JSP is pre-compiled, server can serve the pages very fast.
In JSP we can write Java code seamlessly. It allows for writing code that can
interact with the rest of the application.
Javascript code is mostly executed at client side. This limits the tasks that can be
done in Javascript code. We cannot connect to database server from Javascript at
the client side.
1. Page: This directive is used for page related attributes. It can be put
anywhere in the JSP page. But by convention we put it on the top of the
page.
E.g.
<%@ page attribute="value" %>
2. Taglib: We can create custom tags in JSP and use these by taglib
directive in a JSP page.
E.g.
<%@ taglib uri=“abc.html” prefix=“tag_prefix” >
3. Include: We use include directive to read a file and merge its content
with the JSP page. This is done during compilation stage.
Session attribute in JSP is used for HTTP session mechanism. If we do not want to
use HTTP session in JSP, then we set this attribute to false. If it is set to true, we
can use built in session object in JSP.
22. What are the different scopes of a JSP object?
A JSP object, implicit or explicit, can have one of the following scopes:
1. Page: In this scope, the object is accessible from the page where it was
created. Important point here is that when a user refreshes the page, the
objects of this scope also get created again.
3. Session: In this scope, the object is available throughout the same HTTP
session.
In JSP, pageContext is an implicit object. This is used for storing and accessing all
the page scope objects of JSP.
We use jsp:useBean to invoke the methods of a Java Bean class. The Java Bean
class has some data and setter/getters to access the data.
With this tag, container will try to locate the bean. If bean is not already loaded
then it will create an instance of a bean and load it. Later this bean can be used in
expressions or JSP code.
25. What is difference between include Directive and include Action
of JSP?
Some of the main differences between include Directive and include Action are as
follows:
3. Include directive is just copying of content from another file to JSP code
and then it goes through compilation. Include action will dynamically
process the resource being called and then include it in the JSP page.
26. How will you use other Java files of your application in JSP code?
We can use import tag to import a Java file in JSP code. Once a file is imported, it
can be used by JSP code. It is a very convenient method to use Java classes in JSP
code.
For better organization of Java code, we should create a package of classes that we
are planning to use in JSP code.
27. How will you use an existing class and extend it to use in the JSP?
We can use extends attribute in include tag to use an existing class and extend it in
the current JSP.
E.g.
All the code that we write in a JSP goes into _jspService method during translation
phase. We cannot override this method. Where as other lifecycle methods jspInit()
and jspDestroy() can be overridden.
It appears that container uses _ symbol to distinguish the method that cannot be
overridden by client code.
Taglib is also a nice way to communicate with UI designers who can use custom
tags in the html without going into the details of how the code is implemented.
Another benefit of taglib is reusability of the code. This promotes writing code only
once and using is multiple times.
JSTL stands for JavaServer Pages Standard Tag Library. In JSTL, we have a
collection of JSP tags that can be used in different scenarios. There are following
main groups of tags in JSTL:
1. Core tags
2. SQL tags
3. Formatting tags
4. XML tags
5. JSTL Functions
31. How will you pass information from one JSP to another JSP?
We can pass information from one JSP to another by using implicit objects. If
different JSP are called in same session, we can use session object to pass
information from one JSP to another.
If we want to pass information from one JSP to another JSP included in the main
JSP, then we can use jsp:param to pass this information.
JSP allows running Java code from a .jsp file. We can call a stored procedure by
using JDBC code.
If we are using Spring framework, then we can use JdbcTemplate class to invoke
stored procedure from a JSP.
No, JSP specification does not allow overriding of _jspService method in JSP. We
can override other methods like jspInit() and jspDestroy().
JSP directive is a mechanism to pass message to JSP container. JSP directive does
not produce an output to the page. But it communicates with JSP container.
E.g. <%@include ..%> directive is used for telling JSP container to include the
content of another file during translation of JSP.
Some of the important directives in JSP are: page, include and taglib.
35. How will you implement Session tracking in JSP?
3. Session object: We can use the built in session object to track session in
JSP.
Like- www.abcserver.com?sessionid=1234
36. How do you debug code in JSP?
Another option in debugging is to link JSP container with an IDE. Once we link
IDE debugger to JSP Engine, we can use standard operations of debugging like
breakpoint, step through etc.
To implement an error-handling page in JSP, we first create a JSP with error page
handling information. In most of the cases we gracefully handle error by giving a
user-friendly message like “Sorry! There is system error. Please try again by
refreshing page.”
In this error page, we show user-friendly message to user, but we also log
important information like stack trace to our application log file.
Now we can use this error page in other JSP where we want to handle error. In case
of an error or exception, these JSP will direct it to errorPage.
In general, JSP is used to pass HTML data to web browser. If we want to send data
in XML format, we can easily do it by setting contentType=”text/xml” in page
directive.
When a user calls JSP page from web browser, the request first comes to web
server. Web server checks for .jsp extension of page and passes the request to JSP
container like Tomcat.
The JSP container checks whether it has precompiled JSP class or not. If this is the
first time this JSP is called, then JSP container will translate JSP into a servlet and
compiles it.
After compiling, JSP code if loaded in memory and JSP container will call jspInit()
method and _jspService() methods.
The _jspService() method will create the output that will be sent by JSP container
to client browser.
We can use setIntHeader() method to set the refresh frequency with which we want
to auto-refresh a JSP page.
We can send key “Refresh” with the time in seconds for auto refresh of the JSP
page.
E.g. response.setIntHeader(“Refresh”,10)
41. What are the important status codes in HTTP?
Every HTTP request comes back with a status code from the server.
The important status codes in HTTP are as follows:
In HTTP header, Accept attribute is used to specify the MIME types that a HTTP
client or browser can handle. MIME type is the identifier for specifying the type of
file/data that we are planning to pass over the internet.
43. What is the difference between Expression and Scriptlet in
JSP?
Scriptlet is for writing Java code in a JSP. We can define variable, methods etc in a
Scriptlet. A Scriptlet can handle much more complex code and can be also reused.
1. setMaxAge(): we can set the maximum age of a cookie. After this time
period, Cookie will expire and will be deleted.
First we create a Cookie object. We set the name and value of the cookie to be
created.
We set the expiry time of the Cookie by setting the maximum age.
We can use setMaxAge() method for this.
Finally, we can send the cookie in a HTTP Response by sending it in HTTP header.
In this way cookie goes to client browser and gets stored there till the maximum
age is not achieved.
Once a Cookie is set in the client browser, we can call getCookies() method to get
the list of all the cookies set in Client. We iterate through the list of all the cookies
and get the value of the cookie that was set in earlier request.
In this way we can use Cookie to set some information at client side and retrieve its
value.
46. What is the main difference between a Session and Cookie in JSP?
A Session is always stored at the Server side. In JSP, session is a built-in object in
JSP container.
We can use both the methods for Session tracking. But Cookie method needs
permission from user for storing cookie at the client location.
We can simply set the session attribute as false in page directive to prevent creation
of session object.
We can write output in JSP in such a way that it becomes a comment in HTML
code. This comment will not be visible in the web browser. But when we view page
source to see HTML, we can see output comment.
We can use set the header in response object for Cache-Control to specify no
caching.
Sample code is as follows:
response.setHeader(“Cache-Control”, “no-store”);
response.setDateHeader(“Expires”,”0”);
50. How will you redirect request to another page in browser in JSP
code?
We can use sendRedirect() method in JSP to redirect the request to another location
or page.
In this case the request will not come back to server. It will redirect in the browser
itself.
Sample code is as follows:
<% response.sendRedirect(URL); %>
51. What is the difference between sendRedirect and forward in a
JSP?
Both forward and sendRedirect are mechanisms of sending a client to another page.
The main difference between these two are as follows:
We use init-param to specify the parameters that are specific to a servlet or jsp.
This information is confined to the scope of that JSP.
We use context-param to specify the parameters for overall application scope. This
information does not change easily. It can be used by all the JSP/Servlet in that
Container.
54. What is the purpose of RequestDispatcher?
We have to first get the RequestDispatcher object from the container and then we
can call include or forward method on this object.
There is a built-in request object in a JSP that provides methods to read Form data.
Some of the methods are as follows::
JSP Engine loads all the filters in when we start the server.
57. How can you upload a large file in JSP?
To upload a file by JSP we can use <input type=”file”> in the Form data being
passed from HTML.
Once the request is received, we can implement the logic to read mulitpart data in
doPost() method of JSP. There are methods in JSP framework to read large files via
this method.
To initialize multiple JSP objects, we have to specify same Servlet object multiple
times in web.xml.
This indicates to JSP container to initialize separate JSP/Servlet object for each
element. Each of the Servlet instance will have its own ServletConfig object and
parameters.