随机抽取学号

idea 配置

抽学号

浏览器 提交一个100 以内的整数。,后端接受后,根据提供的整数,产生 100 以内的

随机数,返回给浏览器?

  • 前端:提供 随机数范围 ,病发送请求
  • 后端:处理随机数的产生,和响应至浏览器


添加web添加



html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form action="/chouxuehao/demo" method="get">
    请输入参与活动的总人数
    <br>
    <input type="text" name="renshu" placeholder="100以内的整数">
    <br>
    <input type="submit" value="确定活动人数">
    
</form>
</body>
</html>

创建artifacts




web.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>aa</servlet-name>
        <servlet-class>com.yanyu.Demo</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>aa</servlet-name>
        <url-pattern>/demo</url-pattern>
    </servlet-mapping>
</web-app>

JDK8在线文档

后端

java 复制代码
package com.yanyu;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class Demo extends HttpServlet {
//    接受前端奇怪的活动总人数
//    产生随机数

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
//       获取响应对象
        PrintWriter out = response.getWriter();
//        huoqu 前段  数据
        String renshu = request.getParameter("renshu");//45
//        把字符串 转为  int
        int i = Integer.parseInt(renshu);



        int random =(int) (Math.random() * i);
        int i1 = random + 1;
//        out.print("中奖的是:"+i1);

//        ======================
out.print("        <!DOCTYPE html>");
out.print("<html lang='en'>");
out.print("<head>");
out.print("    <meta charset='UTF-8'>");
out.print("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.print("    <title>Document</title>");
out.print("</head>");
out.print("<body>");
out.print("                中奖的是:");
out.print("    <br>");
out.print("    <div style='color: red; font-size: 48px;'>");
out.print(i1);
out.print("    </div>");
out.print("</body>");
out.print("</html>");



    }
}
相关推荐
桦说编程1 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen1 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研1 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员2 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋2 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国3 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~3 小时前
《设计模式》装饰模式
java·设计模式
A尘埃3 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交3 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
CHEN5_024 小时前
【Java基础面试题】Java基础概念
java·开发语言