Showing posts with label context scope. Show all posts
Showing posts with label context scope. Show all posts

Thursday, March 17, 2011

Chapter 16: Servlet Context

In the previous chapter, we saw how data can be stored and utilized per user session. The session is specific to a users navigation of a website and is not shared among users. If you want the system to retain some information that needs to be shared among various users of the system, we need to user the Context. In this chapter, we are going to see what the context is and what are the methods that are available for us to use.

So, lets get started!!!

The Servlet Context

A Web application includes many parts. It is more than just one servlet or JSP. Numerous JSPs and one or more Servlets and other supporting java classes together form the web application. To help manage an application, you will sometimes need to set and get information that all of the servlets share together, which we will refer to as context-wide.

For Example, if you want a single name using which you can refer to the application, you can set it in the servlet context and have it shared across all instances that use the application.

Ex Code:
public void init(ServletConfig config) throws ServletException
{
super.init(config);
// Get the Context
ServletContext context =config.getServletContext();
// Set the attribute
context.setAttribute(“appName", "My Test App");
}

Any time you want, you can refer to this attribute in the context and get its value like below:

String appName = context.getAttribute(“appName”);

After the above line of code, the variable appName will have the value “My Test App”

Methods in the Servlet Context

Apart from setting and getting custom attributes used for our application, the context also contains various methods that we can use to retrieve specific information about the application and other aspects. They are:

getAttributeNames() - Returns an Enumeration object containing the attribute names available within this servlet context.
getContext(String uripath) - Returns a ServletContext object that corresponds to a specified URL on the server.
getInitParameter(String name) - Returns a string containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
getInitParameterNames() - Returns the names of the context's initialization parameters as an Enumeration of string objects, or an empty Enumeration if the context has no initialization parameters.
getMajorVersion() - Returns the major version as an int of the Java Servlet API that this Servlet Container supports.
getMimeType(java.lang.String file) - Returns the MIME type as a string of the specified file, or null if the MIME type is not known.
getMinorVersion() - Returns the minor version as an int of the Servlet API that this Servlet Container supports.
getNamedDispatcher(String name) Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
getRealPath(String path) - Returns a string containing the real path for a given virtual path.
getRequestDispatcher(String path) Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
getResource(String path) - Returns a URL to the resource that is mapped to a specified path.
getResourceAsStream(String path) - Returns the resource located at the named path as an InputStream object.
getServerInfo() Returns the name and version as a String of the Servlet Container on which the servlet is running.

So, as you can see, the context is extremely powerful and useful for any J2EE developer…

Previous Chapter: Chapter 15 - Session

Next Chapter: Chapter 17 - Servlet Life Cycle

Monday, March 7, 2011

Chapter 13: Web Application Scope

In the previous Chapters we saw what the HTTP Request object is and how to read stuff from it. In this chapter, we are going to discuss a very important topic. “Scope” – Yes, the Web Application Scope. It is just like basic java programming where you can or cannot see a particular variable depending on its visibility. In Web parlance, the scope of an object defines how long it exists and how you can access it.

So, lets get started!!!

Scope

For the exam, you need to worry about 3 different scopes. They are:
• Request
• Session &
• Application

The scope would play an important part of any web application.
Let us look at the details of these 3 Scopes.

Scope Accessibility Lifetime
Request Current, included, or forwarded pages Until the response is returned to the user.
Session All requests from same browser within session timeout Until session timeout or session ID invalidated (such as user quits browser).
Application All request to same Web application Life of container or explicitly killed (such as container administration action).
You know that you can set and retrieve attributes (objects) in the request or the session or to the context. So, the purpose of this chapter is to let us identify what attribute is accessible where.

For ex: if you use the request.setAttribute(), you will be able to access it only until this request is alive. Once the request is complete, you will no longer be able to read the attribute that you just set into the request.

But, if you set an attribute into the session you will be able to read it across multiple requests. Similarly if you use the context, you will be able to read it across multiple sessions.

Exam Trivia:
Request is the least available whereas a session is a bit more available and the context is globally available. But, it is not advisable to put all your stuff into the context. Keep only information that you would need across all the users of the website in the context. Keep information that you would need as long as a user is browsing (say his login info) in the session and page specific information in the request.

Dont worry if we did not cover the details of the request, scope or the context because, that is exactly what the subsequent chapters are going to do!!!

Previous Chapter: Chapter 12 - Retrieving HTTP Request Header Information

Next Chapter: Chapter 14 - The Request Object
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers