1

How to link a JSP ,servlet and a Java view bean? I donot want to have any scriplet in JSP. How can i ask my JSP to invoke servlet to set the Implicit request and response objects and then display the properties from view bean on the jsp.

I need to pass request and response objects from jsp to servlet and then i want to display the properties defined in view bean to a JSP page.

JSP -> Servlet -> View Bean -> JSP

How to achieve this?

user1602657
  • 141
  • 2
  • 6
  • 8

1 Answers1

4

No need to pass request and response to the bean/model because your model classes are instantiated either in JSPs or Servlets.

You have to choose servlet-centric approach.

ViewBean.java


package in.abc.model;

public class Employee{
     private Integer id;
     private String  name;
     public Employee() { id=0; name="";}
     public Employee(Integer id, String name) { this.id=id; this.name=name;}
     public Integer getId()  { return id;}
     public String getName() { return name;}
     public void setId(Integer id) { this.id=id;}
     public void setName(String name) { this.name=name;}
}

ViewServlet.java


package in.abc.servlets;

@WebServlet(name = "ViewServlet", urlPatterns = {"/view"})
public class ViewServlet extends HttpServlet{
  public doGet(HttpServletRequest request,
               HttpServletResponse response) 
                 throws ServletException, IOException {

         //Instantiate the model 
         Employee emp=new Employee(10,"Mr.A");

         //Insert "model" object to request
         request.setAttribute("emp",emp);

         //forward the request to view.jsp
         request.getRequestDispatcher("/view.jsp").
                  forward(request,response);
  }

 public doPost(HttpServletRequest request,
               HttpServletResponse response) 
                 throws ServletException, IOException {

         //Instantiate the model 
         Employee emp=new Employee();

         //Read the request
         try{
             emp.setId(Integer.parseInt(request.getParameter("id"));
         }catch(Exception ex) {}
         emp.setName(request.getParameter("name"));

         //Insert "model" object to request
         request.setAttribute("emp",emp);

         //forward the request to view.jsp
         request.getRequestDispatcher("/view.jsp").
                  forward(request,response);
  }
}

view.jsp


<h3>Employee info</h3>
<p>ID : ${emp.id}</p>
<p>Name : ${emp.name}</p>

<form method="post" action="view">
   <br/> <input type="text" name="id"/> 
   <br/> <input type="text" name="name"/>
   <br/> <input type="submit"/>
</form>

index.jsp


<h1>
  <a href="view">View Employee details</a>
</h1>
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Thanks for the reply. But in servlet code, request.getRequestDispatcher("/view.jsp"). forward(request,response); code is giving me the Cannot forward. Response already committed. error. I am calling the servlet in the jsp by – user1602657 Aug 20 '12 at 05:33
  • @user1602657 - You have to *edit* your question and include code of .jsps and servlets. – KV Prajapati Aug 20 '12 at 06:26