Archive for July, 2007

HttpServletResponse response, (Remote web server) UserContainer userContainer ) throws Exception; //

Thursday, July 26th, 2007

HttpServletResponse response, UserContainer userContainer ) throws Exception; // This super Action is also a good place to put commonutility methods public boolean isLoggedIn( HttpServletRequest request ){ UserContainer container = getUserContainer(request); return ( container != null && container.getUserView( ) != null ); } /** * Retrieve the UserContainer for the user tied to the request. */ protected UserContainer getUserContainer(HttpServletRequest request) { HttpSession session = request.getSession( ); UserContainer userContainer = (UserContainer)session.getAttribute(IConstants.USER_CONTAINER_ KEY); // Create a UserContainer if one doesn’t exist already if(userContainer == null) { userContainer = new UserContainer( ); userContainer.setLocale(request.getLocale( )); session.setAttribute(IConstants.USER_CONTAINER_KEY, userContainer); } return userContainer; } } The StorefrontBaseAction class shown in Example 9-4 illustrates how you can use a base Action to perform repetitive behavior so that all of the subclasses need not perform it themselves. Suppose, for example, that all of your Action classes needed to obtain the UserContainerfor the current user and use some information within it (e.g., user ID or security permissions). One approach is to force all of the Action classes to obtain the UserContainer on their own, handle the situation when there isn’t one, and so on. An alternate, more manageable approach is to put that behavior in a super Actionand pass the UserContainerto the subclasses. As Example 9-4 shows, the StorefrontBaseAction implements the execute( ) method, but inside that method, it gets an instance of a UserContainer and passes it as an argument to
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

this code eliminates the redundancy. Example 9-4 provides (Web server extensions)

Thursday, July 26th, 2007

