【问题标题】:Variable not showing up Java Servlets [duplicate]变量未显示 Java Servlets [重复]
【发布时间】:2017-03-17 14:20:00
【问题描述】:

我尝试制作一个简单的 Java Servlet 应用程序,它显示日期和时间。 问题是:当我加载页面时它不会显示(页面本身确实显示)。我已经调试过了,日期和时间确实显示在控制台中。

TestServlet.java(不是整个文件):

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

    DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date timeDate = new Date();
    @SuppressWarnings("unused")
    String time = format.format(timeDate);


    request.getRequestDispatcher("/WEB-INF/test.jsp").forward(request, response);

}

test.jsp(整个文件):

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <h1>Welcome! It is ${time}!</h1>
    </body>
</html>

欢迎所有帮助;)

【问题讨论】:

  • 也许把时间放在范围内;目前它只是doGet 中的一个局部变量。
  • 下次做一些研究;)Here a good example

标签: java jsp servlets


【解决方案1】:

搞定了!我看过一个视频,他们像我在问题中那样做。现在我查看了 AxelH 给我的链接(我之前看过,但认为它没有回答我的问题)并找到了答案!

对于同样遇到问题的任何人: 我必须将其设置为请求中的属性,如下所示:

request.setAttribute("name", "value");

然后我可以在jsp文件中使用它;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-29
    • 2021-06-07
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2016-04-03
    • 1970-01-01
    相关资源
    最近更新 更多