Saturday, January 5, 2013

Java Servlet

They're (in most cases) inherently more efficient than CGI, because servlets spawn new threads, rather than separate processes, for requests

When you create a Java servlet, you typically subclass HttpServlet. This class has methods that give you access to the request and response wrappers you can use to handle requests and create responses.

There is one ServletConfig object per servlet and one ServletContext per webapp.
The ServletContext object is contained within the ServletConfig object.
jsp files are compiled at run time but can also be compiled at deployment time.
The include directive <%@ include file=" welcome.jsp " %> is similar to copy and paste operation . It is evaluated when the page containing the directive is compiled  
Later changes to the included file will not be detected.
The <jsp:include page="welcome.jsp "> tag compiles the file as a separate JSP file, and embeds a call to it in the compiled JSP.  
jsp:include action is evaluated at request time, so if the included file has been editted, this is detected and it will be recompiled.
JSP files are compiled , first time a page is loaded in browser.
JSP files can be pre compiled during development time

Which of the following are the best practices for making the web pages faster ?  
Put Stylesheets at Top
Make JavaScript and CSS external
Add an Expires or a Cache-Control Header
Minimize HTTP Requests

Http Session object can be passed to EJB if all the objects inside that are Serializable
When to use doGet() and when doPost()?

Always prefer to use GET (As because GET is faster than POST), except mentioned in the following reason:

  • If data is sensitive
  • Data is greater than 1024 characters
  • If your application don't need bookmarks.

forward()sendRedirect()
A forward is performed internally by the servlet.A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original.
The  browser is completely unaware that it has taken place, so its original URL remains intact.The browser, in this case, is doing the work and knows that it's making a new request.
Any browser reload of the resulting page will simple repeat the original request, with the original URLA browser reloads of the second URL ,will not repeat the original request, but will rather fetch the second URL.
Both resources must be part of the same context (Some containers make provisions for cross-context communication but this tends not to be very portable)This method can be used to redirect users to resources that are not part of the current context, or even in the same domain.
Since both resources are part of same context, the original request context is retainedBecause this involves a new request, the previous request scope objects, with all of its parameters and attributes are no longer available after a redirect.
(Variables will need to be passed by via the session object).
Forward is marginally faster than redirect.redirect is marginally slower than a forward, since it requires two browser requests, not one.


ServletRequest.getRequestDispatcher(String path)ServletContext.getRequestDispatcher(String path)
The getRequestDispatcher(String path) method ofjavax.servlet.ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a “/” it is interpreted as relative to the current context root.The getRequestDispatcher(String path) method ofjavax.servlet.ServletContext interface cannot accept relative paths. All path must start with a “/” and are   interpreted as relative to current context root.


The <load-on-startup> element of a deployment descriptor is used to load a servlet file when the server starts instead of waiting for the first request. It is also used to specify the order in which the files are to be loaded. The <load-on-startup> element is written in the deployment descriptor as follows:
<servlet>
  <servlet-name>ServletName</servlet-name>
  <servlet-class>ClassName</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
Note: The container loads the servlets in the order specified in the <load-on-startup> element.

A session refers to all the requests that a single client might make to a server in the course of viewing any pages associated with a given application. Sessions are specific to both the individual user and the application. As a result, every user of an application has a separate session and has access to a separate set of session variables.
What are the types of Session Tracking ?
Sessions need to work with all web browsers and take into account the users security preferences. Therefore there are a variety of ways to send and receive the identifier:

  • URL rewriting : URL rewriting is a method of session tracking in which some extra data (session ID) is appended at the end of each URL. This extra data identifies the session. The server can associate this session identifier with the data it has stored about that session. This method is used with browsers that do not support cookies or where the user has disabled the cookies.
  • Hidden Form Fields : Similar to URL rewriting. The server embeds new hidden fields in every dynamically generated form page for the client. When the client submits the form to the server the hidden fields identify the client.
  • Cookies : Cookie is a small amount of information sent by a servlet to a Web browser. Saved by the browser, and later sent back to the server in subsequent requests. A cookie has a name, a single value, and optional attributes. A cookie's value can uniquely identify a client.
  • Secure Socket Layer (SSL) Sessions : Web browsers that support Secure Socket Layer communication can use SSL's support via HTTPS for generating a unique session key as part of the encrypted conversation.

What is the difference between using getSession(true) and getSession(false) methods?

Answer
getSession(true) will check whether a session already exists for the user. If yes, it will return that session object else it will create a new session object and return it.
getSession(false) will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null.

No comments:

Post a Comment