Following is the servlet class that sets the name by invoking a method on the object of a bean class and then forwards to a jsp page .
package BeanTesters;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class Controller extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
Bean bean = new Bean();
bean.setName("Suhail Gupta");
//request.setAttribute("name", bean);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
}
And this is the bean class :
package BeanTesters;
public class Bean {
private String name = null;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
following is a jsp snippet that tries to display the name set by the servlet:
<jsp:useBean id="namebean" class="BeanTesters.Bean" scope="request" />
Person created by the Servlet : <jsp:getProperty name="namebean" property="name" />
The result i get is : Person created by the Servlet : null Why do i get a null value ?