Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts

Sunday, July 3, 2011

Context.xml

Some of you guys already know that I love to develop on Tomcat and release into a bigger App Server, and for those who have noticed, I use a lot this file on my apps, well there you have some interesting info about this file :)

The context.xml file is an optional file which contains a <Context> tag (Context Fragment) for a single Tomcat web application. This can be used to define certain behaviours for your application, JNDI resources and other settings.
The context.xml file was introduced in Tomcat 5, to remove Context settings from the server.xml file.
The Context Fragment can be embedded in server.xml, placed in the <CATALINA>/conf folder, or placed inside each application in the META-INF folder in a file called context.xml. The META-INF method is preferred as this means changes to the context don't require Tomcat to be restarted. You can restart your application by modifying web.xml when automatic reloading is active, or reloading manually with the Tomcat Manager.
Your context.xml file should be installed in the META-INF folder of your application, as META-INF/context.xml. The META-INF folder belongs at the same level as WEB-INF (not inside it).
index.jsp
/WEB-INF
    web.xml
    /lib
        myjar.jar
    /classes
        myclass.class
/META-INF
    context.xml
Surprisingly, not many developers use this file within their applications yet, although it is very useful. Switching to the use of a context.xml file removes all dependencies from server.xml, making the application much more portable and easier to deploy.

Example context.xml file

This file provides a JNDI resource representing a MySQL datasource, and a security realm.

Notes

  • Some examples show path and docBase attributes in the <Context> tag. These are required if the context tag is in server.xml, but in context.xml they are optional, and often just cause deployment problems (eg: if the application is deployed with a different name). It is easier (and better) to leave them out, making the context name automatic. In this case, just use <Context> with no attributes, as in the above example.
  • Default settings for all Contexts are defined in the context.xml.default file in the <CATALINA>/conf folder

Gotchas / Troubleshooting / Debugging JNDI DataSource issues

  • JNDI issues are difficult to debug, as there is no debug output, so if you have problems, check this list
  • With a JNDI datasource, make sure to include the JDBC driver in <CATALINA/common/lib or it will not load
  • If a JNDI datasource does not work, ensure the connection details are correct by connecting a different way (eg: telnet or the mysql command client using the same details)
  • deployXML must be set to false (the default) in the <Host> entry in server.xml, or the context.xml file will be ignored
  • Tomcat 5.5 and Tomcat 6 copy the context.xml file to <CATALINA>/conf/Standalone after the first the successful deployment, and do not remove or update the file unless the application is undeployed. If you change your context.xml file and find it still doesn't work, or your changes are ignored, delete any files in the <CATALINA>/conf/Standalone folder to ensure they are redeployed correctly. Tomcat 7 does not have this problem, unless copyXML is set false (default is true).

Friday, June 17, 2011

Removing taglib from web.xml

JSP 1.2 required the <taglib> directive to be used in web.xml for every JSP Custom Tag Library used by the application. In JSP 2.0 this is no longer required. This guide shows how to get rid of your <taglib> directives and how JSP 2.0 finds TLD files.
The <taglib> directive looks like this. You will find one in web.xml for older versions of Struts, and any other software which uses tag libraries in JSP 1.2.
 
Since JSP 2.0, this is now optional.
The TLD is discovered automatically when the taglib is first referenced in a JSP file.
The actual JSP 1.2 TLD files themselves are upwardly compatible with JSP 2.0, so do not need to be modified.

Where does JSP look for the TLD files?

JSP 2.0 containers such as Tomcat 5.5 search for TLD files in the following places:
The following locations are searched:
  • <appName>/WEB-INF folder
  • All subfolders of <appName>/WEB-INF
  • Inside each JAR file in <appName>/WEB-INF/lib, in the META-INF folder of the JAR. This method is prefered for easy deployment of a JAR containing a taglib.  

How should I package my JAR file?

Place TLD files in a folder named META-INF, at the root level of the JAR file. eg:

Why is this better?

For library developers, distribution is now simpler as packaging is as simple as including the TLD file in the JAR file.
For end-users, deployment and re-use is simpler, as a tag library JAR file can simply be dropped into the WEB-INF/lib folder with no web.xml configuration required.

