jeudi 3 mai 2018

Java EE Servlets - best practice to bind form data to bean

This semester I have a course that requires me to write a Java web application, a small store, basically. The requirements are to use the MVC pattern with Java EE and glassfish as application server, servlets as the controller. Zero frameworks allowed, JSP + plain JS for the frontend.

If I read correctly so far, there is no way to automatically have a HttpServlet bind form data to a bean. So there are these three options

  • Simply reading every single parameter line by line by calling getParameter(String) on the request, then manually creating the new bean with that. Creates a lot of boilerplate code, especially for beans with a lot of fields.
  • Using <jsp:useBean>, so basically delegating the binding into the JSP. It's said this is kind of "hackish" though and doesn't really follow the MVC pattern.
  • Using reflection and getParameterMap(). I could easily write a small utility class that does the binding then. But now we have reflection, which is generally considered as bad because it's supposedly slow.

Are there any further options? Which one is preferred? How did Spring and other frameworks solve this, do they use reflection?




Aucun commentaire:

Enregistrer un commentaire