Set - 3

Question 1 :

What is JSP?

Answer :

Let's consider the answer to that from two different perspectives: that of an HTML designer and that of a Java programmer.
If you are an HTML designer, you can look at JSP technology as extending HTML to provide you with the ability to seamlessly embed snippets of Java code within your HTML pages. These bits of Java code generate dynamic content, which is embedded within the other HTML/XML content you author. Even better, JSP technology provides the means by which programmers can create new HTML/XML tags and JavaBeans components, which provide new features for HTML designers without those designers needing to learn how to program.
Note: A common misconception is that Java code embedded in a JSP page is transmitted with the HTML and executed by the user agent (such as a browser). This is not the case. A JSP page is translated into a Java servlet and executed on the server. JSP statements embedded in the JSP page become part of the servlet generated from the JSP page. The resulting servlet is executed on the server. It is never visible to the user agent.
If you are a Java programmer, you can look at JSP technology as a new, higher-level means to writing servlets. Instead of directly writing servlet classes and then emitting HTML from your servlets, you write HTML pages with Java code embedded in them. The JSP environment takes your page and dynamically compiles it. Whenever a user agent requests that page from the Web server, the servlet that was generated from your JSP code is executed, and the results are returned to the user.


Question 2 :

How do I mix JSP and SSI #include?

Answer :

Answer 1
If you're just including raw HTML, use the #include directive as usual inside your .jsp file.
But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. If your data.inc file contains jsp code you will have to use
The is used for including non-JSP files.
Answer 2
If you're just including raw HTML, use the #include directive as usual inside your .jsp file.

But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. Ronel Sumibcay (ronel@LIVESOFTWARE.COM) says: If your data.inc file contains jsp code you will have to use
<%@ vinclude="data.inc" %>
The is used for including non-JSP files.


Question 3 :

How do I mix JSP and SSI #include? What is the difference between include directive & jsp:include action?

Answer :

Difference between include directive and
1. provides the benefits of automatic recompliation,smaller class size ,since the code corresponding to the included page is not present in the servlet for every included jsp page and option of specifying the additional request parameter.
2.The also supports the use of request time attributes values for dynamically specifying included page which directive does not.
3.the include directive can only incorporate contents from a static document.
4. can be used to include dynamically generated output e.g.. from servlets.
5.include directive offers the option of sharing local variables, better run time efficiency.
6.Because the include directive is processed during translation and compilation, it does not impose any restrictions on output buffering.


Question 4 :

How do you prevent the Creation of a Session in a JSP Page and why? What is the difference between include directive & jsp:include action?

Answer :

By default, a JSP page will automatically create a session for the request if one does not exist.
However, sessions consume resources and if it is not necessary to maintain a session, one should not be created. For example, a marketing campaign may suggest the reader visit a web page for more information. If it is anticipated that a lot of traffic will hit that page, you may want to optimize the load on the machine by not creating useless sessions.


Question 5 :

How do I use a scriptlet to initialize a newly instantiated bean?

Answer :

A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.
The following example shows the "today" property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the jsp:setProperty action.
value=""/ >


Question 6 :

How can I set a cookie and delete a cookie from within a JSP page?

Answer :

A cookie, mycookie, can be deleted using the following scriptlet:


Question 7 :

How do you connect to the database from JSP?

Answer :

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.


Question 8 :

What is the page directive is used to prevent a JSP page from automatically creating a session?

Answer :

<%@ page session="false">


Question 9 :

How do you delete a Cookie within a JSP?

Answer :

Cookie mycook = new Cookie("name","value");
response.addCookie(mycook);
Cookie killmycook = new Cookie("mycook","value");
killmycook.setMaxAge(0);
killmycook.setPath("/");
killmycook.addCookie(killmycook);


Question 10 :

Can we implement an interface in a JSP?

Answer :

No


Question 11 :

What is the difference between ServletContext and PageContext?

Answer :

ServletContext: Gives the information about the container
PageContext: Gives the information about the Request


Question 12 :

What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

Answer :

request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.


Question 13 :

How to pass information from JSP to included JSP?

Answer :

Using <%jsp:param> tag.


Question 14 :

How is JSP include directive different from JSP include action. ?

Answer :

When a JSP include directive is used, the included file's code is added into the added JSP page at page translation time, this happens before the JSP page is translated into a servlet. While if any page is included using action tag, the page's output is returned back to the added page. This happens at runtime.


Question 15 :

Can we override the jspInit(), _jspService() and jspDestroy() methods?

Answer :

We can override jspinit() and jspDestroy() methods but not _jspService().


Question 16 :

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

Answer :

_jspService() method will be written by the container hence any methods which are not to be overridden by the end user are typically written starting with an '_'. This is the reason why we don't override _jspService() method in any JSP page.


Question 17 :

What happens when a page is statically included in another JSP page?

Answer :

An include directive tells the JSP engine to include the contents of another file (HTML, JSP, etc.) in the current page. This process of including a file is also called as static include.


Question 18 :

A JSP page, include.jsp, has a instance variable "int a", now this page is statically included in another JSP page, index.jsp, which has a instance variable "int a" declared. What

Answer :

happens when the index.jsp page is requested by the client?
Compilation error, as two variables with same name can't be declared. This happens because, when a page is included statically, entire code of included page becomes part of the new page. at this time there are two declarations of variable 'a'. Hence compilation error.


Question 19 :

Can you override jspInit() method? If yes, In which cases?

Answer :

ye, we can. We do it usually when we need to initialize any members which are to be available for a servlet/JSP throughout its lifetime.