使用java在JSP中生成随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22409321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:34:42  来源:igfitidea点击:

Random number generation in JSP using java

javajsp

提问by user3346307

I want to generate random numbers in JSP.

我想在 JSP 中生成随机数。

I have created java code to generate a random number in a JSP page and output the random number in a textbox.

我已经创建了 java 代码来在 JSP 页面中生成一个随机数并在文本框中输出随机数。

<%@page import="java.util.*" %>
<%
Random rand = new Random();
int n = rand.nextInt(90000) + 10000;
System.out.println(n);
%>
<html>
    <body>
        ORDER NO  <input type="text" name="order" value=""/>
    </body>
</html>

I am not getting output for this code, can anyone suggest why?

我没有得到这段代码的输出,有人能建议为什么吗?

采纳答案by Thomas

System.out.println(n);would write to the console, a JSP provides its own outwriter, thus just write out.println(n);, i.e. without the Systemcontext.

System.out.println(n);将写入控制台,JSP 提供自己的out编写器out.println(n);,因此只需 write ,即没有System上下文。

Or if you want to write the number into your input field then use something like this:

或者,如果您想将数字写入输入字段,请使用以下内容:

ORDER NO  <input type="text" name="order" value="<%=n%>"/>