jsp and servlets Interview Qns

JSP Interview Questions

What is JSP?

Java Server Pages technology (JSP) is a server-side programming language used to create a dynamic web page in the form of HyperText Markup Language (HTML). It is an extension to the servlet technology.
A JSP page is internally converted into the servlet. JSP has access to the entire family of the Java API including JDBC API to access enterprise database.

List out some advantages of using JSP.

  • Better performance.
  • The compilation of JSP is done before it is processed by the server which eradicates the need for loading of interpreter and code script each time.
  • JSP has access to all-powerful enterprises.
    Easy to maintain: JSP can be easily managed because we can easily separate our business logic with presentation logic. In Servlet technology, we mix our business logic with the presentation logic.
  • JSP can also be used in combination with servlets.

<%-- Comment --%>


JSP comment will not be shown whereas HTML comment will be displayed.

What are the JSP implicit objects?
JSP provides nine implicit objects by default. They are as follows:
Object
Type
1) out
JspWriter
2) request
HttpServletRequest
3) response
HttpServletResponse
4) config
ServletConfig
5) session
HttpSession
6) application
ServletContext
7) pageContext
PageContext
8) page
Object
9) exception
Throwable

Is JSP technology extensible?

Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?

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.

How to disable caching on the back button of the browser?

  1. <%  
  2. response.setHeader("Cache-Control","no-store");   
  3. response.setHeader("Pragma","no-cache");   
  4. response.setHeader ("Expires", "0"); //prevents caching at the proxy server  
  5. %>   


How can we handle the exceptions in JSP?

There are two ways to perform exception handling, one is by the errorPage element of page directive, and second is by the error-page element of the web.xml file.

What are the two ways to include the result of another page. ?

There are two ways to include the result of another page:

How can we forward the request from JSP page to the servlet?

Yes of course! With the help of "forward action" tag, but we need to give the URL-pattern of the servlet.

Can we use the exception implicit object in any JSP page?

No. The exception implicit object can only be used in the error page which defines it with the isErrorPage attribute of page directive.

How is JSP used in the MVC model?

JSP is usually used for presentation in the MVC pattern (Model View Controller ), i.e., it plays the role of the view. The controller deals with calling the model and the business classes which in turn get the data, and this data is then presented to the JSP for rendering on to the client.

What are context initialization parameters?

Context initialization parameters are specified by the <context-param> in the web.xml file, and these are initialization parameter for the whole application and not specific to any servlet or JSP.

What are the different scope values for the <jsp:useBean> tag?

There are 4 values:
  1. page
  2. request
  3. session
  4. application

What do JSP literals consist of?

  • Boolean
  • Integer
  • Floating point
  • String
  • Null

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.

What is the purpose of <jsp:setProperty >?

This action sets the properties of a bean.

What is the purpose of <jsp:getProperty >?

This action retrieves the properties of a bean.

List out the various scope values of JSP action.

The possible scope values are:
  • page
  • request
  • session
  • application

What is the use of 'out' implicit object?

The object is used to give a response to contents.

Give the use of session object.

The object is used between the client requests for the tracking of client sessions.

Give the use of exception object.

The object is used for the generation of a response to the errors thrown.

What is the difference between ServletContext and PageContext?-

ServletContext gives the information about the container whereas PageContext gives the information about the Request.

What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

request.getRequestDispatcher(path) is used to create it we need to give the relative path of the resource whereas context.getRequestDispatcher(path)to create it we need to give the absolute path of the resource.

What is EL in JSP?

The Expression Language(EL) is used in JSP to simplify the accessibility of objects. It provides many objects that can be used directly like param, requestScope, sessionScope, applicationScope, request, session, etc.

What are the primary differences between the JSP custom tags and java beans?

  • Custom tags can manipulate JSP content whereas beans cannot.
  • Complex operations can be reduced to a significantly simpler form with custom tags than with beans.
  • Custom tags require quite a bit more work to set up than do beans.
  • Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.

Can an interface be implemented in the JSP file?

No.

What is JSTL?

JSP Standard Tag Library is a library of predefined tags that ease the development of JSP.

How many tags are provided in JSTL?

There is 5 type of JSTL tags.
  1. core tags
  2. sql tags
  3. xml tags
  4. internationalization tags
  5. functions tags

Which directive is used in JSP custom tag?

The JSP taglib directive.

What are the three tags used in JSP bean development?

  1. jsp:useBean
  2. jsp:setProperty
  3. jsp:getProperty

How to disable session in JSP?

  1. <%@ page session="false" %>


List the various action tags used in JSP.

Following are the list of various action tags used in JSP:
  • 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 object.
  • 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.

Explain the steps for creating custom tags in JSP?

For creating any custom tag, we need to follow the following steps:
  1. Create the Tag handler class
    To generate the Tag Handler, we are inheriting the TagSupport class and overriding its method doStartTag().To write data for the JSP, we need to use the JspWriter class.
    The PageContext class provides getOut() method that returns the instance of JspWriter class. TagSupport class provides an instance of pageContext by default.
  2. Create the TLD file
    Tag Library Descriptor (TLD) file contains information of tag and Tag Hander classes. It must hold inside the WEB-INF directory.
  3. Create the JSP file
    Let's use the tag in our JSP file. Here, we are specifying the path of tld file directly. However, it is recommended to use the URI name instead of full a path of tld file. We will learn about URI later. It uses taglib directive to use the tags defined in the tld file.

