Tuesday, October 30, 2012

Various ways of deploying to Tomcat

Below are the various possibilities we can deploy a web-application in Tomcat:

  • By setting autoDeploy to true at the host in server.xml, we can copy either copy the war file or unpacked directory structure onto the folder configured against appBase.  The application context would be either war name or the directory name.

  • Configure <Context> element in server.xml under <Host> element
        ...
        ...
        <Context path="/myApp" docBase="/myFolder" reloadable="true" />
      </Host>
    </Engine>
  </Service>
</Server>

The application context would be myApp

  • Write a configuration file with a <Context> element and place it under "conf\Catalina\localhost"
Sample myApp.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="Path_to_folder" path="/myApp" />

The application context would be  myApp

The context path can also be explicitly set in case of WAR files by packaging context.xml under META-INF within war.

Tomcat architecture

Here is a sum up on the architecture of tomcat for a quick recap:

Server
  • Is the top level component.
  • Shutdown port, string are configured here, but the request should be sent from same server on which Tomcat is running
  • Only one per jvm
  • Any implementation of org.apache.catalina.Server can be used via className attribute, if not specified org.apache.catalina.core.StandardServer is used
  • Global naming resources are configured here
  • Listeners could be configured here
Listener
  • Performs actions when specific events occur, usually Tomcat starting or Tomcat stopping
  • May be nested inside a Server, Engine, Host or Context only
  • Some Listeners are only intended to be nested inside specific elements only
  • APR Lifecycle, Jasper, Server Lifecycle, Global Resources Lifecycle, JMX Remote Lifecycle listeners could be configured only within Server
Container
  • Any one of Engine, Host, Context, and Cluster are referred as Container
  • Engine contains Host, it contains Context.  Cluster could be inside Engine or Host
Service
  • Groups a container (of type Engine) with a set of Connectors
  • Cannot define sub-components at level as it is not a Container
  • Only components that may be nested inside a Service element are one or more Connector elements, followed by exactly one Engine element
  • Can have multiple services under server, name must be unique (probably when we want one per protocol?)
  • Any implementation of org.apache.catalina.Service can be used via className attribute, if not specified org.apache.catalina.core.StandardService is used
 Connector
  • Is associated with a TCP port to handle communications between the Service and the clients
 Engine
  • Highest-level of a container, contains one or more Hosts
  • Represents the entire request processing machinery associated with a particular Service
  • Receives and processes all requests from one or more Connectors, and returns the completed response to the Connector for ultimate transmission back to the client
  • Exactly one Engine element MUST be nested inside a Service element, following all of the corresponding Connector elements associated with this Service
Host
  • Receives HTTP requests from the HTTP connector, and direct them to the correct host based on the hostname/IP address in the request header
  • Exactly one of the Hosts associated with each Engine MUST have a name matching the defaultHost attribute of that Engine
  • Can nest at most one instance of Realm, Valve
Realm
  • A Realm is a database of user, password, and role for authentication
  • You can define Realm for any container, such as Engine, Host, and Context, and Cluster
Valve
  •  A Valve can intercept HTTP requests before forwarding them to the applications, for pre-processing the requests
  • A Valve can be defined for any container, such as Engine, Host, and Context, and Cluster

Friday, November 19, 2010

XAMPP setup

Recently one of my friends asked me about PHP for web development.  My first question is why would one go for it when he is already aware of servlets/jsps?  One reason I got after some googling is the availability of ISPs providing PHP, still don't know why?  Is offering a web server that cheaper in terms of memory/processing compared to servlet container?  Whatever be the reasons, I started to get ready to jump into PHP and got stuck in the first step of setting up a development environment!!

Then I came across this wonderful package XAMPP comprising Apache server, PHP, Perl, MySql among other things.  Excellent - so I downloaded the zip bundle of it and could not get the server started even after struggling for couple of hours.  Tried changing the port where Apache runs, various directory paths in many conf files etc.

And finally it turned out to be a simple issue - unzip the package directly under C or D drive, no sub-folders in between, and everything will work like a charm.

Update: there is nice setup_xampp.bat that comes along, running which appropriate conf files will be updated with the location of extract.  Not really  necessary to unzip to C/D drive directly!!

Friday, July 24, 2009

Read-only or protected select box

We were trying to make a select box protected on one of you html pages. As part of capturing information we allow user to select an option, but on amend if the select box is already selected we want to make it read-only or protected.

First thing we did to achieve this is by disabling the select box. This gave us the required functionality on UI, but the select box's value is not reaching the server-side as part of form submit.

Next thing we tried is replacing the select box with a read-only input field of type text. This also gave the required UI feel, but still the value is not reaching the server-side as part of form submit. We then realised that while creating the text field, we gave it an id and other attributes except 'name' attribute. So one need to remember two things to make a value from client to server via form-submit:

1. The field should not be disabled
2. The field must be given the name attribute

Thursday, December 18, 2008

JSP include - Action vs Directive

In one of our web projects, We have a jsp file for creating the main interface of the appication. As development progressed, requirements grew and accordingly our jsp got bigger and bigger. After sometime when we looked back, we could identify some repeating sections (similar layout). Around the same time, we also got a requirement to have a dynamic number of repeating section based on client. With these requirements in mind we started working towards splitting the existing jsp into multiple jsp files and including them either conditionally or always according to the requirements.

