JSP Java Interview Questions – Set 04

What are the life-cycle methods of JSP

Life-cycle methods of the JSP are:

  1. jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance.
  2. _jspService(): The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann’t be overridden.
  3. jspDestroy(): The container calls this when its instance is about to destroyed.
    The jspInit() and jspDestroy() methods can be overridden within a JSP page.

What are implicit Objects available to the JSP Page

Implicit objects are the objects available to the JSP page. These objects are created by Web container and contain information related to a particular request, page, or application.

The JSP implicit objects are:

Application, config, exception, out, page, pageContext, request, response andsession.

How can you prevent the Browser from Caching data of the Pages you visit

By setting properties that prevent caching in your JSP Page.They are:

<%

response.setHeader(“pragma”,”no-cache”);//HTTP 1.1

response.setHeader(“Cache-Control”,”no-cache”);

response.setHeader(“Cache-Control”,”no-store”);

response.addDateHeader(“Expires”, -1);

response.setDateHeader(“max-age”, 0);

//response.setIntHeader(“Expires”, -1);//prevents caching at the proxy server

response.addHeader(“cache-Control”, “private”);

%>

Why are JSP pages the preferred API for creating a web-based client program

Because no plug-ins or security policy files are needed on the client systems(applet does). Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.

What is difference between custom JSP tags and beans

Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components:

  • the tag handler class that defines the tag’s behavior
  • the tag library descriptor file that maps the XML element names to the tag implementations
  • the JSP file that uses the tag library

When the first two components are done, you can use the tag by using taglib directive:

<%@ taglib uri=”xxx.tld” prefix=”…” %>

Then you are ready to use the tags you defined. Let’s say the tag prefix is test:

MyJSPTag or

JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags to declare a bean and use to set value of the bean class and use to get value of the bean class.

<%=identifier.getclassField() %>

Custom tags and beans accomplish the same goals — encapsulating complex behavior into simple and accessible forms. There are several differences:

Custom tags can manipulate JSP content; 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 usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.

What is JSP declaration

A JSP scripting element that declares methods, variables, or both in a JSP page.

What is JSP page

A text-based document containing static text and JSP elements that describes how to process a request to create a response. A JSP page is translated into and handles requests as a servlet.

A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.

Advantages of JSP over Servlet.

  • Efficient: With traditional CGI, a new process is started for each HTTP request. If the CGI program does a relatively fast operation, the overhead of starting the process can dominate the execution time. With servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread, not a heavyweight operating system process. Similarly, in traditional CGI, if there are N simultaneous request to the same CGI program, then the code for the CGI program is loaded into memory N times. With servlets, however, there are N threads but only a single copy of the servlet class. Servlets also have more alternatives than do regular CGI programs for optimizations such as caching previous computations, keeping database connections open, and the like.
  • Convenient: Hey, you already know Java. Why learn Perl too? Besides the convenience of being able to use a familiar language, servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such utilities.
  • Powerful: Java servlets let you easily do several things that are difficult or impossible with regular CGI. For one thing, servlets can talk directly to the Web server (regular CGI programs can’t). This simplifies operations that need to look up images and other data stored in standard places. Servlets can also share data among each other, making useful things like database connection pools easy to implement. They can also maintain information from request to request, simplifying things like session tracking and caching of previous computations.
  • Portable: Servlets are written in Java and follow a well-standardized API. Consequently, servlets written for, say I-Planet Enterprise Server can run virtually unchanged on Apache, Microsoft IIS, or WebStar. Servlets are supported directly or via a plugin on almost every major Web server.
  • Inexpensive: There are a number of free or very inexpensive Web servers available that are good for “personal” use or low-volume Web sites. However, with the major exception of Apache, which is free, most commercial-quality Web servers are relatively expensive. Nevertheless, once you have a Web server, no matter the cost of that server, adding servlet support to it (if it doesn’t come preconfigured to support servlets) is generally free or cheap.

How to pass information from JSP to included JSP

Using <%jsp:param> tag.

How do you connect to the database from JSP

A Connection to a database can be established from a jsp page by writing the code to establish a connection using a jsp scriptlets.

Further then you can use the resultset object “res” to read data in the following way.