2

I'm implementing a JSP which expects a few parameters which have to be validated before running the jsp.

  1. Suggestion: Validate the parameters inside the JSP using Taglibraries
  2. Suggestion: Pre-parse the Parameters in a Filter

What do you think?

Edit

Thank you for the good answers, but I was wondering what would be the best practice in case you are offering a service like google chart API where you can't expect that the parameters are checked by a form before they are sent. example: https://chart.googleapis.com/chart?cht=&chd=&chs=&...additional_parameters...

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Zounadire
  • 1,496
  • 2
  • 18
  • 38

2 Answers2

9

None of both are good approaches. Controller/business logic doesn't belong in a JSP (tag). A filter is almost good, but it's not specific enough. This job should be done by a servlet. You're submitting the form to a servlet to postprocess it, right? It sounds like that you're not already doing that, the answer would otherwise have been pretty straightforward.

In our servlets tag wiki page you can find a hello world example of a good approach of using a JSP with a Servlet to postprocess a form submit. Here's an extract of relevance:

<input id="name" name="name" value="${fn:escapeXml(param.name)}">
<span class="error">${messages.name}</span>

with

String name = request.getParameter("name");
if (name == null || name.trim().isEmpty()) {
    messages.put("name", "Please enter name");
}

// ...

request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);

Further, there exist MVC frameworks which removes all the boilerplate (duplicated/repeated) servlet code for this kind of use cases, such as JSF, Spring MVC, Wicket, Stripes, Struts2, etc. With for example JSF it look just something like this:

<h:inputText id="name" value="#{bean.name}" required="true" requiredMessage="Please enter name" />
<h:message for="name" />

That's all. The JSF's FacesServlet controller servlet will validate if it's been filled in and display a (configureable) message at the given location, without any need for custom Java code. You could even move it to the model, JSF has transparent support for JSR303 bean validation as well. E.g.

<h:inputText id="name" value="#{bean.name}" />
<h:message for="name" />

with

@NotNull(message="Please enter name")
private String name;

Update as per your edit:

Thank you for the good answers, but I was wondering what would be the best practice in case you are offering a service like google chart API where you can't expect that the parameters are checked by a form before they are sent. example: https://chart.googleapis.com/chart?cht=&chd=&chs=&...additional_parameters...

Just use a servlet the same way. The only difference is that you've to implement the job in doGet() instead of doPost() and if necessary return HTTP 400 on an error :) Once again, check our servlets tag wiki page to understand their purpose better. Or to go a step further, use a webservice framework instead, such as JAX-WS or JAX-RS which do this job transparently like a MVC framework does for HTML pages.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Very good answer, just checked out the wiki and it helped a lot. also liked many of your answers... But one thing confuses me a little. Aren't web-service frameworks like jax-ws or jax-rs supposed to deliver SOAP,JSON or XML? Can they also deliver html? ps sorry about the edit in the comment hit return by accident – Zounadire Aug 18 '11 at 16:35
  • They can also deliver HTML, but that's not their sole intent. You mentioned Google chart API as an example. That's a webservice which returns an image. – BalusC Aug 18 '11 at 16:45
6

Use an MVC Framework (Spring MVC, Stripes, Struts 2 etc.) and validate the parameters in the controller class. Every MVC framework supports parameter validation, and you get a clean separation of concerns.

Example: Spring MVC automatically registers JSR-303-style parameter Validation (if you have a JSR-303 provider, e.g. Hibernate-Validator, on the classpath) when using mvc:annotation-driven

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588