this code eliminates the redundancy. Example 9-4 provides a very simple version of a base Action class that performs this type of behavior. Example 9-4. A base Action class import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Iterator; import javax.servlet.http.*; import org.apache.struts.action.*; import com.oreilly.struts.storefront.framework.util.IConstants; import com.oreilly.struts.storefront.framework.exceptions.*; import com.oreilly.struts.storefront.framework.UserContainer; import com.oreilly.struts.storefront.service.IStorefrontService; /** * An abstract Action class that all Storefront Action classes can extend. */ abstract public class StorefrontBaseAction extends Action { /** * The default execute( ) method that all actions mustimplement. */ public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception{ // It just calls a worker method that contains the realexecute logic return executeAction( mapping,form,request,response,getUserContainer( request)); } /** * The actual do work method that must be overridden by thesubclasses. */ abstract public ActionForward executeAction( ActionMapping mapping, ActionForm form, HttpServletRequest request,
You want to have a cheap webhost for your apache application, then check apache web hosting services.

if ( remoteHost == null|| !remoteHost.startsWith( (Web host 4 life) “127.”) ){

Wednesday, July 25th, 2007

if ( remoteHost == null|| !remoteHost.startsWith( “127.”) ){ // Not the localhost, so don’t allow the host to accessthe site continueProcessing = false; ForwardConfig config = appConfig.findForwardConfig(”Unauthorized”); try{ response.sendRedirect( config.getPath( ) ); }catch( Exception ex ){ log.error( “Problem sending redirect fromprocessPreprocess( )” ); } } return continueProcessing; } The most important thing to note in this example is that even though the processPreprocess( )method is returning false, it’s still up to the method to take care of redirecting the request. Returning false just lets the RequestProcessor know that it doesn’t need to continue. The controller doesn’t do anything with the request; that’s the responsibility of the processPreprocess( ) method. The manner in which Example 9-3 specifies the path in the method prevents us from having to hardcode the URI of the unauthorized_access.jsp resource. This way, if the actual page for the forward changes, this method will not have to be modified. There are other ways that you can perform the same logic without using the processPreprocess( )method. One alternative is to use a servlet filter (part of the Servlet 2.3 API). Filters allow you to inspect the request before it ever reaches the Struts controller. However, there are two problems to be aware of with filters. First, because filters are part of the 2.3 API, you will not be able to use them if you are using a servlet container that supports only 2.2. Second, because the filter inspects the request very early in the processing stage, filters don’t have easy access to the Struts API. This makes it hard to look up ActionForwards or anything else that you might normally use in the processPreprocess( )method. In fact, because the Struts controller hasn’t even seen the request at the time the filter inspects it, the controller hasn’t had a chance to select the proper application module. 9.3.3 Extending the Base Action Class There have been several places in previous chapters where I’ve mentioned a technique of creating a base Action that extends the Struts Actionclass and then using it as a superclass for other actions. One of the reasons for doing this is that in many applications, there is common logic that must be implemented by most of the Actionclasses. Letting this Action superclass contain most of
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

The processorClass attribute allows you to specify the (Cool web site)

Wednesday, July 25th, 2007

The processorClass attribute allows you to specify the fully qualified Java class name of your specialized RequestProcessor. The Struts framework will create an instance of your specialized RequestProcessor at startup and use it to process all of the requests for the application. Because each application module can have its own Struts configuration file, you can specify a different RequestProcessorfor each module. 9.3.2.1 Using the processPreprocess( ) method There are many methods that can be overridden within the RequestProcessor class. One of the methods that was designed with extension in mind is the processPreprocess( ) method. This method is called for each request. By default, it does nothing with the requests, but you can use this method in various ways to change the default request-processing behavior. The processPreprocess( )method looks like this: protected boolean processPreprocess( HttpServletRequestrequest, HttpServletResponseresponse ){ return (true); } This method is called early in the request-processing stage, before the ActionForm is called and before the execute( )method is called on the Action object. By default, the processPreprocess( ) method returns true, which tells the RequestProcessorto continue processing the request. However, you can override this method and perform some type of conditional logic, and if the request should not be processed any further, you can return false. When processPreprocess( )returns false, the RequestProcessorstops processing the request and simply returns from the doPost( )or doGet( ) call. Consequently, it’s up to you to programmatically forward or redirect the request within the processPreprocess( ) method. Example 9-3 illustrates this approach. This example checks to see whether the request is from a local host. If so, it returns trueand the request continues. If the request is from any other host, the request is redirected to the unauthorized access page and the method returns false. Example 9-3. Using the processPreprocess( ) method protected boolean processPreprocess( HttpServletRequestrequest, HttpServletResponseresponse ){ boolean continueProcessing = true; // Get the name of the remote host and log it String remoteHost = request.getRemoteHost( ); log.info( “Request from host: ” + remoteHost ); // Make sure the host is from one that you expect
You want to have a cheap webhost for your apache application, then check apache web hosting services.

9.3 Controller Extension Points (Ipower web hosting) The next set of

Tuesday, July 24th, 2007

9.3 Controller Extension Points The next set of possible extension points is within the controller layer. Some of these have been mentioned briefly in previous chapters, but they’re repeated here for completeness. 9.3.1 Extending the ActionServlet Class In earlier versions of the Struts framework, it was almost a given that an application needed to extend the ActionServlet class because most of the controller functionality, excluding the Action class behavior, was present in this class. With Struts 1.1, this is no longer true. However, there are still a few good reasons why you might need to extend the ActionServletclass. As was pointed out in Chapter 5, the initialization routines that are invoked when a Struts application is first launched reside in the ActionServlet class. If you need to modify the way the framework initializes itself, this is the place to do so. To extend the ActionServlet class, just create a subclass of org.apache.struts.action.ActionServlet. You can then override the method or methods that you need to function differently. Once this is done, you need to modify the deployment descriptor so that the Struts application will use your custom ActionServlet: storefront com.oreilly.struts.storefront.framework.ExtendedActionServlet Most of the runtime request-processing behavior has been moved to the RequestProcessorclass in Struts 1.1. If you need to customize the manner in which your Struts application processes a request, see the next section. 9.3.2 Extending the RequestProcessor Class If you need to override functionality within the RequestProcessor class, you must let the framework know that it should use your customized version rather than the default. You can make the framework aware of your specialized RequestProcessor by modifying the configuration file for the Struts application. If your configuration file doesn’t already have a controller element within it, you’ll need to add one (there are several attributes that can be configured within the controller element see Chapter 4 for more details):
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

When the framework calls the init( (Web design portfolio) )method of

Tuesday, July 24th, 2007

When the framework calls the init( )method of the JNDIConnectorPlugin class, the plug-in creates an InitialContext object and stores it into the ServletContext. This allows the JNDI InitialContext to be used by the entire application, when needed. This is just a simple example; there are many possible uses for the PlugIn mechanism. For example, the Validator framework, which we’ll discuss in Chapter 11, uses the PlugIn mechanism to initialize the validation rules for an application. 9.2.1.1 Adding the plug-in to the configuration file The plug-in must be declared in the Struts configuration file in order for the framework to be aware of it and initialize it at startup. It’s specified in the configuration file using the plug-in element:
You also can pass properties to your PlugInclass by using the set-property element. If more than one element is specified, they will be initialized in the order in which they are listed in the configuration file. For more information on configuring the plug-in element or passing properties to an instance, see Section 4.7.2 in Chapter 4. 9.2.2 Extending the Struts Configuration Classes One of the biggest changes to Version 1.1 of the Struts framework is the org.apache.struts.config package. This package contains all of the classes that are used as in-memory representations of the information stored in the Struts configuration file. They represent everything from Action configurations to PlugIn configurations. If you look back at Chapter 4, you’ll see that most of the configuration elements in the Struts configuration file allow you to supply a fully qualified Java class name for the configuration class through the className attribute. This gives you the freedom to customize the configuration element and pass additional information. For example, suppose that you want to pass an additional parameter to your Action classes. By default, the ActionMapping class, which extends ActionConfig from the configpackage, is used. To pass an additional parameter called ssl-required that controls whether HTTP or HTTPS is used, you can extend the ActionMapping class and configure this extension through the classNameattribute. The ability to extend the Struts configuration elements through this mechanism makes the framework extremely extensible and flexible enough to meet just about any application need.
Check Tomcat Web Hosting services for best quality webspace to host your web application.

private String jndiFactoryClass; (Web hosting ecommerce) private String jndiURL; private Context

Monday, July 23rd, 2007

private String jndiFactoryClass; private String jndiURL; private Context initCtx = null; public JNDIConnectorPlugin( ) { super( ); } public void init(ActionServlet servlet, ApplicationConfigconfig) throws ServletException{ // Get the host and port where the JNDI service isrunningjndiFactoryClass = servlet.getInitParameter(”jndifactory-class”); jndiURL = servlet.getInitParameter(”jndi-url”); try{ Hashtable props = new Hashtable( ); // The EJB spec also allows these to be read from the jndi.properties fileprops.put( Context.INITIAL_CONTEXT_FACTORY, jndiFactoryClass ); props.put( Context.PROVIDER_URL, jndiURL ); initCtx = new InitialContext(props); }catch( Exception ex ){ throw new ServletException( ex ); } // Store the JNDI Context into the ServletContext servlet.getServletContext( ).setAttribute( “Storefront.InitCtx”, initCtx ); } public void destroy( ){ try{ if ( initCtx != null ){ initCtx.close( ); initCtx = null; // No need to remove from ServletContext because app is being shut down} }catch( Exception ex ){ ex.printStackTrace( ); } } }
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Example 9-1. The org.apache.struts.action.PlugIn interface public interface PlugIn

Monday, July 23rd, 2007

Example 9-1. The org.apache.struts.action.PlugIn interface public interface PlugIn { /** * Notification that the specified application module isbeing started. */ public void init(ActionServlet servlet, ApplicationConfig config) throws ServletException; /** * Notification that the application module is being shutdown. */ public void destroy( ); } During startup of a Struts application, the ActionServlet calls the init( ) method for each PlugIn that is configured; the framework supports configuration of one or more PlugIns for each application. Initialization routines that your plug-in needs to perform should be done during the init( )method. This is a good time to initialize a database connection or establish a connection to a remote system, for example.[1] [1] You also can initialize a database connection through the use of a datasource. The second method that your plug-in must implement is the destroy( )method. The framework calls this method when the application is being shut down. You should perform any necessary cleanup during this time. For example, this is the perfect time to close database connections, remote sockets, or any other resources that the plug-in is using. Let’s provide a concrete example of how to use the Struts framework’s PlugIn mechanism. Suppose that your application needs the ability to communicate with an EJB tier. One of the first things you must do before that can occur is to get a reference to the Java Naming and Directory Interface (JNDI) service. JNDI enables clients to access various naming and directory services, such as datasources, JavaMail sessions, and EJB home factories. Example 9-2 illustrates a simple example of acquiring an InitialContext for a JNDI service using the Struts PlugIn mechanism. Example 9-2. An example of using the Struts PlugIn mechanism package com.oreilly.struts.storefront.framework.ejb; import java.util.Hashtable; import javax.naming.InitialContext; import javax.naming.Context; import org.apache.struts.action.ActionServlet; import org.apache.struts.config.ApplicationConfig; import org.apache.struts.action.PlugIn; import javax.servlet.ServletException; public class JNDIConnectorPlugin implements PlugIn {
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Jetty web server - It should be obvious from looking at the

Monday, July 23rd, 2007

It should be obvious from looking at the tags included with JSTL that not all of the Struts tags are being replaced. The tags within the HTML tag library in particular will be around for some time. Chapter 9. Extending the Struts Framework One of the biggest advantages of using a framework is the ability to extend and customize it based on the needs of the application. The Struts framework is no exception it provides several very important extension points for developers. This chapter takes a quick glance at some of those extension points and discusses the benefits (as well as a few downsides) of extending the framework. 9.1 What Are Extension Points? Think of a framework as a house that comes with part of the structure already complete, but gives you the option to modify certain characteristics, such as the wallpaper and paint colors. If the default characteristics already suit your needs, you don’t have to change anything. If you like the paint job, for example, that’s one less thing to worry about. This is similar to how a framework functions, and that’s a big advantage when building applications. If functionality that suits the needs of your application is present in the framework, you don’t have to worry about that aspect of the application. This, in turn, frees up developers to focus on the core application rather than the infrastructure. This is not a perfect analogy, but the point is that a good framework should provide much of the infrastructure the foundation and the plumbing, for example. The most important aspect of a framework, however, is that it should provide extension points throughout. Framework extension points, also referred to as “hooks,” allow you to extend the framework in specific places to adapt it to meet the application’s requirements. Where and how a framework provides these hooks is very important. If they’re provided incorrectly or in the wrong locations, it becomes very hard for an application to adapt the framework, which makes the framework less useful. The rest of this chapter focuses on where the Struts framework provides these extension points and how you can take advantage of them to build out specialized functionality for your application. 9.2 General Extension Points This section discusses some extension points that affect the overall framework, not necessarily one particular layer. Arguably the most important of these is the PlugIn mechanism. 9.2.1 Using the PlugIn Mechanism The Struts framework provides a mechanism to allow components to be plugged in and loaded dynamically. This feature was added in Version 1.1 and is supported through the use of the org.apache.struts.action.PlugIn interface. Any Java class can function as a plug-in, as long as it implements the PlugIn interface. A plug-in is simply any Java class that you need to initialize when the Struts application starts up, and destroy when the application shuts down. The PlugIn interface contains two methods, as shown in Example 9-1.
We recommend high quality webhost to host and run your jsp application: christian web host services.

The messaging tags (Web hosting unlimited bandwidth) assist page authors with creating

Sunday, July 22nd, 2007

The messaging tags assist page authors with creating messages that can be adapted to any locale available in the JSP container. They include tags such as bundleand message. The formatting tags allow various data elements, such as numbers, currencies, dates, and times, to be formatted and parsed in a locale-sensitive manner. They include tags such as formatDate, parseNumber, and timeZone. 8.10.3 JSTL XML Tags The JSTL XML tag library includes tags that allow XML documents to be accessed. The tags are based on XPath. The XML tags use XPath as a local expression language. 8.10.4 JSTL SQL Tags The JSTL SQL tags allow direct access to JDBC resources. They are designed for quick prototyping and simple applications. For larger, more advanced applications, database operations should not be present in the presentation layer they are normally performed in the model layer. This helps to ensure separation of responsibility. The tags within the JSTL SQL library include tags for configuring the JDBC resource as well as for querying and updating the database. 8.10.5 A New Expression Language JSTL will also introduce a new expression language (EL) to make it easier for page authors to access application data without forcing them to learn a full-fledged programming language like Java. The EL will eventually be part of JSR 152 (the JSP 1.3 specification), but it has been included with JSTL to ensure that a specification is available for the JSTL schedule. The two groups are working closely together so that the EL fulfills both sets of needs. The JSTL tag libraries come in two versions. One version, known as the JSTL-RT, will continue to support expressions as they are used today. The second version, known as JSTL-EL, will support the new expression language. Both versions will be supported. For more information on the EL, refer to the JSTL 1.0 specification, which can be downloaded from http://jcp.org/jsr/detail/52.jsp. 8.10.6 JSTL Requirements JSTL requires a servlet container that supports the Servlet 2.3 specification and Version 1.2 of the JSP specification. If you are using a container that doesn’t support these specifications, you will not be able to use JSTL. Tomcat is one container that supports both, but there are many more. Make sure to check with your vendor before attempting to use JSTL. 8.10.7 JSTL and Struts The Struts Bean and Logic tag libraries may eventually be phased out in favor of the JSTL tags. In the next few versions of the Struts framework, look for changes in these two libraries to help ease that migration.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.