28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Validating Input in Java<br />

Code-Level <strong>Defense</strong>s • Chapter 8 353<br />

In Java, input validation support is specific to the framework being used. To demonstrate<br />

input validation in Java, we will look at how a common framework for building Web<br />

applications in Java, Java Server Faces (JSF), provides support for input validation. For this<br />

purpose, the best way to implement input validation is to define an input validation class that<br />

implements the javax.faces.validator.Validator interface. Refer for the following code snippet for<br />

an example of validating a username in JSF:<br />

public class UsernameValidator implements Validator {<br />

public void validate(FacesContext facesContext,<br />

UIComponent uIComponent, Object value) throws ValidatorException<br />

{<br />

}<br />

//Get supplied username <strong>and</strong> cast to a String<br />

String username = (String)value;<br />

//Set up regular expression<br />

Pattern p = Pattern.compile("^[a-zA-Z]{8,12}$");<br />

//Match username<br />

Matcher m = p.matcher(username);<br />

if (!matchFound) {<br />

}<br />

FacesMessage message = new FacesMessage();<br />

message.setDetail("Not valid – it must be 8–12 letter only");<br />

message.setSummary("Username not valid");<br />

message.setSeverity(FacesMessage.SEVERITY_ERROR);<br />

throw new ValidatorException(message);<br />

And the following will need to be added to the faces-config.xml file in order to enable<br />

the above validator:<br />

<br />

namespace.UsernameValidator<br />

namespace.package.UsernameValidator<br />

<br />

You can then refer to this in the related JSP file as follows:<br />

<br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!