getAttribute() – Passing data from Server to JSP
Suppose some data at the Server side has been created and now in order to pass that information in a JSP page, there is a need of request.getAttribute() method. This, in fact differentiates the getAttribute() and getParameter() methods. The latter is used to pass Client side data to a JSP.
1) First create data at the server side and pass it to a JSP. Here a list of student objects in a servlet will be created and pass it to a JSP using setAttribute().
2) Next, the JSP will retrieve the sent data using getAttribute().
3) Finally, the JSP will display the data retrieved, in a tabular form.
Servlet to create data and dispatch it to a JSP : StudentServlet.java
JSP to retrieve data sent by servlet “StudentServlet.java” and display it : stdlist.jsp
The Student.java class
Running the application
1) Run the servlet “StudentServlet.java”, which will pass student data to JSP page “stdlist.jsp”.
2) The JSP page “stdlist.jsp” retrieves the data and displays it in a tabular form.
Note : Entire application has been developed and tested on NetBeans IDE 8.1
Displaying Student Data : stdlist.jsp
Please Login to comment...