Sunteți pe pagina 1din 4

What is JSP and why do we need it?

JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web
pages. JSP is extension of Servlet technology to help developers create dynamic pages with
HTML like syntax.

We can create user views in servlet also but the code will become very ugly and error prone.
Also most of the elements in web page is static, so JSP page is more suitable for web pages. We
should avoid business logic in JSP pages and try to use it only for view purpose. JSP scripting
elements can be used for writing java code in JSP pages but its best to avoid them and use JSP
action elements, JSTL tags or custom tags to achieve the same functionalities.

One more benefit of JSP is that most of the containers support hot deployment of JSP pages.
Just make the required changes in the JSP page and replace the old page with the updated jsp
page in deployment directory and container will load the new JSP page. We dont need to
compile our project code or restart server whereas if we make change in servlet code, we need
to build the complete project again and deploy it. Although most of the containers now provide
hot deployment support for applications but still its more work that JSP pages.

What are the JSP lifecycle phases?


If you will look into JSP page code, it looks like HTML and doesnt look anything like
java classes. Actually JSP container takes care of translating the JSP pages and
create the servlet class that is used in web application. JSP lifecycle phases are:

1.Translation JSP container checks the JSP page code and parse it to
generate the servlet source code. For example in Tomcat you will find
generated servlet class files
atTOMCAT/work/Catalina/localhost/WEBAPP/org/apache/jsp directory. If
the JSP page name is home.jsp, usually the generated servlet class name is
home_jsp and file name is home_jsp.java
2.Compilation JSP container compiles the jsp class source code and produce
class file in this phase.
3.Class Loading Container loads the class into memory in this phase.
4.Instantiation Container invokes the no-args constructor of generated class
to load it into memory and instantiate it.
5.Initialization Container invokes the init method of JSP class object and
initializes the servlet config with init params configured in deployment
descriptor. After this phase, JSP is ready to handle client requests. Usually from
translation to initialization of JSP happens when first request for JSP comes but
we can configure it to be loaded and initialized at the time of deployment like
servlets using load-on-startup element.
6.Request Processing This is the longest lifecycle of JSP page and JSP page
processes the client requests. The processing is multi-threaded and similar to
servlets and for every request a new thread is spawned and ServletRequest and
ServletResponse object is created and JSP service method is invoked.
7.Destroy This is the last phase of JSP lifecycle where JSP class is unloaded
from memory. Usually it happens when application is undeployed or the server
is shut down.
1.What are JSP lifecycle methods?
JSP lifecycle methods are:

8.jspInit(): This method is declared in JspPage and its implemented by JSP


container implementations. This method is called once in the JSP lifecycle to
initialize it with config params configured in deployment descriptor. We can
override this method using JSP declaration scripting element to initialize any
resources that we want to use in JSP page.
9._jspService(): This is the JSP method that gets invoked by JSP container for
each client request by passing request and response object. Notice that method
name starts with underscore to distinguish it from other lifecycle methods
because we cant override this method. All the JSP code goes inside this method
and its overridden by default. We should not try to override it using JSP
declaration scripting element. This method is defined in HttpJspPage interface.
10.jspDestroy(): This method is called by container when JSP is unloaded from
memory such as shutting down application or container. This method is called
only once in JSP lifecycle and we should override this method to release any
resources created in JSP init method.
1.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.

What are JSP implicit objects?


JSP implicit objects are created by container while translating JSP page to Servlet source to
help developers. We can use these objects directly in scriptlets that goes in service method,
however we cant use them in JSP Declaration because that code will go at class level.
We have 9 implicit objects that we can directly use in JSP page. Seven of them are declared as
local variable at the start of _jspService() method whereas two of them are part
of _jspService() method argument that we can use.
1.out Object
2.request Object
3.response Object
4.config Object
5.application Object
6.session Object
7.pageContext Object
8.page Object
9.exception Object
10.JSP Implicit Objects Example

Can we use JavaScript with JSP Pages?


Yes why not, I have seen some developers getting confused with this. Even though JSP is a
server side technology, its used to generate client side response and we can add javascript or
CSS code like any other HTML page.

What is difference between JspWriter and Servlet


PrintWriter?
PrintWriter is the actual object responsible for writing the content in response. JspWriter uses
the PrintWriter object behind the scene and provide buffer support. When the buffer is full or
flushed, JspWriter uses the PrintWriter object to write the content into response.

what are the implicit Object

Ans: This is a fact based interview question what it checks is how much coding you do in JSP if you are doing
it frequently you definitely know them. Implicit object are the object that are created by web container
provides to a developer to access them in their program using JavaBeans and Servlets. These objects are
called implicit objects because they are automatically instantiated.they are bydefault available in JSP page.
They are: request, response, pageContext, session, and application, out, config, page, and exception.

S-ar putea să vă placă și