javascript - 从 Servlet 向 JSP 发送响应

标签 javascript java jsp servlets

我已经创建了 JSP、Servlet 和 Java 页面,但现在尝试获取 MyAlgorithm(Java 算法)的输出并将其发送回我的 JSP 页面。然而它不起作用......我在这里错过了什么吗?

如果问题不清楚请问我。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());    
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String s_LocalDate = request.getParameter("s_JavascriptDate");
        System.out.println(s_LocalDate);
        algorithm MyAlgorithm = new algorithm();
        MyAlgorithm.Days();
        doGet(request, response);
    }

最佳答案

您应该将传递的值绑定(bind)到属性中,以便它与响应一致。

request.setAttribute("algoAnswer", MyAlgorithm.Days()); request.getRequestDispatcher("pagewhereanswershouldappear.jsp").forward(request,response);

在页面端,您可以使用 jSTL 通过 ${algoAnswer} 从属性中获取值。

这里有更多相关信息https://www.journaldev.com/2090/jstl-tutorial-jstl-tags-example

关于javascript - 从 Servlet 向 JSP 发送响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58785019/

相关文章:

javascript - 如何在 Angular 2 中返回 forEach?

java - Uri 引起的 CursorIndexOutOfBoundsException

javascript - jquery - 范围在变量中丢失

java - 我正在尝试以相反的顺序迭代每个循环但无法成功

javascript - 使用 jQuery 确定相对路径

javascript - JSON 请求的动态脚本标签...检测是否存在 XXX 错误?

java - java中的多线程静态方法

java - SQLException.getSQLState 的所有可能值是什么?

javascript - 网站离线jsp

javascript - 使用 jquery 自定义自动完成