Top 100 Advanced Java Interview Questions & Answers

1) What is JSP?
Answer:

JSP is the abbreviation for Java Server Pages which is used in developing web applications. It is used in creating dynamic web content. We can write the java code into the html pages with the use of JSP.

It is an extension to the servlet as it provides more features such as JSTL, expression language,etc.

It is mainly used to develop the presentation layer.

2) What are the implicit objects in JSP?
Answer:

There are a total of nine implicit objects in jsp.These are built – in objects created during the translation phase of jsp to servlet.These objects are created by the web container automatically and can be used directly in the scriptlets.

a. request – This object is an instance of HttpServletRequest and is created by the container for each request.

b. response– This object is an instance of HttpServletResponse and is associated with the response that is been sent to the client.The response object is used to set the content type and to redirect to the response page.

c. out–It is used to write the data to the buffer and send content in the response.It ia an instance of PrintWriter which contains print() and println() methods.

d. session–This is an instance of HttpSession and it is used to setthe object’s data in the session and get the users data from the session as well as it remove the attributes from session scope. It is mainly used to get the user’s information until the session is active.

e. application–It is an instance of ServletContext object and is created only once by the container when application is deployed to the server. It is mainly used to get the initialization parameters from the web.xml. It is also used to share the attributes and its values across all the JSP pages.

f. config–This is an implicit object of ServletConfig and it is used to get the initialization parameters of a particular JSP page. It is created by the web container specific to each jsp page.

g. pageContext–This is an implicit object of PageContext and is used to set,get and remove the attributes. It contains information of the directives , error page URL , etc. It has 4 scopes which are page, request, session and application scopes.

h. page–It refers to the current instance of the servlet class.

i. exception–This is used in exception handling process to display the error messages.This object is applicable only to the JSP page where the attribute “isErrorPage” is true.


3) Explain life cycle methods of JSP.
Answer:

The following are the lifecycle methods of the jsp as shown in the diagram.

 

  first step is the translation phase where the jsp is converted into Servlet.

The JSP engines compiles the servlet into the executable bytecode format(.class).

The class is loaded into the web container to process the request.

Creating a servlet instance.

The For every user request, init method is invoked where the initializations are performed such as database initialization, opening files,etc.It is called only once.

Once the JSP is initialized, it calls the service method where request is processed and response is sent to the client.

void _jspService(HttpServletRequest req,HttpServletResponse resp){

        //business logic 

}

Destroy method is invoked only once to destroy the object and perform cleanup activities.


cover image
4) Difference between include directive and include action tag.
Answer:

include directive includes the file during translation phase whereas include action includes the file during runtime.

If the included file is modified but the file which is including it is not changed, the changes will reflect only with the include action tag. While using include directive , the changes will not reflect as the jsp which included it is not changed and hence it is not translated to servlet.

Syntax of include directive:

<%@ include file="file_name" %>

Syntax of include action tag:


We can pass the parameters to the included page while using include action tag but this wont work with include directive.


5) Explain the translation phase of JSP.
Answer:

When a request is mapped to a particular jsp page, it is handled by a special servlet which checks if the servlet is older than the jsp page.If it is, then the jsp file is converted into a servlet class during translation phase.During this phase, container validates the syntax and correctness of the jsp files and tags.

The servlet class is then compiled into an executable byte code (.class file) which is loaded into the web container automatically.