Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Unheralded Java Filters Simplify Web-app Testing : Page 3

Java filters, the powerful—yet under-appreciated—components of the Java Servlet specification, can perform a range of useful tasks. Make them a part of your Java Web application toolkit.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Getting Started with the Filter API (cont'd)
With your HTML in hand, the real work begins. Examine the remaining code below and read the highlights at the end:

// determine the script paths String filePath = req.getSession().getServletContext().getRealPath (req.getServletPath()) + ".validate.htm"; String fileUrl = http:// + req.getServerName() + ":" + req.getServerPort() + req.getRequestURI() + ".validate.htm"; // write the temporary page out to disk FileWriter fw = new FileWriter(filePath); fw.write(contents); fw.close(); // validate the page String results = this.validate(req, fileUrl, contents); // delete the page on disk File f = new File(filePath); f.delete(); // insert our validation results into the page contents = contents + results; // send the page to the browser CharArrayWriter caw = new CharArrayWriter(); caw.write(contents); response.setContentLength(caw.toString().length()); out.write(caw.toString()); out.close();

The code should be easy enough to read. First, you determine where to write your file and the corresponding URL where the W3C Validation Service can access it. Then you write the file out. The following line performs the actual submission to the W3C Validation Service and formats the XML results, via the validate() method:

String results = this.validate(req, fileUrl, contents);

I've left out the code for the validate() method, as it has little to do with Java filters specifically. If you have worked with XML in Java—using JAXP and DOM and such—the code should be familiar territory. If not, it's not tough to follow anyway. You can find the complete source code in the downloadable code for this article.

Since your temporary HTML page has been validated, you can now delete it:

File f = new File(filePath); f.delete();

After that, send the results back to the Web browser with these lines:

CharArrayWriter caw = new CharArrayWriter(); caw.write(contents); response.setContentLength(caw.toString().length()); out.write(caw.toString());

Almost There, Just Configure the Filter
You are not quite done yet. You still need to deploy the filter so your servlet container knows you want to use it. One of the nice things about filters is that you do not have to modify any existing code in order to introduce them into your Web app. All you need to do is add a <filter> element into the web.xml file:

<filter> <filter-name>HTMLValidatorFilterLite</filter-name> <filter-class>org.twine.filter.HTMLValidatorFilterLite</filter-class> </filter>

Then you need to set up a mapping in your web.xml file that tells the servlet container when to use this filter. To set the filter up to validate all JSP files, you use the following code:

<filter-mapping> <filter-name>HTMLValidatorFilterLite</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>

Be careful with these mappings. The W3C Validation Service will make requests to your server as well. So if you set up the filter to catch all requests (<url-pattern>*</url-pattern>), the W3C Validation Service's request will trigger another invocation of the filter, followed by another request, and so on. The file written to disk has a default extension of '.htm', so any <url-pattern> you use should avoid that one.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date