Interview Questions On Java,Java EE

Enter your email address:

Add to Technorati Favorites
Google PageRank Checker
Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Friday, June 1, 2007

How can you prevent caching in JSP?

Execute the following scriptlet at the beginning of your JSP pages  to prevent JSPs from being cached by browsers.You need both the statements to  take care of some of the older browser versions.

response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server

 If the above fails, try changing the first line to

response.setHeader("Cache-Control","no-store"); //HTTP 1.1
Continue reading...

How will you handle runtime exceptions in your JSPs?

JSP runtime exceptions are handled through the implicit object named "exception". This exception object can be used in a special type of JSP page called an error page, where you display the exception's name and class, its stack trace, and an informative message for your user.
In any JSP which is made eligible for catching exceptions then it must have a mention to error page in @page directive,something like:

<%@ page errorPage="ErrorHandling.jsp" %>

The error page, in this case ' ErrorHandling.jsp', has 'isErrorPage' attribute in page directive which should be set to true. When a JSP page has been declared an errorPage, it is made available an object with name of "exception" of type java.lang.Throwable.
Continue reading...

How can I show various kind of documents to a web client from JSP's and/or servlets?

You can show MS Word, Excel or PDF or any other type of document through your JSP or servlet by using :

response.setContentType("mime/type");   and

in jsp directive 

<%@ page contentType="mime/type" %>.

In case of MS Word documents contentType is "application/msword"
In case of Excel it is "application/"
In case of PDF it is "application/pdf"
Continue reading...

Does JSP support XML pages creation?

The answer is Yes. There are two ways you could use XML data in a JSP page:
• Convert the XML elements into server-side objects and then extract the object properties.
• Invoke a transformation on the XML data.

The static XML tags may be included as static template portions of the JSP page.The dynamic generation of XML tags occur through bean components or custom tags that generate XML output.The other mechanism of dynamic XML data use in JSP could be by XML transformation.This technique uses XSLT(a W3C standard for transforming an XML to XML/HTML/PDF) which could be used either for formatting data in a desired fashion or simply extracting the data from an XML.
Continue reading...

Explain different constituents of JSP like comments,expressions,declarations,scriptlets.

JSP Comments:
The JSP supports two type of comments :-
JSP Output Comments are those ones which can be viewed on the HTML file, while Hidden Comments are never shown.

JSP Expressions:
The JSP expression tag is inserts Java values directly into the output. The syntax of the Expression tag is:
<%= expression %>

This kind of tag contains is evaluated, converted to a String, and inserted where the expression appears in the JSP file.

JSP Declarations:
JSP declarations are used to declare variables.To add a declaration, the <%! and %> sequences is used to enclose declarations,and ends in semi-colon. These variables can be used within functions and expressions.It is not a good practice to declare variables in global space as such variable are shared by multiple threads and which may change it's values in an inconsistent manner.To avoid such a scenario one must make these variables as synchronized but then it hampers the performance of the JSP .Usually variables are declared within local scope of Java scriptlets.

JSP Scriptlets:
JSP allows to write block of Java code within scriptlet tags which are <% some Java code goes here %>.It does not contribute anything to HTML page and Java code is evaluated each time when JSP loads.

Here is an example which shows use of JSP Comments,Expression,Declaration and Scriptlet:

Continue reading...

Explain the life-cycle methods of JSP

JSP has following three life cycle methods:

a) jspInit(): It is the very first method which is called by JSP container to initialize the servlet instance.All JSPs implement the javax.servlet.jsp.JspPage interface that has two methods: jspInit and jspDestroy. It is important to implement when one has to load some database driver for example.

b)_jspService(): The JSP container calls the _jspservice() for each request and it passes the request and the response objects.The _jspService() method corresponds to the body of the JSP page. This method is defined automatically by the JSP container and should never be defined by the JSP page author. That is why _jspService() method can not be overridden.

c) jspDestroy(): When an instance of JSP is going to be destroyed then JSP container calls this method .It is used for cleaning up Both jspInit() and jspDestroy() methods can be overridden within a JSP page.This method helps in reclaiming resources like network and database connections when a JSP instance is destroyed and JSP no longer services any client request.
package javax.servlet.jsp;

public interface JspPage extends javax.servlet.Servlet {

public void jspInit();
public void jspDestroy();
public void _jspService(ServletRequestSubtype request,
ServletResponseSubtype response)
throws ServletException, IOException;
// _jspService - depends on the specific protocol used and
// cannot be expressed in a generic way in Java.
Continue reading...

Thursday, April 19, 2007

Interview Questions on JSP

Continue reading...

Wednesday, April 18, 2007

Java Servlets and JSPs

Continue reading...

Tuesday, April 17, 2007

What are implicit objects in JSP?

The objects which are created by web container and are available in a JSP page for various information extractions from there objects.The different implicit objects in JSPs are(click on image to enlarge):

Continue reading...

What is the difference between ' include file = ' and 'jsp:include page = ...'

<@ include file = ...>: The content of included file is textually embedded in the page that has <@ include file=".."> directive.It occurs at compile time. If included file changes, the changed content will not be included in the output. This approach is used in template creations where code from one jsp file is included in multiple jsp files.
<jsp:include page="..">: It is like a function call from one jsp to another jsp. The inclusion of one jsp content to another happens at execution time. This approach is handy for web application modularization.If included file changes then the new contents will be included in the output.
Continue reading...

What is the difference between 'jsp:forward' and HttpResponse.sendRedirect()?

is processed at the server end for forwarding a client request to a specified URL while HttpResponse.sendRedirect() method is used at the client end for request redirection.
Continue reading...

What is the difference between page directive 'include' and 'jsp: include' action command?

JSP directive <%@include%> is interpreted at the compile time and text is merged before JSP being converted into servlet.
While in case of is interpreted at the runtime where text is merged with JSP at runtime, may be a servlet or a JSP is included at runtime.
Continue reading...

What is the difference between Java Servlets and Java ServerPages (JSP)?

A JSP is a Servlet with a Web page flavor.There are more similarities than differences between the two.JSP uses its own tags which are ultimately using tag libraries implemented in Java.
The source code of a servlet  with HTML embedded in out. print (...) statements, gives dynamic features to webpages but that makes a cumbersome process to maintain servelts from web design perspective.If someone has implemented GUI related code inside servlet then each time a small GUI change will result in recompilation of the whole code.Hence business logic centric development should be done inside servlet and JSP gives a choice of combining both in much targeted way.JSP was devised to differentiate the tasks of web designers,responsible for look and feel of the webpages, from web developers who deal in business logic development.So both entities have focused interest met through JSP.In fact, a JSP gets compiled into a servlet.
Continue reading...

Interview Questions On Java,Java EE Copyright © 2017. Reads: best tracker