Jsp Questions


Jsp Interview Questions :-

  What is JSP and tell its uses?

JSP stands for Java Server Pages. It is a presentation layer technology independent of platform. It comes with SUN's J2EE platforms. They are like HTML pages but with Java code pieces embedded in them. They are saved with a .jsp extension. They are compiled using JSP compiler in the background and generate a Servlet from the page.


  Explain Implicit objects in JSP.

Objects created by web container and contain information regarding a particular request, application or page are called Implicit Objects. They are :

1. response

2. exception

3. application

4. request

5. session

6. page

7. out

8. config

9. pageContext


  Is JSP technology extensible?

Yes, JSP is easily extensible by use and modification of tags, or custom actions, encapsulated in tag libraries.


  How is JSP better than Servlet technology?

JSP is a technology on the server's side to make content generation simple. They are document centric, whereas servlets are programs. A Java server page can contain fragments of Java program, which execute and instantiate Java classes. However, they occur inside HTML template file. It provides the framework for development of a Web Application.


  How to disable scripting?

Scripting can be easily disabled by setting scripting-invalid element of the deployment descriptor to true. It is a sub-element of property group. Its can be false as well.


  Explain the <jsp:param> action.

It is an action used with include or forward standard actions. It helps in passing the parameter names and values to a resource.


  Explain static method.

A static method is of the class and not the object of a class. It can be invoked without instance of a class. Static members can also access the static data and change its value.


  Explain include Directive and include Action of JSP

include Directive :- Syntax for include Directive is <%@include file="fileName"%>which means we are including some file to our JSP Page when we use include directive contents of included file will be added to calling JSP page at translation time means when the calling JSP is converted to servlet ,all the contents are added to that page .one important thing is that any JSP page is complied if we make any changes to that particular page but if we have changed the included file or JSP page the main calling JSP page will not execute again so the output will not be according to our expectation, this one is the main disadvantage of using the include directive that why it is mostly use to add static resources, like Header and footer .

include Action :- Syntax for include Action is <jsp:include page="relativeURL"/> it's a runtime procedure means the result of the JSP page which is mentioned in relative URL is appended to calling JSP at runtime on their response object at the location where we have used this tag

So any changes made to included page is being effected every time, this is the main advantage of this action but only relative URL we can use here ,because request and response object is passed between calling JSP and included JSP.


  In JSP page how can we handle runtime exception?

We can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page.

Example :- <%@ page errorPage="error.jsp" %>

It will redirect the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, will have to indicate that it is an error-processing page, using the directive.

Example :- <%@ page isErrorPage="true" %>


  How can you avoid scriptlet code in JSP?

JavaBeans or Custom Tags can be used instead of scriptlet code.


  Explain the jspDestroy() method.

Whenever a JSP page is about to be destroyed, the container invokes the jspDestroy() method from the javax.servlet.jsp.JspPage interface. Servlets destroy methods are similar to it. It can be easily overridden to perform cleanup, like when closing a database connection.


  Explain the <jsp:param> action.

It is an action used with include or forward standard actions. It helps in passing the parameter names and values to a resource.


  What is the need of tag library?

Tag library is a collection of custom tags. Custom actions helps recurring tasks will be handled more easily they can be reused across more than one application and increase productivity.

JSP tag libraries are used by Web application designers who can focus on presentation issues rather than being concerned with how to access databases and other enterprise services. Some of the popular tag libraries are Apache display tag library and String tag library.


  How can you pass information form one jsp to included jsp.

Using <Jsp: param> tag we can pass parameter from main jsp to included jsp page.

For Example :-

<jsp:include page="newbid.jsp" flush="true">

<jsp:param name="price" value="123.7"/>

<jsp:param name="quantity" value="4"/>


  Why is _jspService() method starting with an '_' while other life cycle methods do not?

JSP life cycle method are :-

jspInit(), jspDestroy() and _jspService(), bydefault whatever content we write in our jsp page will go inside the _jspService() method by the container if again will try to override this method. JSP compiler will give error but we can override other two life cycle method as we have implementing this two in jsp so making this difference container use _ in jspService() method and shows that we cant override this method.


  Explain client and server side validation.

JavaScript is used for the client-side validation. It takes place within the browser. JavaScript is used to submit the form data if validation is successful. Validation errors require no extra network trip because form cannot be submitted.

