Tuesday, February 7, 2017

JSP Interview Questions and Answers

In this page we will learn frequently asked interview questions for freshers and experienced developers. JSP is a server side technology,it can be used to prepare presentation/view part in the web application development. JSP pages can be deploy at any place in the web application directory structure. Now let us see frequently asking questions .

1) What is JSP?

Ans: JSP stands for Java Server Page,It is a server side technology and it is used to prepare presentation/view part in the web application development.The main purpose of JSP is for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags,most of which start with<% and end with %>.

2) What are advantages of using JSP?

Ans: JSP offer several advantages listed below:

  1. JSP is always compiled before it's processed by the server unlike.
  2. Performance is significantly better because JSP allows embedding Dynamic Elements in HTML pages itself.
  3. Java server pages are built on top of the Java Servlets API,so like Servlets,JSP also has access to all the powerful enterprise JAVA APIs including JDBC,JNDI,EJB,JAXP etc.
  4. JSP pages can be used in combination with servlets that handle the business logic,the model supported by Java servlet template engines.
3) Difference Between JSP & Servlets?
Ans:  
  • In JSP we can easily separate the presentation Logic with Business Logic but in servlets both are combined
  • One servlet object is communicated with many number of objects,but jsp it is not possible.
  • Internally when JSP is executed by the server it converted into servlet . Therefore we can say that JSP & servlets are work almost similar.
4) What are the web application scope?

Ans: There are 3 web application scopes. They are
1) Request
2)session
3)Application

5) What is JSP declarations?

Ans: A declaration declares one or more variables or methods that you can use in Java code later in JSP file. You must declare the variable or method before you use it in the JSP file
<%declaration; [declaration;]....%>

6) What are JSP expressions?

Ans: A JSP expression element contains a scripting language expression that is evaluated,converted to a string,and inserted where the expression appears in the JSP file. The expression element can contain any expression that is valid according to the java language specification but you can not use a semicolon to end an expression.

Syntax: <%=expression%>

7) What are JSP directives?

Ans: A JSP directive affects the overall structure of the servlet class. It usually has the following form: <%@ directive attribute="value" %>

8) What are the JSP actions?

Ans:  JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file,reuse Java Beans components,forward the user to another page,or generate HTML for the Java plugin.

Syntax: <JSP: action_name   attribute="value">

9) Give some of the JSP action names?

Ans:  There are some frequently used jsp actions. They are

jsp:include
jsp:forward
jsp:useBean
jsp:body
jsp:attribute


10) What is page directive?

Ans: The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code page directives anywhere in your JSP page.

11)what are Difference between <%@ include file="file" %> & <jsp:include page=”abc.jsp” %> ?

Ans: 
<%@include file="abc.jsp"%> directive acts like C "#include", pulling in the text of the included file and compiling it as if it were part of the including file. The included file can be any type (including HTML or text). (Or) includes a jsp/servlet at compile time meaning only once parsed by the compiler.
<jsp:include page="abc.jsp"> include a jsp/servlet at request time it is not parsed by the compiler.

12)What are Difference between res.sendRedirect( ) & req.forward( ) ?

Ans: 

sendRedirect() : It sends a redirect response back to the client's browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response. 
forward(): It does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different servlet/jsp than they originally called. 
Example: if you want to hide the fact that you're handling the browser request with multiple servlets/jsp, and all of the servlets/jsp are in the same web application, use forward() or include(). If you want the browser to initiate a new request to a different servlet/jsp, or if the servlet/jsp you want to forward to is not in the same web application, use sendRedirect (). 

13) How does JSP handle runtime exceptions?

Ans: 
You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. 
For example:
<%@ page errorPage=\"error.jsp\" %> 
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: <%@ page isErrorPage=\"true\" %>.

14)How do I prevent the output of my JSP or Servlet pages from being cached by the Web browser? And Proxy server?

