public class DoubleColorBall extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// 设置一个容器,来装红球
List<Integer> list = new ArrayList<Integer>();
Random random = new Random();
while (list.size() < 6)
{
int x = random.nextInt(33) + 1;
list.add(x);
for(int i = 0;i<list.size()-1;i++)
{
if(x ==list.get(i))
list.remove(list.size()-1); //解决重复问题,每当加入之后,就去判断是否和上一个下标所对应的容器元素的值是否相同,解决问题
}
}
Collections.sort(list); //排序
// 制造蓝球必中
int blueBall = random.nextInt(16) + 1;
String result = " ";
Iterator it = list.iterator();
while (it.hasNext())
{
result += it.next() + " ";
}
result = "RedBall:" + result + "<br/>" + "BlueBall:" + blueBall;
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
PrintWriter pout = resp.getWriter();
pout.println("<html><head><title>DoubleColorBallSs</title></head>");
pout.println("<body><h1>" + result + "</h1></body>");
}
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// 设置一个容器,来装红球
List<Integer> list = new ArrayList<Integer>();
Random random = new Random();
while (list.size() < 6)
{
int x = random.nextInt(33) + 1;
list.add(x);
for(int i = 0;i<list.size()-1;i++)
{
if(x ==list.get(i))
list.remove(list.size()-1); //解决重复问题,每当加入之后,就去判断是否和上一个下标所对应的容器元素的值是否相同,解决问题
}
}
Collections.sort(list); //排序
// 制造蓝球必中
int blueBall = random.nextInt(16) + 1;
String result = " ";
Iterator it = list.iterator();
while (it.hasNext())
{
result += it.next() + " ";
}
result = "RedBall:" + result + "<br/>" + "BlueBall:" + blueBall;
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
PrintWriter pout = resp.getWriter();
pout.println("<html><head><title>DoubleColorBallSs</title></head>");
pout.println("<body><h1>" + result + "</h1></body>");
}
}
=============================================================================================================================
JSP端
</head>
<body> <!-- JSP页面实现一个按钮 连接到servlet进行处理 -->
<a href="/test1/DoubleColorBall" target="_blank" ><h1 color= "red" >点击中奖发财</h1></a>
</body>
</html>