1

If I have a bean that contains the following code:

private String method;

public String getMethod() {
    return method;
}

public void setMethod(String method) {
    this.method = method;
}

Is it necessary to:

request.setAttribute("method", method );

For every variable that I want to be visible from the JSP?

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • 1
    If your bean is accessible in your JSP, you don't need to set each and every field of it to the request. `${beanObj.method}` would work. – Rahul Jan 31 '14 at 12:26

3 Answers3

1

If method attribute is not set in request the evaluated ${method} expression will be null in jsp. If you need some value then you have to set it to that value.

In your servlert do post method;

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException 
{       
 String formParameter = req.getParameter("someParameterName");
 //Your logic dependen on form parameter

 FormObject fo = new FormObject();
 fo.setMethod("new value"); 
 req.setAttribute("formObject", fo);
 req.getRequestDispatcher("/WEB-INF/yourJspPage.jsp").forward(req, res);
}

You java object:

public class FormObject{
      String method;

      public String getMethod(){
         return method;
      }

      public void setMethod(String method){
         return this.method = method;
      }
    }

in your yourJspPage.jsp:

<div>${fo.method}</div>

P.S. I haven't tried this example but the idea should be clear. you can search for jsp + servlet tutorial to understand what you are doing. There is similar what you want : enter link description here

session is similar to request just attributes added to this object last longer (different scope). But I think you should read more documentation and tutorials before asking help for every step.

Community
  • 1
  • 1
jonasnas
  • 3,540
  • 1
  • 23
  • 32
  • If you need to access param itself you can use ${param.paramName}. But if you want to access a modified value on the servlet you have to set attribute on the servlet side (or you could use session attribute). But like others mentioned you can just set the attribute of whole object and then access the properties of the object with the . (dot) expression on the object – jonasnas Jan 31 '14 at 12:33
  • If it is a request attribute, this attribute is no longer available once the request is served. So your servlet sets request attribute, jsp renders it and attribute goes out of scope. Now the next request is created after form submission with no attributes, so you need to set them yourself. If you need attributes last longer you could use different scope than request (session for example) – jonasnas Jan 31 '14 at 12:40
  • Other guys showed you. If you have an object with properties param1 and param2. you could get your request params in doPost. set formObject.param1 and formObject.param2 in you doPost method and then use request.setAttribute("formObject", formObject ); In jsp you could use ${formObject.param1} – jonasnas Jan 31 '14 at 12:48
  • You have to have public getters for your properties to make them accessible with . expression. So you must have public String getMethod(){ return method} in your form object (which you set as attribute) – jonasnas Jan 31 '14 at 13:10
  • It doesn't allow me to edit (or at least I don't know how). You don't need you just need in your doPost method of the servlet to create a new object (not controller) and set that object with the properties you want. after adding that object to request, to access it in jsp you just use ${attributeName.propertyName} and it should show you the value. Just make sure you are accessing the object you added to the request and that the property of that object has a valid getter method – jonasnas Jan 31 '14 at 13:22
  • Have a look at this tutorial. http://www.shaunabram.com/helloworld-jsp-servlet It sets attribute to the request and forwards processing to jsp. The same request object is accessed in jsp since it is like in a chain of request handling. Servlet just sets some attributes depending on some logig. Jsp then access those attributes and prints them together with some html – jonasnas Jan 31 '14 at 13:31
1

For every variable that I want to be visible from the JSP?

No. You just have to set an instance of the bean as request attribute. With that available in JSP page, you can access the properties of that bean, with EL - ${beanInstance.method}.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

If method attribute is not set in request the evaluated ${method} expression will be null in jsp. If you need some value then you have to set it to that value.


There are 3 ways of doing this:

  1. With a session

    request.getSession().setAttribute("method", this); and <c:out value="${mycontroller.method}"/>

  2. Set a single attribute

    request.setAttribute("method", method); and <c:out value="${method}"/>

  3. Set all attributes in the bean simultaneously by assigning the object to the bean

    request.setAttribute("mycontroller", this); and <c:out value="${mycontroller.method}"/>