【问题标题】:is there a way to return a list from servlet to html page [duplicate]有没有办法将列表从 servlet 返回到 html 页面 [重复]
【发布时间】:2014-05-03 21:14:39
【问题描述】:

我想从 html 页面向 servlet 发送请求,然后在 java servlet 中创建一个列表,然后我想将此列表返回到同一个 html 页面。

【问题讨论】:

  • 不要将 HTML 放入 Java 代码中。不要将 Java 代码放在 HTML 中。
  • 不要在Servlet中处理状态,会导致线程安全问题。
  • @SotiriosDelimanolis 我不会说您永远不必将 HTML 代码放入您的 servlet 中。相反,我会说不要从 servlet 端处理整个 HTML 页面。
  • “空错误异常”?从未听说过其中之一。也许您可以给我们空错误异常的堆栈跟踪,并指出它所指的行号?
  • @LuiggiMendoza 对,如果您要编写 HTML,请在专用组件中进行。

标签: java html servlets


【解决方案1】:

问题是你从来没有初始化你的HttpSession s 变量:

@WebServlet(urlPatterns = {"/ShowPersonServlet"})
public class ShowPersonServlet extends HttpServlet {

    HttpSession s; //null by default
    //...

    protected void processRequest(...) {
        //...
        //since never initialized, s is null
        user.add((Person) s.getAttribute("person"));
    }

    @Override
    protected void doGet(...) {
        //...
        //since never initialized, s is null
        s.setAttribute("person",person);
    }
}

Make-it-work解决方案:设置s的值

s = request.getSession();

现实世界解决方案:

  • 删除 Servlet 中的所有字段,切勿尝试处理 servlet 中的状态,除非它们是由容器管理的资源,例如 EJB。
  • HttpSession s 的范围更改为每个方法的本地范围。此外,将其名称从 s 更改为 session 或更有用的名称。
  • 将您的 HTML 代码移动到处理视图详细信息的组件(如 JSP)中,然后执行到视图的转发。

因此,您的代码将如下所示:

@WebServlet(urlPatterns = {"/ShowPersonServlet"})
public class ShowPersonServlet extends HttpServlet {
    /*
    HttpSession s ; //moved as local variable
    Person person = new Person(); //moved as local variable
    private List<Person> user = new ArrayList<Person>(); //not sure where you want to store this
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        List<Person> personList = (List<Person>) session.getAttribute("personList");
        if (personList == null) {
            personList = new ArrayList<>();
            session.setAttribute("personList", personList);
        }
        personList.add((Person) session.getAttribute("person"));
        /*
        try (PrintWriter out = response.getWriter()) {
            //removed to shorten this answer
        }
        */
        request.getRequestDispatcher("/showPerson.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        Person person = new Person();
        person.setKey(request.getParameter("txt_Key"));
        person.setFirstName(request.getParameter("txt_firstName"));
        person.setLastName(request.getParameter("txt_lastName"));
        processRequest(request, response);
        HttpSession session = request.getSession();
        session.setAttribute("person",person);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        processRequest(request, response);
    }
}

更多信息:

【讨论】:

  • 但是如何将列表返回到 html 页面以在那里打印呢?我应该在同一个 html 页面中打印所有用户添加的内容。
  • 这里出现错误:if (personList == null) { personList = new List(); session.setAttribute("personList", personList);列表是抽象的,无法实例化。
  • 我还有错误:** NullPointerException()**
  • @user3459148 将new List&lt;&gt; 错误修复为new ArrayList&lt;&gt;,这是我的错误(快速打字)。
【解决方案2】:

var s 的值未定义。它为空,所以使用s.getAttribute("str") 会抛出异常。

HttpSession s ;

s = request.getSession(false);

【讨论】:

    【解决方案3】:

    您需要提供异常的堆栈跟踪。但它看起来可能在两个地方:

    1. s.setAttribute("person",person);其中 s 从未设置;
    2. out.println("" + p.getKey() + "");其中永远不会设置属性“person”;

    祝你好运。

    【讨论】:

      【解决方案4】:

      您需要从request 获取会话,不能简单地指望通过HttpSession s ; 获取它——snull

      // Something like this (in processRequest), although I 
      // would prefer a local session variable.
      s = request.getSession();
      

      【讨论】:

        猜你喜欢
        • 2020-09-05
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多