Search code examples
jspservletsjsp-tagsjavabeans

JavaBean Introspection in Servlets [Tomcat]


What is the equivalent "servlet code" for this:

<jsp:useBean id="user" class="beans.UserBean" scope="session"/>
<jsp:setProperty name="user" property="*"/>

Tomcat translates this to:

beans.UserBean user = null;
synchronized (session) {
    user = (beans.UserBean) _jspx_page_context.getAttribute("user", PageContext.SESSION_SCOPE);
    if (user == null) {
        user = new beans.UserBean();
        _jspx_page_context.setAttribute("user", user, PageContext.SESSION_SCOPE);
    }
}

org.apache.jasper.runtime.JspRuntimeLibrary.introspect(_jspx_page_context.findAttribute("user"), request);

Is there no other way of easily doing this?


Solution

  • Each jsp compiler will yield different results. Using the commons beanutils it will look something of a sort of

    for(Enumeration pnames = request.getParameterNames();pnames.hasMoreElements();) {
      String name = pnames.nextElement();
      BeanUtils.setProperty(bean,name,request.getParameter(name));
    }
    

    Notice that it does not handle arrays and other special cases.

    Also, this is why usually you use a web framework such as Spring, Struts, Wicket, etc - it just a boiler plate code, and you have to code your validations anyway.