编写一个JSP页面lucknum.jsp,产生0-9之间的随机数作为用户幸运数字,将其保存到会话中,并重定向到另一个页面showLuckNum.jsp中,在该页面中将用户的幸运数字显示出来
lucknum.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> <!-- <script type="text/javascript"> /*产生随机数 */ function fun(){ int num = Math.round(Math.random()*9); } </script> --> </head> <body > <% //获取幸运数字 double num = Math.random()*10; long lucknum=Math.round(num); session.setAttribute("lucknum", lucknum); response.sendRedirect("two.jsp"); %> </body> </html>
showLuckNum.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> <% Object lucknum = session.getAttribute("lucknum"); if(lucknum.toString().equals("5")){ out.print("恭喜中奖了,数字是:"+lucknum.toString()); }else{ out.print("没中奖,数字是:"+lucknum.toString()); } %> </body> </html>