TLD Tips in JSP 2.0

  • The TLD file does not need to have the same name as the library. For example, your mytags library definition can be in a file called mytaglist.tld. However, for easier identification later, it is good practise to use the same name as you intend to use in the JSP files. For example, if you plan to reference your library as mytags, with <mytags:testtag> in JSP, call the file mytags.tld.
  • The <short-name> element in the TLD file does not need the same name as the library. Once again, for easier maintenance and to avoid potential clashes, it is good practise to use the same name. For example <short-name>mytags</short-name>.
  • The <uri> in the TLD file is the main reference for matching JSP declarations with the correct TLD. When automatically finding TLD files, the container matches the uri attribute in your JSP taglib declaration with the <uri> tag in the TLD file. Ensure this is always unique to avoid clashes. For example:


Upgrading your application

  • Many older applications still include the <taglib> directive in web.xml. If your application falls into this category, try removing it and check if everything still works. Don't forget to move your TLD file into WEB-INF, or preferably into your JAR file. Your web.xml will be simpler, and the Tag Library will be more modular (easier to remove from the app, replace, or reuse).
  • In older applications, the TLD file is sometimes in a separate folder, such as in the example above (/WEB-INF/jsp/mytaglib.tld). In this case, move the file into the JAR file if possible. TLD files will still work from subfolders of WEB-INF, but since the location of the TLD files is no longer recorded in web.xml, it can make them harder to find when debugging your system later - especially after a few months of not looking at it, or if someone else is trying to debug your code. It therefore makes sense to either include them in the JAR file which contains the classes for the tag, or at least place them in an easy to notice location such as a subfolder called WEB-INF/tld or WEB-INF/taglibs.

Reference: http://wiki.metawerx.net/wiki/Web.xml

Monday, March 7, 2011

Mapping 2 or more URL patterns with same servlet

We have a simple Spring MVC servlet (as usual), nothing out of normal.
With its respective mapping
This is the tricky part, I want to map some weird request mappings like: *.htm or *.jspx (besides *.do)
  • registerCustomer.do
  • registerCustomer.htm
  • registerCustomer.jspx

The solution is simple, just add another entry to your servlet-mapping: The only restriction is to use web-app version=2.4 or 2.5
I always use 2.4 and I didn't have anything else to do. But for educational purposes I updated my web.xml (web app descriptor) for my future developments.

Thursday, August 19, 2010

How to test servlet using Telnet

Essentially what I need to do is just make an HTTP Call using Telnet.

$> telnet 127.0.0.1 9086
Ok, I'm connected, and now what??
I know which servlet I want to test, I know which parameters is expecting, then "let's do it":

Which method should I use? POST or GET?? let's use POST (due to well known limitations with GET)

This is my URL:
/MyWebApp/proxyServlet?operation=doSomethingNice&consumerName=myActiveMQConsumers&server=192.168.5.122:9080&targetServlet=/MyOtherWebApp/utilsServlet
I'll finish my command with the HTTP version I want to use: HTTP/1.0 (if I use 1.1 it'll keep you connected)

And finally let's put all together:

Important: Once you have copied this full line in your telnet body, please type ENTER twice.

How do I know if this works? You should see this on your telnet console:
HTTP/1.0 200 OK
Content-Type: text/xml;charset=ISO-8859-1
Cache-Control: no-cache
Content-Language: en-US
Content-Length: 21
Date: Thu, 19 Aug 2010 18:38:45 GMT
Server: WebSphere Application Server/6.1

true

Connection to host lost.

Wednesday, August 11, 2010

Cross Domain issue - Ajax

Who say yes???
I was working in a GUI for controlling components in different servers from one server - stopping, starting, purging queues & consumers; finally beans
The firSt thing it came to my mind was: "let's do it with AJAX to avoid multiple calls and decrease the traffic!!!"
Which AJAX framework should I choose??? Let's learn jQuery, because that one has a bunch of cool features which will be really helpfull to develop my GUI (check this site: flowplayer.org they have a bunch of demos which are really easy to follow and all based on jQuery)
My first issue was learnign that Ajax call, but after a few hours I could do my first ajax call using jQuery (I have a servlet which returns XML or JSON)

Now what??? If I'm accessing the servlet in the same context - domain, everything works fine, but... If I try to access my servlet but in another server

Tons tuve que eccharle coco
My solution was to create a ProxyServlet which basically will take my request and pass it to the remote server & servlet using an HttpClient and in the same way, it will get the response coming from the remote servlet, in other words, my ProxyServlet (in the same domain - context) will be the only one talking to my Ajax call, eventhough ProxyServlet will be talking to the remote server & servlet. Then I did it again  

This is my code:

This is my main servlet (the one which returns XML or JSON using json-simple project)

And this now, these are portions of my jQuery code:


You can get the additional JavaScript libraries from: JSON2 and my version of jQuery Tools