📜  getAttribute() – 将数据从服务器传递到 JSP

📅  最后修改于: 2021-10-29 04:20:51             🧑  作者: Mango

假设在服务器端已经创建了一些数据,现在为了在 JSP 页面中传递这些信息,需要request.getAttribute()方法。这实际上区分了 getAttribute() 和 getParameter() 方法。后者用于将客户端数据传递给 JSP。

执行
1)首先在服务端创建数据并传递给一个JSP。这里将创建 servlet 中的学生对象列表,并使用 setAttribute() 将其传递给 JSP。
2)接下来,JSP 将使用 getAttribute() 检索发送的数据。
3)最后,JSP 将以表格形式显示检索到的数据。

用于创建数据并将其分派到 JSP 的 Servlet:StudentServlet。Java

package saagnik;
  
import java.io.*;
import java.util.ArrayList;
import javax.servlet.*;
import javax.servlet.http.*;
  
public class StudentServlet extends HttpServlet {
  
  protected void processRequest(HttpServletRequest request,
                                HttpServletResponse response)
    throws ServletException, IOException
    {
     response.setContentType("text/html;charset=UTF-8");
     try (PrintWriter out = response.getWriter()) {
       out.println("");
       out.println("");
       out.println("");
       out.println("Servlet StudentServlet");
       out.println("");
       out.println("");
  
       // List to hold Student objects
       ArrayList std = new ArrayList();
  
       // Adding members to the list. Here we are 
       // using the parameterized constructor of 
       // class "Student.java"
       std.add(new Student("Roxy Willard", 22, "B.D.S"));
       std.add(new Student("Todd Lanz", 22, "B.Tech"));
       std.add(new Student("Varlene Lade", 21, "B.B.A"));
       std.add(new Student("Julio Fairley", 22, "B.Tech"));
       std.add(new Student("Helena Carlow", 24, "M.B.B.S"));
  
       // Setting the attribute of the request object
       // which will be later fetched by a JSP page
         request.setAttribute("data", std);
  
       // Creating a RequestDispatcher object to dispatch
       // the request the request to another resource
         RequestDispatcher rd = 
             request.getRequestDispatcher("stdlist.jsp");
  
       // The request will be forwarded to the resource 
       // specified, here the resource is a JSP named,
       // "stdlist.jsp"
          rd.forward(request, response);
            out.println("");
            out.println("");
        }
    }
    /** Following methods are used to handle
        requests coming from the Http protocol request.
        Inspects method of HttpMethod type
        and if the request is a POST, the doPost() 
        method will be called or if it is a GET,
        the doGet() method will be called. 
    **/
    @Override
    protected void doGet(HttpServletRequest request,
                        HttpServletResponse response)
        throws ServletException, IOException
    {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request,
                        HttpServletResponse response)
        throws ServletException, IOException
    {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }
}

JSP 检索由 servlet “StudentServlet.”发送的数据。 Java”并显示它:stdlist.jsp

<%@page import="saagnik.Student"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


  
   
   Student List
  
  
      

Displaying Student List

                                                                    <%-- Fetching the attributes of the request object              which was previously set by the servlet                "StudentServlet.java"         --%>          <%ArrayList std =              (ArrayList)request.getAttribute("data");         for(Student s:std){%>         <%-- Arranging data in tabular form         --%>                                                                                          <%}%>         
Student NameStudent AgeCourse Undertaken
<%=s.getName()%><%=s.getAge()%><%=s.getCrs()%>
          
    

学生。 Java类

package saagnik;
  
public class Student {
    private int age;
    private String name;
    private String crs;
    // Parameterized Constructor to set Student
    // name, age, course enrolled in.
    public Student(String n, int a, String c)
    {
        this.name = n;
        this.age = a;
        this.crs = c;
    }
    // Setter Methods to set table data to be
    // displayed
    public String getName() { return name; }
    public int getAge() { return age; }
    public String getCrs() { return crs; }
}

运行应用程序
1)运行 servlet “StudentServlet. Java”,它将学生数据传递给JSP页面“stdlist.jsp”。
2) JSP 页面“stdlist.jsp”检索数据并以表格形式显示。

注意:整个应用程序已在 NetBeans IDE 8.1 上开发和测试

输出
显示学生数据:stdlist.jsp
显示学生数据