Ans:
Web browser caching:
<% response.setHeader("Cache-Control","no-store"); 
   response.setHeader("Pragma","no-cache"); 
   response.setDateHeader ("Expires", 0); 
 %>
   
Proxy server caching:
response.setHeader("Cache-Control","private");

15)What's a better approach for enabling thread-safe servlets & JSPs? SingleThreadModel Interface or Synchronization?

Ans:
SingleThreadModel technique is easy to use, and works well for low volume sites. If your users to increase in the future, you may be better off implementing explicit synchronization for your shared data Also, note that SingleThreadModel is pretty resource intensive from the server's perspective. The most serious issue however is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the unserviced requests are queued until something becomes free.

16) What are the various attributes of page directive?

Ans: The following are the page attributes:

  • language
  • extends
  • import
  • session
  • isThreadSafe
  • errorPage
  • contentType
  • autoFlush
17) What is include directive?

Ans: The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. 

Syntax: <%@ include file="relative url">

18) What is taglib directive?

Ans: The tablib directive follows the following 

Syntax: <%@ tablib uri="uri" prefix="prefixOfTag">

In the above uri attribute value resolves to a location the container understands.
prefix attribute informs a container what bits of markup are custom actions.

19)What implicit objects are supported by JSP?

Ans: request,response,out,session,application,exception

20) What are difference between JspWriter and PrintWriter?

Ans:  The JspWriter object contains most of the same methods as the java.io.PrintWriter class. However,JspWriter has some additional methods designed to deal with buffering. Unlike the PrintWriter object,JspWriter throws IOExceptions.

21) What is session Object?

Ans: The session object is an instance of javax.servlet.http.HttpSession and is used to track client session between client requests.

22) What are difference between GET and POST method in HTTP protocol?

Ans: 
The GET method sends the encoded user information appended to the page request.The page and the encoded information are separated by the ? character.

The  POST method packages the information in exactly the same way as GET methods,but instead of sending it as text string after a ? in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing.

23) How to read data form data using JSP?

Ans: By using getParameter(),getParameterValues(),getParameterName(),getInputStream()

getParameter(): You can call request.getParameter() method to get the value of a form parameter.

getParameterValues(): Use this method to call if the parameter appears more than once and returns multiple values. Example: Checkbox

getParameterName(): This method is used if you want a complete list of all parameters in the current request.

24) Define Filters?

Ans: JSP Filters are java classes that can be used in JSP programming for the following purpose:
  • To manipulate responses from server before they are sent back to the client
  • To intercept requests from a client before they access a resource at back end.
25)  What are cookies?

Ans: Cookies are text files stored on the client computer and they are kept for various information tracking purpose.

26)How do set Cookies in the JSP page?

Ans:  There are three steps to set cookies in JSP

Creating a cookie object
Setting the maximum age
Sending the Cookie into the HTTP response Headers

27) What is auto refresh feature in JSP?

Ans: Consider a webpage which is displaying live game score or stock market status or currency exchange ration. For all such types of pages,you would need to refresh your web page regularly using refresh or reload button with your browser.

Here jsp makes this job easy by providing you a mechanism where you can make a webpage in such a way that it would refresh automatically after a given interval.

Example:  response.setIntHeader("Refresh","5 ; URL=http://www.cricbuzz.com")

28) What is JSTL?

Ans: The Java Server Pages Standard Tag Library(JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. It supports for common,structural tasks such as iteration and conditionals,tags for manipulating XML documents and SQL tags.

29) How do you pass control from one JSP page to another?

Ans:

The following are the ways to pass control of a request from one servlet to another or one jsp to another: They are

  • Using the response.sendRedirect() method
  • Using RequestDispatcher object's forward method to the pass the control
30) What are the difference between Java Beans and taglib directives?

Ans:
Java Beans and taglib fundamentals were introduced for reusability. The following are the major differences:
  1. Taglibs are for generating presentation elements while Java Beans are good for storing information and state
  2. Use custom tags to implement actions and Java Beans to present information





No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...