Showing posts with label JSP. Show all posts
Showing posts with label JSP. 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

Friday, May 20, 2011

Cross-site Scripting (XSS)

Cross-Site Scripting attacks are a type of injection problem, in which malicious scripts are injected into the otherwise benign and trusted web sites. Cross-site scripting (XSS) attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user in the output it generates without validating or encoding it.

An attacker can use XSS to send a malicious script to an unsuspecting user. The end user’s browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by your browser and used with that site. These scripts can even rewrite the content of the HTML page.

The normal practice is to HTML-escape any user-controlled data during redisplaying in JSP, not during processing the submitted data in servlet nor during storing in DB. In JSP you can use the JSTL (to install it, just drop jstl-1.2.jar in /WEB-INF/lib) <c:out> tag or fn:escapeXml function for this. E.g.

OR
This is the classic example: Look carefully to the param authtype, did you see the alert(1)??? If you don't write safe code, you might get an infinite loop in your screen :)

For more details please go to: https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)

Wednesday, April 27, 2011

Upload Files with Spring MVC

Have you ever tried to upload a file as part of a form to be submited?
In my case I have a form to add or update products, and each product must have an image (JPG file), besides the regular information related to each product: Name, Description, etc.
Then, we need a form with some inputs and a method on one of my controllers to handle this request and we're set.

This topic is really simple to cover, despite of the fact is poorly covered by the official documentation.
Steps
Add entries on your POM
Add a MultipartResolver in your Web Application Context
On your form don't forget to include enctype attribute and make it POST

Add your input type file as regular HTML


In your controller add a method to handle with all params
I'm using FileUtils to save the uploaded file :D
Look carefully to this parameter @RequestParam("productImage") matches to

And you're all set! Easy, Isn't it?

Tuesday, March 15, 2011

Pass parameters from one page to another

This is the issue, I have a JSP which helps me to generate a URL to change the Locale, to switch between Spanish and English, but I have a JSP (maybe more) which is displayed based on a Parameter, because shows the details for an Object based on idObject.
This is my original JSP (So each time I clicked the link, the page was refreshed, but missing the param, obviously the content was never displayed - duh!)

The fix is really simple, just add more params to each URL, by using JSTL :) Like this (creating dynamically my params)

Friday, March 11, 2011

Applications Multilanguage - Issue with Ƒ and Accents

It's been a while since I worked with an Application in Spanish.
Always English!

This time, testing that my messages properties were used properly (I was just testing that I didn't miss any message), I saw something really weird:

After thinking about what happened in Japan and reading the news, I came back to this issue.

Then, I realized I was using the wrong META TAG (Charset)
After updating my Charset to "ISO-8859-1", everything look as I expected, hopefully I won't have to face any issue like this one with another language (like Russian, Hindi or Japanese) because as I know all of them have a different CharSet different from English and Spanish :)

Friday, August 13, 2010

Spring MVC - Binding VO to a Form

Problema:
I have a VO and I wanted to populate it on my JSP and on my controller just process the VO already populated.

Solution:

On my POST method I'll handle my VO already populated.
This entry will do 50% of the job @ModelAttribute(value="LoggerVO") LoggerVO loggerVO
On my JSP I'll map using spring's tag library
By doing this modelAttribute="LoggerVO" commandName="LoggerVO"
I'm matching what I have on my form and what I have on my Controller
I hope this will help you!
Salu2