EARN 400$ PER WEEK

Custom Search
Showing posts with label session. Show all posts
Showing posts with label session. Show all posts

Monday, November 8, 2010

Important concepts on Java Server Pages (JSP):

Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSP’s are secure, platform-independent, and best of all; make use of Java as a server-side scripting language.

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.

JSP TAGS:

JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries

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:

1. the tag handler class that defines the tag's behavior

2. the tag library descriptor file that maps the XML element names to the tag implementations

3. the JSP file that uses the tag library

1. Directive tags:

· Page directive: simple one for import statements.

· Include directive: to include specified file in jsp page.

· Taglib directive: used to create custom tags.

2. Scriplet tags:

· Scriplet tag: to specify java code with in jsp page.

· Expression tag: to evaluate an expression.

· Declaration tag: to declare variables. Etc.

3. Action tags:

· forward action: forward from one jsp page to another jsp

Page similar to “request dispatcher class in servlets”

· Include action: very much similar to include directive

except the imp. Difference. It’s for dynamic content where

as that is for static content.

· Use bean action: working with beans using jsp page.


Implicit Objects:

Certain objects that is available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. The implicit objects re listed below:

  1. Request : same as HttpServletRequest object.
  2. Response : same as HttpServletResponse object.
  3. Page : same as java.lang.Object.
  4. Config : same as ServletConfig.
  5. Context : same as servletContext.
  6. Out : same as printWriter object in servlets.
  7. Session: Http: same as Session obj.
  8. Page context: like page Context.
  9. Exception : java.lang.exception.

FAQ’s on JSP:

1. Difference between forward and sendRedirect?

When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completely with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.

2. How does JSP handle runtime exceptions?

Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.

3. How do you delete a Cookie within a JSP?

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

4. How to pass information from JSP to included JSP?

Using <%jsp: param> tag.

5. What is the difference between directive include and jsp include?

<%@ include>: Used to include static resources during translation time.

JSP include: Used to include dynamic content or static content during runtime.

6. What are the different scope values for the ?

The different scope values for are

1. Page
2. Request
3. Session
4. Application

7. Explain the life-cycle methods in JSP?

The generated servlet class for a JSP page implements the HttpJspPage interface of the javax.servlet.jsp package.

The HttpJspPage interface extends the JspPage interface which in turn extends the Servlet interface of the javax.servlet package. The generated servlet class thus implements all the methods of these three interfaces. The JspPage interface declares only two methods – jspInit () and jspDestroy () that must be implemented by all JSP pages regardless of the client-server protocol.

However the JSP specification has provided the HttpJspPage interface specifically for the JSP pages serving HTTP requests. This interface declares one method _jspService ().


The 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.
The _jspservice () - The container calls the _jspservice () for each request, passing it the request and the response objects.


The jspDestroy () - The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance.

8. What is a Scriptlet?

A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language. Within scriptlet tags, you can

1. Declare variables or methods to use later in the file
2. Write expressions valid in the page scripting language
3. Use any of the JSP implicit objects or any object declared with a tag.
You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet.

Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it.

9. What is an output comment?

A comment that is sent to the client in the viewable page source. The JSP engine handles an output comment as uninterrupted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser.

10. 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 or "comment out" part of your JSP page.

You can use any characters in the body of the comment except the closing -- %> combination. If you need to use -- %> in your comment, you can escape it by typing -- %\>.
JSP Syntax
<%-- comment -- %>

Examples
<%@ page language="java" %>

<%-- This comment will not be visible to the client in the page source --%>

FAQ's on Java Servlets

  1. What mechanisms are used by a Servlet Container to maintain session information?

Cookies, URL rewriting, and HTTPS protocol information are used to maintain session information

  1. Difference between GET and POST

In GET, your entire form submission can be encapsulated in one URL, like a hyperlink. Query length is limited to 260 characters, not secure, faster, quick and easy.

In POST, your name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the form's output. It is used to send a chunk of data to the server to be processed, more versatile, most secure.

  1. What is session?

The session is an object used by a servlet to track a user's interaction with a Web application across multiple HTTP requests.

  1. What is servlet mapping?

The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to servlets.

  1. What is servlet context?

The servlet context is an object that contains a servlet's view of the Web application within which the servlet is running. Using the context, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can use.

6.Which interface must be implemented by all servlets?

Servlet interface.

  1. What information that the ServletRequest interface allows the servlet access to?

Information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. The input stream, ServletInputStream.Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and PUT methods.

  1. What information that the ServletResponse interface gives the servlet methods for replying to the client?

It allows the servlet to set the content length and MIME type of the reply. Provides an output stream, ServletOutputStream and a Writer through which the servlet can send the reply data.

  1. When using servlets to build the HTML, you build a DOCTYPE line, why do you do that?

I know all major browsers ignore it even though the HTML 3.2 and 4.0 specifications require it. But building a DOCTYPE line tells HTML validators which version of HTML you are using so they know which specification to check your document against. These validators are valuable debugging services, helping you catch HTML syntax errors.

http://validator.w3.org and 
http://www.htmlhelp.com/tools/validator/ are two major online validators.

10. What's the advantages using servlets than using CGI?

Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. It is efficient, convenient, powerful, portable, secure and inexpensive. Servlets also address the problem of doing server-side programming with platform-specific APIs: they are developed with Java Servlet API, a standard Java extension.

11.Which code line must be set before any of the lines that use the PrintWriter?

setContentType () method must be set before transmitting the actual document.