使用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
Random number generation in JSP using java
提问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 out
writer, thus just write out.println(n);
, i.e. without the System
context.
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%>"/>