One of the servlet in my web app takes parameters in the form www.service.com/servletname/parameter1/parameter2
.
The first parameter is required and the second parameter is an integer. I've made some code to validate the parameters but it looks really really messy. Is there a nicer and cleaner way I can write the following:
Thanks :)
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType(CONTENT_TYPE);
PrintWriter out = resp.getWriter();
int maximumResults = defaultMaxResults;
// check that parameters were provided
if (req.getPathInfo() == null)
{
out.println( JSONHelper.writeStatus(false, Status.TOO_FEW_ARGS.getMessage()));
return;
}
// seperate parameters
String[] parameters = req.getPathInfo().split(PARAMETER_SEPERATOR);
if (parameters.length < MIN_NUM_PARAMETERS)
{
out.println( JSONHelper.writeStatus(false, Status.TOO_FEW_ARGS.getMessage()));
return;
}
// validate parameters
if (parameters.length > MIN_NUM_PARAMETERS)
{
try
{
maximumResults = Integer.parseInt(parameters[2]);
} catch (NumberFormatException nfe)
{
out.println( JSONHelper.writeStatus(false, Status.INVALID_MAX_RESULTS.getMessage()));
return;
}
}