Validation is also carried out in the server after submission. If validation fails, extra network trip is required to resend the form to the client.


  What is Translation Phase?

JSP engine translates and compiles a JSP file to a servlet. This servlet moves to the execution phase where requests and responses are handled. They are compiled for the first time they are accessed unless manually compiled ahead of time. The manual or explicit compilation is useful for long and convoluted programs.


  What is the Static Block?

It is used to start the static data member. It is executed before classloading.


  What are standard actions in JSP?

They affect overall runtime behaviour of a page and response sent to the client. They are used to include a file at request time, to instantiate a JavaBean or find one. They are also used to generate a browser-specific code or forward a request to a new page.


  What are attributes of page directives?

1. Session :- It shows if a session data is available to the page.

2. Import :- it shows packages that are imported.

3. isELIgnored :- It shows whether EL expressions are ignored when JSP translates into a Servlet.

4. contentType :- it allows the user to specify the content type of page.


  Explain the various scope values for <jsp:useBean> tag.

<jsp:useBean> tag is used to use any java object in the jsp page. Some scope values are :-

1. application

2. request

3. page

4. session


  What is the meaning of Composition?

Composition has a stronger relationship with the object than Aggregation.


  Can constructor be used instead of init(), to initialize servlet?

Yes, it is possible. But it is not preferred because init() was developed because earlier Java versions could not invoke constructors with arguments dynamically. So they could not assign a servletConfig. Today, however, servlet containers still call only no-arg constructor. So there is no access to servletContext or servletConfig.


  How to restrict page errors display in a JSP page?

By setting up an "ErrorPage" attribute of PAGE directory to the name of the error page in the JSP page, and then in the error jsp page set "isErrorpage="TRUE", Errors can be stopped from getting displayed.


  Which implicit object is not available in normal JSP pages?

JSP exception implicit object is not available in normal JSP pages and it's used in JSP error pages only to catch the exception thrown by the JSP pages and provide useful message to the client.


  What are the benefits of PageContext implicit object?

JSP pageContext implicit object is instance of javax.servlet.jsp.PageContext abstract class implementation. We can use pageContext to get and set attributes with different scopes and to forward request to other resources. pageContext object also hold reference to other implicit object.

This is the only object that is common in both JSP implicit objects and in JSP EL implicit objects.


  How to use JSP EL to get HTTP method name?

We can use pageContext JSP EL implicit object to get the request object reference and use dot operator to get the HTTP method name in JSP page. The JSP EL code for this will be GET.


  What are the types of JSTL tags?

There are five categorized into JSTL functions :-

1. Core Tags :- Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc.

2. Formatting and Localization Tags :- These tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles.

3. SQL Tags :- JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc.

4. XML Tags :- XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation.

5. JSTL Functions Tags :- JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc.


  What is JSP Custom Tag and what are it's components?

Sometimes JSP EL, Action Tags and JSTL tags are not enough and we might get tempted to write java code to perform some operations in JSP page. Fortunately JSP is extendable and we can create our own custom tags to perform certain operations.

We can create JSP Custom Tags with following components :-

(a). JSP Custom Tag Handler.

(b). Creating Tag Library Descriptor (TLD) File.

(c). Deployment Descriptor Configuration for TLD.

We can add custom tag library in JSP page using taglib directive and then use it.


  What is difference between JspWriter and Servlet PrintWriter?

Difference between JspWriter and Servlet PrintWriter :-

PrintWriter :-

1. Does not support buffering.

2. Does not use BufferWriter internally.

3. Can create class of java.io.* package.

4. Its print (-) method does not throw IOException.

5. It is useful for servlet programming.

JspWriter :-

1. Supports buffering.

2. Uses BufferWriter, PrintWriter internally.

3. Abstract class of javax.servlet.jsp package.

4. Its print (-) method throw IOException.

5. It is the type of implicit object out in JSP


  Why is it that JComponent have add() and remove() methods but Component doesn't?

JComponent is a subclass of Container. It contains other Components and JComponents.


  How can a thread safe JSP page be implemented?

It can be done by having them implemented by the SingleThreadModel Interface. Add <%@page isThreadSafe="false" %> directive in the JSP page.


  How can the output of JSP or servlet page be prevented from being cached by the browser?

