0

HI I am not using any MVC .. But I would like to get the same functionality in my JSP Servlets application.

I have jsp having some text fields of date, string, number..etc.. I have created a form bean class with the names of text fields in my jsp with getters and setters like below..

JSP (Edited)

   <form name="myForm" action="myAction" method="post">

      <jsp:useBean id="jspBean" class="com.package.my.BeanClass" scope="request" />
       <jsp:setProperty prperty="*" name="jspBean" />
    //Below some text fields and date fileds 
     <table>
       <tr>
          <td>
               UserName :
           </td>
           <td>
               <input type="text" value="${myBean.userName}" name="userName"/>
          </td>

        </tr>
          <td>
               PassWord :
           </td>
           <td>
               <input type="text" value="${myBean.passWord}" name="passWord"/>
          </td>

        </tr>
          <td>
               Date : 
           </td>
           <td>
               <input type="text" value="${myBean.date}" name="date"/>
             // This text field is with JQUery Date picker
          </td>

        </tr>
        <tr>
         <td>
           <input type="submit" value="login"  />
          </td
        </tr>
     </table>
   </form>   

IN My servlet

   // I Can get those text fields as Parameters i.e

       req.getParameter("userName")

    //And I cant get that bean Object `jspBean`
    // I am trying to get that as below
      sysout(req.getAttribute("jspBean"));//It prints null   

     // So I create a plain bean object and I am setting the data here 
        MyBean bean = new MyBean();
         // bean.setUserName();
          // I am setting all these fields whaterver needed
        req.setAttribute("myBean",bean);  

problem

1) I can get the bean object and its data from Servlet to JSP usinf EL I am able to do.. What I want is I want send form data(all fields) as BeanObject instead of Parameters.. That I am not able to acheive,

How to achieve this.. Please help

Thanks in advance./

Rookie007
  • 1,229
  • 2
  • 18
  • 50
  • You must put your bean in a package. Put your class in a folder named something like mypackage and add the line package mypackage; as the first line in your MyBeanClass.java file. Also use class="mypackage.MyBeanClass" in your useBean tag. Finally access with ${beanId.field1} – rickz Feb 06 '15 at 17:26
  • @rickz I have specified along with package only what my problem is I am unable to get that object in my action class. – Rookie007 Feb 08 '15 at 03:17
  • Are you able to access your bean's fields in your JSP? – rickz Feb 08 '15 at 04:23
  • @rickz no I cant access... :( – Rookie007 Feb 09 '15 at 02:29
  • @rickz Now I can access as I removed `` and just Using `EL` to get the form data from servlet to JSP.. Now able to do.. but. How to achieve communication from JSP to Servlet using Bean class ? – Rookie007 Feb 09 '15 at 03:31
  • Please show us your JSP. – rickz Feb 09 '15 at 05:53
  • When you click on submit button in your JSP, you are creating a new request. Did you try changing the scope of your bean to session? – rickz Feb 09 '15 at 16:09

2 Answers2

0

You might want to specify the scope.

<jsp:useBean id="beanId" class="MyBeanClass" scope="request" />

Then in Servlet:

MyBeanClass counter = (MyBeanClass)request.getAttribute("beanId");

Also, to use ${beanId.getField1()} you have to Set the Expression Language to true in your JSP. Refer to this link for using Expression Language.

Community
  • 1
  • 1
Sid
  • 4,893
  • 14
  • 55
  • 110
  • Sorry to mention in my question I have set scope to `request` and tried but no result.. And for EL is working fine in my JSP as other EL used are working fine in same JSP.. – Rookie007 Feb 06 '15 at 07:49
0

The problem is, in your jsp there is no binding of your html form with java bean.

<input type="text" value="${myBean.userName}" name="userName"/>

Hence in the servlet, you don't get the values from the request attribute. With simple JSP and servlet, this cannot be done.

You may possibly use Spring MVC framework to achieve this, where you can bind the java bean with the spring form. Or you may go for struts framework.

If you still want to go for servlets, then may have this method in you servlet to build the java bean using request.

public MyBean valueOf(HttpServletRequest req) {
  MyBean bean = new MyBean();
  bean.setUserName(req.getParameter("userName"));
  bean.setPassword(req.getParameter("passWord"));
  ...
  return bean;
}

P.S. This is a work around, and not binding for form with java bean

  • I want to use JSP Servlets only. If possible plz tell me how to bind my form fields to FormBean object ..? And where I need to have that method ..? – Rookie007 Feb 09 '15 at 07:30