tira
tira

Reputation: 25

passing value from servlet to jsp page

EditDeleteServlet - which this is my servlet of listing all the students. Here is the code:

Student sbean = new Student();
            EditDeleteDAO sDAO = new EditDeleteDAO();

            String command = request.getParameter("command");
            PrintWriter out = response.getWriter();
            out.print(command);

            try{
                if(command.equals("Submit")) 
                {
                    String id=request.getParameter("id");
                    String studName=request.getParameter("studName");
                    String icNum=request.getParameter("icNum");
                    String matrixID=request.getParameter("matrixID");
                    String contactNum=request.getParameter("contactNum");
                    String email=request.getParameter("email");
                    String course=request.getParameter("course");
                    sbean.setId(Integer.parseInt(id));
                    sbean.setStudName(studName);
                    sbean.setICNum(icNum);
                    sbean.setMatrixID(Integer.parseInt(matrixID));
                    sbean.setContactNum(Integer.parseInt(contactNum));
                    sbean.setEmail(email);
                    sbean.setCourse(course);
                    EditDeleteDAO.insertDetails(sbean);
                    List<Student> list = sDAO.getAllDetails();
                    if(list!=null)
                    {
                        request.setAttribute("list",list);
                        RequestDispatcher rd = request.getRequestDispatcher("/studListing.jsp");
                        rd.forward(request, response);
                    }
                }

studListing.jsp - this is my jsp page where I want to display my data here

list = request.getAttribute("list"); %>

Upvotes: 0

Views: 447

Answers (3)

prmotwani
prmotwani

Reputation: 111

Essentially JSP & Servlets share the same space, actually a JSP gets compiled into Servlet at the backend & they share the same HttpServletRequest and HttpServletResponse, so you can set the value in servlet in request and fetch in JSP using the same request object.

e.g. inside Servlet, request.setAttribute("list",list);

inside JSP, request.getAttribute("list");

Alternatively you could use session object as well to share this across your HttpSession.

Upvotes: 0

gbandres
gbandres

Reputation: 911

You have to use request.setAttribute() to add pairs of key-value that your jsp page can use. In your code:

request.setAttribute("list", list);

Then you can retrieve the attribute in your jsp like this:

<% List<Student> list = request.getAttribute("list"); %> // Note that the name of the attribute is "list"

But if you are going to iterate throught the list, it is recommended that you use EL:

<c:forEach items="${list}" var="listItem">
    <c:out value="${listItem.eid}"/>
</c:forEach>

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

You have added to your request with the key named list

request.setAttribute("list",list);

so you have to retrieve it using this name in your JSP

Also use EL not java in you JSP

<c:foreach items="${list}" var="student">
    ${student.studName}
<c:forEach>

Upvotes: 4

Related Questions