Using appropriate HTTP header attributes to prevent the dynamic content output by a JSP page from being cached by the browser.


  What is the Expression in JSP?

Expression tag is used to insert Java values directly in the output. Its syntax is <%=expression%>.

It contains a scripting language expression that is evaluated, then converted to a string, and then inserted where the expression comes in JSP file


  Explain the jsp:setProperty action.

It is used to give values to properties of beans that have been referenced beforehand.

<jsp:useBean id="ABC"./>

............

<jsp:setProperty name="ABC" property="myProperty"...

jsp:setproperty is executed even if a new bean is instantiated or existing bean is found.

By adding </jsp.useBean> at the end of the code, the condition for execution is inverted i.e. It is not executed if existing object was found and only if a new object was instantiated.


  What is the jsp:plugin action in JSP?

This action helps in insertion of a specific object in the browser or embed the element needed to specify the running of applet using Java plugin.


  What are custom tags and why it is needed?

JSP tags are extended by creating a custom set of tags which is called as tag library (taglib). The page which uses custom tags declares taglib and uniquely names, defines and associates a tag prefix to differentiate the usage of those tags.


  How cookies is deleted in JSP?

There are two ways by which the cookies can be deleted in JSP. Firstly, by setting the setMaxAge() of the cookie class to zero. And secondly by setting a timer in the header file that is response. setHeader(expires {Mention the time} attribute), which will delete the cookies after that prescribed time.


  What is a Hidden Comment?

A comment that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags. A hidden comment is not sent to the client, either in the displayed JSP page or the HTML page source. The hidden comment is useful when you want to hide part of your JSP page.


  How JSP calls a stored procedure?

Java Server Pages possess all the characteristics of java and to call and have similar syntax to call a function. Functions and stored procedures of a database can be called by using the statement callable. Another way to call the stored procedure is by writing the JDBC code in between the tag of scriptlet tab.write.


  Explain the difference between RequestDispatcher and sendRedirect? How JSP handles runtime exceptions?

1. RequestDispatcher redirects the server-side by requesting and responding the objects. But sendRedirect redirects the Client-side with new request and responding the objects.

2. JSP handles runtime exceptions by using the errorPage attribute of the page directive.

3. By specifying ErrorPage = true the current page intended URL is redirected to the JSP.


  Explain the difference between ServletContext and PageContext. How to create request.getRequestDispatcher () and context.getRequestDispatcher()?

1. The ServletContext :- Provides the information about the container.

2. PageContext :- Provides the information about the Request.

3. To create request.getRequestDispatcher(path) we have to provide the relative path of the resource. But to create context.getRequestDispatcher (path) we have to provide the absolute path of the resource.


  Explain the differences between <jsp:forward page = ... > and response.sendRedirect(url).

1. The <jsp:forward page = ... > element propagates the request object containing the client request information from one JSP file to another file.

2. The target file might be an HTML file, another JSP file, or any servlet, but it must be in the same application context.response.sendRedirect(url).

3. The sendRedirect helps to send HTTP temporary redirect response to the browser, and the browser helps to create a new request for going to the redirected page

4. The response.sendRedirect helps to kill the session variables.


  Why to use the HttpServlet Init method to perform expensive operations that need only be done once?

Because the servlet init() method is invoked when servlet instance is loaded, it is the perfect location to carry out expensive operations that need only be performed during initialization. By definition, the init() method is thread-safe.

The results of operations in the HttpServlet.init() method can be cached safely in servlet instance variables, which become read-only in the servlet service method.


  Why is it not a good practice to create HttpSessions in JSPs by default?

By default, JSP files create HttpSessions. This is in compliance with J2EETM to facilitate the use of JSP implicit objects, which can be referenced in JSP source and tags without explicit declaration. HttpSession is one of those objects.

If you do not use HttpSession in your JSP files then you can save some performance overhead with the following JSP page directive :-

<%@ page session="false"%>


  How can one Jsp Communicate with Java file.

We have import tag <%@ page import="market.stock.*" %> like this we can import all the java file to our jsp and use them as a regular class another way is servlet can send the instance of the java class to our jsp and we can retrieve that object from the request obj and use it in our page.


PHP

PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language..

Read more