0

This is my program for getting an ArrayList which contain bean-object from the servlet and showing its content in jsp using jstl tag . After i run the program no result is shown. Please look to my code and help me find the error. I am new to jstl.

servlet code

package com.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.servlet.mybean;
@WebServlet("/Bean")
public class Bean extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Bean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        mybean mybean_obj = new mybean();
        ArrayList<mybean> obj=new ArrayList<mybean>();

        mybean_obj.id=100;
        mybean_obj.name="vishal";
        mybean_obj.roll=1225;

        mybean mybean_obj1 = new mybean();

        mybean_obj1.id=101;
        mybean_obj1.name="anand";
        mybean_obj1.roll=1226;

        mybean mybean_obj2 = new mybean();

        mybean_obj2.id=102;
        mybean_obj2.name="google";
        mybean_obj2.roll=1557;

        mybean mybean_obj3 = new mybean();

        mybean_obj3.id=103;
        mybean_obj3.name="yahoo";
        mybean_obj3.roll=1558;

        obj.add(mybean_obj);
        obj.add(mybean_obj1);
        obj.add(mybean_obj2);
        obj.add(mybean_obj3);
        HttpSession session = request.getSession();
        session.setAttribute("mybean", obj);
        request.setAttribute("mybean", obj);
        RequestDispatcher fwd = request.getRequestDispatcher("encodeUrl.jsp");
        fwd.forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

jsp page

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Encode URL </title>
</head>
<body>
 <c:forEach var="start" items="${sessionScope.mybean}">
      ${sessionScope.start.id}<br/>${sessionScope.start.roll }<br/>${sessiontScope.start.name }<hr/>
  </c:forEach>
</body>
</html>

i am calling servlet from home page and from servlet the request is forwarded to final jsp page whose code is above.

1 Answer 1

2

Rewrite your code like this :

<c:forEach var="start" items="${sessionScope.mybean}">
      ${start.id}<br/>${start.roll }<br/>${start.name }<hr/>
  </c:forEach>

the var="start" creates a local variable named start representing the current item. You can use that within the forEach statement. (there is no need to prefix it with sessionScope as there is no sessionScope based bean called start)

Some other tips for your code :

MyBean bean = new MyBean(); // classes start with an uppercase and use CamelCasing.
List<mybean> beans=new ArrayList<MyBean>(); // program against the list interface.
bean.setId(100); // use accessor methods instead of accessing variables directly.
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, for clearing my doubt :) and I had written the code to just understand jstl on fly so I didn't follow the java rules and beans getter and setter methods.
I discovered that there is need of get and set methods in bean class and it can't be skipped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.