Search code examples
servletsrequestvelocity

I've been working with velocity recently, but I made a big mess, what should I do?


I got this program, that I made: http://www.2shared.com/file/73q6K9Yy/testv.html The Login.java is the servlet, that should use the login.vm template, and modify it. It doesn't do anything, and I don't understand why. In the function:

public Template handleRequest(HttpServletRequest request,HttpServletResponse response, Context ctx )

If I put:

 error= (String) request.getAttribute("error");
 ctx.put("error", error);

It doesn't do anything. IF I put:

 error= request.getAttribute("error").toString();
 ctx.put("error", error);

I get a Java null pointer exception. BUT if I put:

 ctx.put("error","this is a string");

It changes the variable of the template login.vm into that string. Why doesn't it get the Attribute from the request, what I'm a doing wrong.

PS: If I press the Register button, it should redirect me to the "/reg.jsp" page. I made this only to see if it works, and it doesn't. Made the same program in MVC, with jsp's but I need to make it with velocity, for my internship.


Solution

  • My best guess is that there is no attribute under the "error" key in your request. It has nothing to do with Velocity itself. If you check the line number the null exception occurs at, it should be the first line of your code excerpt.

    Note that in a web context, you should use the VelocityViewServlet, from the velocity-tools subproject, rather than the deprecated VelocityServlet that will disappear in next versions.