So we have two requirements - To conditionally include (and include repeatedly) certain jsp pages and always include other jsp pages, so we came across the classic question of when to use jsp include action vs directive !!

The JSP spec says - action is run-time and directive is compile/translation-time activities. One can expect individual servlet files for each jsp file included using action and all jsps included using directive get merged into the including jsp itself.

Being aware of this information, we divided our huge jsp into multiple small files and gave them an extention jspf (for jsp fragment). Then we used jsp include directive for all the pages that need to be included always and jsp include action for other case, where need to repetitively include certain number of times based on some conditions. When tested this setup, no dynamic behaviour is displayed by the jsp include action and to make the problem more interesting we ended up with only a single servlet at the server-side (tested on both JBOSS and Websphere). We tried various combinations and tried changing everywhere to use only include actions, only include directives and other meaningful combinations and ended up with same result.

We did extensive googling and listed out the key differences between jsp include directive and action as below:

JSP include directive:

1. Affects the process during compile/translation of including JSP and the result is - whole content gets copied into the including JSP.
2. JSP could be completely static (pure HTML) or completely dynamic (pure JSP) or a mix. The dynamic content (scriptlets, expressions etc) is handled properly while the including JSP is compiled into a servlet.
3. As it is straight content copy, any variables declared in the including JSP are available for use.
4. The included JSP need not be valid on its own as it never gets compiled alone, but always as part of the including JSP
5. The file attribute value has to be a static name

JSP include action:

1. Affects the process during run-time/execution of including JSP, which means the including JSP get only the result of its execution (through an explicit call).
2. With include action, we can pass parameters from the including JSP and make the content more dynamic.
3. As the including JSP gets only the result of included JSP, variables declared in the including JSP are not accessible.
4. The included JSP need to be completely valid on its own.
5. The page attribute value could be dynamic (an expression)

We understand all the above points, but still couldn't understand where the problem is ! Finally, we tried renaming all *.jspf files to *.jsp and tested and bingo !! multiple servlets got created as expected for each include action. The undocumented rule is - as long as the included file extention is not jsp, include action works exactly as if it is a directive without throwing any errors. We observed the same behaviour with JBOSS (Tomcat) and Websphere. Hope this saves some effort for others.

Saturday, July 19, 2008

JavaScript frameworks

For a long time I was dying to put my hands into web application development and finally the day came. First thing I realized is that the foundations I have are sufficient only to make me feel at home, but are not at all sufficient to do some really useful stuff.

To start with we have put couple of servlets and a basic html and made the skeleton ready. Now its time to pick one javascript framework available for giving the web those special looks. I did googling for about couple of hours and got to know below frameworks:

  • jquery - Probably this is one of the recent (I know there is nothing like latest in our domain) frameworks out there. It provides various ways of manipulating DOM and event handling. Along with comes jquery ui, which provides some special effects like sliding, tabbed interfaces etc.
  • prototype - This is probably out there for quite some time. This also provides ways for manipulating DOM and handling events. There are other frameworks like scriptaculous/rico, developed over Prototype for developing special effects and animations.
  • mootools - This is all-in-one framework providing support for handling events, manipulating DOM, special effects, sorting of tables etc.
  • yui & ext - Both very rich in features, but could not understand what they are and how they work as my understanding of javascript is very limited as of now :-(
  • dojo - Yet another framework for DOM manipulation, dijit is developed over dojo for providing special effects.
Deciding on any of these frameworks would be a challenge, but fortunately other my job became simpler as there were other existing applications developed earlier using prototype/scriptaculous.

As I am skeptical about following practices (even standard ones, till I see the benefits), I did some research on jquery as an alternative to prototype. My understanding is that the way one collects a group of elements for special handling is different.

Jquery feels like a scripting language by the looks of its (non-object) syntax, uses CSS notations for forming collections and applying special effects whereas Prototype goes for object oriented notation. I also read some review which mentioned that Prototype also offers Jquery like syntax so it is all hazy at the moment for me. With this I am starting to learn Prototype/scriptaculous.

forward vs include of request dispatcher

Recently I was analyzing a web application along with my colleagues and came across an issue, which required us to be clear on the concepts of forward and include methods of RequestDispatcher. A brief on the application setup before I explain the problem and solution.

We have a filter, a servlet and a jsp. All requests coming for jsp shall go through the filter. Filter does a forward to servlet and then call the next one in the chain, which is jsp. With this setup we are getting the IllegalStateException when we invoke the jsp.

This issue helped to clear my basic understanding of forward & include. I always thought that with forward control does not come back to the invoker !! Wrong, control does come back.

Forward - control comes back to the invoker, but response shall be committed even if the invoked did not write anything to the output.

Include - control comes back to the invoker, but response shall not be committed. The invoked might add something to the response, to which invoker might add more.

Redirect - control does not come back to the invoker.

In our case the problem was after the forward call to the servlet from filter, response is committed. Then when the control reaches jsp, it is trying to create a session (we haven't disabled session creation) which is not allowed. To correct this we are creating the session before forwarding the request to the servlet so that when the control reaches the jsp, session shall be available.