jsp中标签disabled之后,在action中用request.getParameter()取不到该标签

本文探讨了在JSP页面中使用disabled属性时,表单元素如何无法通过request.getParameter()方法在后端获取的问题。当<input>标签设置了disabled属性后,即使指定了name属性,也无法在服务器端获取其值。

jsp中标签disabled之后,在action中用request.getParameter()取不到该标签

如jsp中<input type="text" name="hello" value="helloworld" disabled />,

在action中,request.getParameter("hello");会报错。

<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% // 数据库连接配置 String dbUrl = "jdbc:sqlserver://localhost:1433;databaseName=Lanqiu;" + "encrypt=true;trustServerCertificate=true"; String dbUser = "sa"; String dbPass = "hfy123456"; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection(dbUrl, dbUser, dbPass); String sql = "SELECT court_id, court_name, location, court_type, status FROM basketball_courts"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); %> <!DOCTYPE html> <html> <head> <title>场地预约情况</title> <style> body { font-family: Arial, sans-serif; padding: 20px; } .court-card { border: 1px solid #ccc; margin: 15px 0; padding: 15px; display: flex; justify-content: space-between; align-items: center; } .btn { padding: 8px 12px; border: none; cursor: pointer; color: white; border-radius: 4px; } .available { background-color: green; } .unavailable { background-color: red; } </style> </head> <body> <h2>篮球场地预约情况</h2> <% while (rs.next()) { int courtId = rs.getInt("court_id"); String courtName = rs.getString("court_name"); String location = rs.getString("location"); String courtType = rs.getString("court_type"); String status = rs.getString("status"); boolean isAvailable = status.equals("可用"); if (isAvailable) { String checkSql = "SELECT * FROM reservations WHERE court_id = ? AND start_time <= GETDATE() AND end_time >= GETDATE()"; PreparedStatement checkPs = conn.prepareStatement(checkSql); checkPs.setInt(1, courtId); ResultSet checkRs = checkPs.executeQuery(); if (checkRs.next()) { isAvailable = false; } checkRs.close(); checkPs.close(); } %> <div class="court-card"> <div> <strong><%= courtName %></strong><br/> 地点:<%= location %><br/> 类型:<%= courtType %><br/> 状态:<%= status %> </div> <div> <% if (isAvailable) { %> <button class="btn available" onclick="reserve(<%= courtId %>)">预约</button> <% } else { %> <button class="btn unavailable" disabled>不可预约</button> <% } %> </div> </div> <% } } catch (Exception e) { System.out.println("错误:" + e.getMessage()); } finally { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } %> <script> function reserve(courtId) { fetch('reserveCourt.jsp?court_id=' + courtId) .then(res => res.text()) .then(data => alert(data)) .catch(err => alert('预约失败')); } </script> </body> </html> 这是我的一个1jsp文件里的代码,<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8"%> <html> <head> <title>篮球场预约</title> <style> body { font-family: Arial, sans-serif; padding: 20px; background-color: #f5f5f5; } h2 { text-align: center; color: #333; } form { max-width: 400px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } label { display: block; margin-top: 10px; } input, select { width: 100%; padding: 8px; margin-top: 5px; } button { margin-top: 15px; width: 100%; padding: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; } .message { text-align: center; margin-top: 20px; color: red; } </style> </head> <body> <h2>篮球场预约系统</h2> <form action="doReserve.jsp" method="post"> <label for="userId">用户ID:</label> <input type="number" id="userId" name="userId" required> <label for="courtId">选择篮球场:</label> <select id="courtId" name="courtId" required> <option value="">请选择篮球场</option> <% Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { // 加载 JDBC 驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 数据库连接信息 String dbUrl = "jdbc:sqlserver://localhost:1433;databaseName=Lanqiu;encrypt=true;trustServerCertificate=true;"; String dbUser = "sa"; String dbPass = "hfy123456"; // 建立连接 conn = DriverManager.getConnection(dbUrl, dbUser, dbPass); // 查询所有状态为“可用”的篮球场 String sql = "SELECT court_id, court_name FROM basketball_courts WHERE status = '可用'"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { %> <option value="<%= rs.getInt("court_id") %>"><%= rs.getString("court_name") %></option> <% } } catch (Exception e) { System.out.println("<option disabled>加载场地失败</option>"); e.printStackTrace(); } finally { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } %> </select> <label for="startTime">开始时间:</label> <input type="datetime-local" id="startTime" name="startTime" required> <label for="endTime">结束时间:</label> <input type="datetime-local" id="endTime" name="endTime" required> <button type="submit">提交预约</button> </form> <% String error = request.getParameter("error"); if (error != null && error.equals("true")) { %> <div class="message">预约失败,请检查输入内容或场地是否已被占用。</div> <% } %> </body> </html> 这是上面代码最后跳转的页面,两个页面都能独立显示,为什么点击预约后就显示报错
最新发布
06-26
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值