How can the applets be displayed in the JSP?

The jsp:plugin action tag is used to embed an applet in the JSP file. The jsp:plugin action tag downloads plugin at client side to execute an applet or bean.

What is Expression language in JSP?

The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application.
There are many implicit objects, operators and reserve words in EL.
It is the newly added feature in JSP technology version 2.0.
Explain various implicit objects in expression language.
Implicit Objects
Usage
pageScope
it maps the given attribute name with the value set in the page scope
requestScope
it maps the given attribute name with the value set in the request scope
sessionScope
it maps the given attribute name with the value set in the session scope
applicationScope
it maps the given attribute name with the value set in the application scope
param
it maps the request parameter to the single value
paramValues
it maps the request parameter to an array of values
header
it maps the request header name to the single value
headerValues
it maps the request header name to an array of values
cookie
it maps the given cookie name to the cookie value
initParam
it maps the initialization parameter
pageContext
it provides access to many objects request, session, etc.



Servlet interview questions

How many objects of a servlet is created?

Only one object at the time of first request by servlet or web container.

What is the life-cycle of a servlet?

  1. Servlet is loaded
  2. servlet is instantiated
  3. servlet is initialized
  4. service the request
  5. servlet is destroyed

What are the life-cycle methods for a servlet?
Method
Description
public void init(ServletConfig config)
It is invoked only once when first request comes for the servlet. It is used to initialize the servlet.
public void service(ServletRequest request,ServletResponse)throws ServletException,IOException
It is invoked at each request.The service() method is used to service the request.
public void destroy()
It is invoked only once when servlet is unloaded.

Who is responsible to create the object of servlet?

The web container or servlet container.

When servlet object is created?

At the time of first request.

What is difference between PrintWriter and ServletOutputStream?

PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.

What is difference between GenericServlet and HttpServlet?

The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.

What is servlet collaboration?

When one servlet communicates to another servlet, it is known as servlet collaboration. There are many ways of servlet collaboration:
  • RequestDispacher interface
  • sendRedirect() method etc.
more details...

What is the purpose of RequestDispatcher Interface?

The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. This interceptor can also be used to include the content of antoher resource.
more details...

Can you call a jsp from the servlet?

Yes, one of the way is RequestDispatcher interface for example:
  1. RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");  
  2. rd.forward(request,response);  
more details...

Difference between forward() method and sendRedirect() method ?
forward() method
sendRedirect() method
1) forward() sends the same request to another resource.
1) sendRedirect() method sends new request always because it uses the URL bar of the browser.
2) forward() method works at server side.
2) sendRedirect() method works at client side.
3) forward() method works within the server only.
3) sendRedirect() method works within and outside the server.

What is difference between ServletConfig and ServletContext?

The container creates object of ServletConfig for each servlet whereas object of ServletContext is created for each web application.

What is Session Tracking?

Session simply means a particular interval of time.
Session Tracking is a way to maintain state of an user.Http protocol is a stateless protocol.Each time user requests to the server, server treats the request as the new request.So we need to maintain the state of an user to recognize to particular user.

What are Cookies?

A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

What is difference between Cookies and HttpSession?

Cookie works at client side whereas HttpSession works at server side.

What is filter?

A filter is an object that is invoked either at the preprocessing or postprocessing of a request. It is pluggable.

How can we perform any action at the time of deploying the project?

By the help of ServletContextListener interface.

What is the disadvantage of cookies?

It will not work if cookie is disabled from the browser.
more details...

How can we upload the file to the server using servlet?

One of the way is by MultipartRequest class provided by third party.

What is load-on-startup in servlet?

The load-on-startup element of servlet in web.xml is used to load the servlet at the time of deploying the project or server start. So it saves time for the response of first request.

What if we pass negative value in load-on-startup?

It will not affect the container, now servlet will be loaded at first request.

What is war file?

A war (web archive) file specifies the web elements. A servlet or jsp project can be converted into a war file. Moving one servlet project from one place to another will be fast as it is combined into a single file.

How to create war file?

The war file can be created using jar tool found in jdk/bin directory. If you are using Eclipse or Netbeans IDE, you can export your project as a war file.
To create war file from console, you can write following code.
  1. jar -cvf abc.war *  
Now all the files of current directory will be converted into abc.war file.

What are the annotations used in Servlet 3?

There are mainly 3 annotations used for the servlet.
  1. @WebServlet : for servlet class.
  2. @WebListener : for listener class.
  3. @WebFilter : for filter class.

Which event is fired at the time of project deployment and undeployment?

ServletContextEvent.
The ServletContextEvent is notified when web application is deployed on the server.

Which event is fired at the time of session creation and destroy?

HttpSessionEvent.

Which event is fired at the time of setting, getting or removing attribute from application scope?

ServletContextAttributeEvent.

What is the use of welcome-file-list?

It is used to specify the welcome file for the project.

What is the use of attribute in servlets?

Attribute is a map object that can be used to set, get or remove in request, session or application scope. It is mainly used to share information between one servlet to another.






















































No comments:

Post a Comment