10. what is the difference between , pagedirective include, action tag include ?

A JSP page can include page fragments from other files to form the complete response. You can use this, for instance, to keep header, footer, and navigation bar content in separate files and include them in all other pages. There are two include mechanisms: the include directive and the include action. It’s not always obvious which one to use, though.

The include directive, <%@ include file="filename.inc" %>, includes the content of the specified file during the translation phase–when the page is converted to a servlet. The main page and the included file are simply merged. This means that scripting variables declared in one file (using scripting elements or an action element like <jsp:useBean>) are visible in all files and must have unique names. Some containers detect changes in files included with the directive, but the specification doesn’t require it. Therefore, changes you make to the included file in a running system may not be reflected immediately; you may have to update the main JSP page, or remove the class file generated for the main page in order to see the change.

The include action, <jsp:include page="pagename.jsp" flush="true" />, includes the response generated by executing the specified page (a JSP page or a servlet) during the request processing phase–when the page is requested by a user. As opposed to the include directive, the page name can be specified as a so-called request-time attribute value, so which page to include can be decided when the main page is requested. Since it’s the response generated by the page that is included, not the content of the page itself, scripting variables declared in one file are not available to the other files. To share an object between the pages you must instead place it in one of the following JSP scopes: request, session or application scope. If you change a page included with the include action, the change always takes effect immediately.

My rule of thumb for when to use the different mechanisms is this:

  • Use the include directive if the file changes rarely. It’s the fastest mechanism. If your container doesn’t automatically detect changes, you can force the changes to take effect by deleting the main page class file.
  • Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.

9. Explain JSP Import and JSP Extends.

extends=”package.class”

The fully qualified name of the superclass of the Java class file this JSP file will be compiled to. Use this attribute cautiously, as it can limit the JSP container’s ability to provide a specialized superclass that improves the quality of the compiled file.

import=”{package.class | package.* }, …”

 A comma-separated list of Java packages that the JSP file should import. The packages (and their classes) are available to scriptlets, expressions, and declarations within the JSP file. If you want to import more than one package, you can specify a comma-separated list after import or you can use import more than once in a JSP file.

8. What is the diff. b/n declaring members in the JSP and declaraing in jspinit() method ?

Declaring a variable in JSP means it will have a scope of page while declaring it in jspInit() means its scope gets end with init() method only.

1. How do I prevent the output of my JSP or Servlet pages from being cached by the browser?

Appropriate HTTP header attributes have to be set to prevent the dynamic content output by the JSP page from being cached by the browser. The following jsp code has to be included at the beginning of your JSP pages to prevent caching :

Execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser:

<%

response.setHeader(“Pragma”,”no-cache”); //HTTP 1.0
response.setHeader(“Cache-Control”,”no-store”); //HTTP 1.1


%>

6. hy should we setContentType() in servlet ?what is the use of that method?

SetContentType() is used to intimate the browser that which type of content is coming as response,Depending upon the contentType browser treats with the response.

* ContentType is also called as MIME(MultipurposeInternetMailExtension)type

* Using this type we can set the response to the client or browser or useragent

* it will  give the response based upon the ContentType

* default MIME type is(“text/html”)

5. Can a single JSP page be considered as a J2EE application?  

A J2EE application contains J2EE modules, which could be web applications, EJBs and application clients. It also contains meta-information about the application as well as shared libraries. You can also say that a J2EE application is a set of J2EE modules with some added glue that binds them together into a complete integrated application. The shape of a J2EE application is a single Java Archive file with the .ear filename extension.

If a single JSP is bundled according to J2EE structure, then we can consider that one as a J2EE application.