0

I am running a very first foreach loop in jsp. I dont know what the problem is. Its not showing any errors as well.

This is my java code:

package pack1;
    import java.util.ArrayList;

    public class jstlClass

    {
        String emp_name;
        String emp_id;
        String emp_dept;


        public String getEmp_name()

        {
            return emp_name;
        }
        public void setEmp_name(String emp_name)

        {
            this.emp_name = emp_name;
        }
        public String getEmp_id()
        {
            return emp_id;
        }

        public void setEmp_id(String emp_id)
        {
            this.emp_id = emp_id;
        }

        public String getEmp_dept() 
        {
            return emp_dept;
        }

        public void setEmp_dept(String emp_dept) 
        {
            this.emp_dept = emp_dept;
        }

        public static void main(String[] gs)
        {
            ArrayList li=new ArrayList();

            jstlClass emp=new jstlClass();
            emp.setEmp_id("20");
            emp.setEmp_name("vishnu");
            emp.setEmp_dept("it");
            li.add(emp);

            jstlClass j=new jstlClass();
            j.setEmp_id("21");
            j.setEmp_name("prem");
            j.setEmp_dept("csc");
            li.add(j);

        }   
    }

This is my jsp code:

<html>
</head>
<body>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="emp1" class="pack1.jstlClass" scope="session"/>
<%@ page import="java.util.*" %>

<% 
<c:forEach var="li" items="${sessionScope.li}">

<c:out value="${li.emp_id}"/>
<c:out value="${li.emp_name}"/>
<c:out value="${li.emp_dept}"/>

</c:forEach>

%>

</body>
</html>

I have tried for so long but still it shows the same output. I am using Eclipse and Apache Tomcat server. I even tried running it in google chrome server, but no changes. I have put that "Hello world" in there and its displaying that, but not entering the foreach loop. This is my first foreach loop program and i have absolutely no idea what is going wrong. Help please!

2 Answers 2

2

This is not at all right... You are doing it worng... You will have to read servlet jsp before doing this...

for your project create a servlet and pass data from servlet to jsp and then only jsp can access data from there

Try servlet jsp examples first.. Example servlet

public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                      //right your code here to get data from jstlClass and pass it to the jsp in request
      request.setAttribute("","");
            request.getRequestDispatcher("/FirstJSP.jsp").forward(request, response);                
        } 

protected void doget(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        }       
        }
    }
}

and then you can access the parameters there

Try this example http://www.java-samples.com/showtutorial.php?tutorialid=552

Sign up to request clarification or add additional context in comments.

3 Comments

it wont change until you learn the right way... please read about servlet and jsp
check the link in the edited ans and try the example given there it will help you
@Varsha it is possible to get the values from the class , just like obj.methodName . it is valid from servlet 3.0
0

you have to set properties of your class in the JSP.

reason is main method is not called in j2ee prespective:

Try This

<jsp:useBean id="emp1" class="pack1.jstlClass" scope="session"/>
 <jsp:setProperty name="emp1" property="emp_id" value="20"/> 
 <jsp:setProperty name="emp1" property="emp_name" value="vishnu"/> 
 <jsp:setProperty name="emp1" property="emp_dept" value="it"/> 

access using tag:

  <jsp:getProperty name="emp1" property="emp_name"/>

2 Comments

set properties to what?
I want to access the properties i have set in my java code in my jsp file. I want to set the properties in java and display on browser using jsp..

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.