views:

164

answers:

1

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?

+1  A: 

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.

David Rabinowitz
Turns out I can use them directly on my servlets. Sorry for not trying before asking. But, I don't think this works if used on another container. Thanks for the link, David.
Ramon Marco Navarro