在jsp中使用request对象获取form表单提交的test以及select中的数据
时间: 2024-10-20 16:09:32 浏览: 24
在JSP(JavaServer Pages)中,当你需要从用户通过HTML表单(如`<form>`标签)提交的数据中获取信息,通常会使用`request`对象。`request`对象属于`HttpServletRequest`接口,包含了HTTP请求的相关信息。如果你想获取名为`test`的表单字段值,你可以这样做:
```jsp
<form action="your-action-url" method="post">
<input type="text" name="test" />
<!-- 其他表单元素 -->
</form>
<%-- JSP代码部分 --%>
<%
String testValue = request.getParameter("test");
// 现在testValue变量就存储了来自test输入框的用户提交值
// 对于select元素,假设name属性为'selectOption'
String selectValue = request.getParameter("selectOption");
if (selectValue != null) {
System.out.println("Selected option from the dropdown is: " + selectValue);
}
%>
```
这里我们使用`getParameter()`方法来获取特定名称的参数。如果`select`元素是一个下拉列表,其选项会被当作多个独立的参数处理,每个选中的值对应一个单独的键。
相关问题
在javaweb项目中,home.jsp页面中调用存储在session里的对象stuno的值“123”,调用java方法中的sql语句查询数据库中stuno为123的全部信息,存放在ResultSet中,并在home.jsp页面中以<%=rs.getString()%>的方式根据字段名称呈现,假设home.jsp中有一修改按钮,点击按钮后在当前页弹出更新窗口,更新窗口中以表单和输入框的形式让用户输入更新信息,表单提交后,home.jsp能够显示更新后的信息,该如何用代码实现
可以参考以下代码实现:
在home.jsp页面中,添加修改按钮:
```
<button onclick="openUpdateModal()">修改</button>
```
JavaScript代码实现弹出模态框:
```
function openUpdateModal() {
// 弹出模态框
$('#updateModal').modal('show');
}
```
在home.jsp页面中,添加模态框:
```
<div class="modal fade" id="updateModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form id="updateForm" action="update.jsp" method="post">
<div class="modal-header">
<h4 class="modal-title">更新信息</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="name">姓名:</label>
<input type="text" class="form-control" id="name" name="name" value="<%=rs.getString("name")%>">
</div>
<div class="form-group">
<label for="age">年龄:</label>
<input type="number" class="form-control" id="age" name="age" value="<%=rs.getInt("age")%>">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
<button type="submit" class="btn btn-primary">提交</button>
</div>
</form>
</div>
</div>
</div>
```
在update.jsp页面中,处理表单提交:
```
<%@ page import="java.sql.*" %>
<%
// 获取表单提交的更新信息
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
// 查询数据库中stuno为123的信息
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
pstmt = conn.prepareStatement("SELECT * FROM student WHERE stuno = ?");
pstmt.setString(1, "123");
rs = pstmt.executeQuery();
if (rs.next()) {
// 更新数据库中的信息
pstmt = conn.prepareStatement("UPDATE student SET name = ?, age = ? WHERE stuno = ?");
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, "123");
pstmt.executeUpdate();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 返回到home.jsp页面
response.sendRedirect("home.jsp");
%>
```
在home.jsp页面中,重新查询数据库中stuno为123的信息,并呈现在页面上:
```
<%
// 查询数据库中stuno为123的信息
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
pstmt = conn.prepareStatement("SELECT * FROM student WHERE stuno = ?");
pstmt.setString(1, "123");
rs = pstmt.executeQuery();
if (rs.next()) {
// 显示更新后的信息
%>
<p>姓名:<%=rs.getString("name")%></p>
<p>年龄:<%=rs.getInt("age")%></p>
<%
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
```
编写4个JSP页面: searchStu. jsp、byNumber. jsp .byName. jspallStu.jsp页面。searchStu. jsp的具体要求: searchStu. jsp提供两个表单。其中一个表单允许用户输人要查询的学生的学号,即输人stuinfo表中xh字段的查询条件,然后将查询条件提交给要byXh.jsp,另一个表单允许用户输人要查询的学生的姓名,即输人stuinfo表中name字段的查询条件,然后将查询条件提交给要byName. jsp.
好的,我理解了你的要求。以下是四个JSP页面的代码:
1. searchStu.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询学生信息</title>
</head>
<body>
<h1>查询学生信息</h1>
<form action="byNumber.jsp" method="get">
<label for="xh">请输入学生的学号:</label>
<input type="text" name="xh" id="xh">
<input type="submit" value="查询">
</form>
<form action="byName.jsp" method="get">
<label for="name">请输入学生的姓名:</label>
<input type="text" name="name" id="name">
<input type="submit" value="查询">
</form>
</body>
</html>
```
2. byNumber.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>按学号查询学生信息</title>
</head>
<body>
<h1>按学号查询学生信息</h1>
<%
String xh = request.getParameter("xh");
String sql = "SELECT * FROM stuinfo WHERE xh=?";
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, xh);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
out.println("学号:" + rs.getString("xh") + "<br>");
out.println("姓名:" + rs.getString("name") + "<br>");
out.println("性别:" + rs.getString("sex") + "<br>");
out.println("年龄:" + rs.getInt("age") + "<br>");
} else {
out.println("没有找到学号为 " + xh + " 的学生!");
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
<br>
<a href="searchStu.jsp">返回</a>
</body>
</html>
```
3. byName.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>按姓名查询学生信息</title>
</head>
<body>
<h1>按姓名查询学生信息</h1>
<%
String name = request.getParameter("name");
String sql = "SELECT * FROM stuinfo WHERE name=?";
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
out.println("学号:" + rs.getString("xh") + "<br>");
out.println("姓名:" + rs.getString("name") + "<br>");
out.println("性别:" + rs.getString("sex") + "<br>");
out.println("年龄:" + rs.getInt("age") + "<br><br>");
}
if (!rs.first()) {
out.println("没有找到姓名为 " + name + " 的学生!");
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
<br>
<a href="searchStu.jsp">返回</a>
</body>
</html>
```
4. allStu.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询所有学生信息</title>
</head>
<body>
<h1>查询所有学生信息</h1>
<%
String sql = "SELECT * FROM stuinfo";
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
out.println("学号:" + rs.getString("xh") + "<br>");
out.println("姓名:" + rs.getString("name") + "<br>");
out.println("性别:" + rs.getString("sex") + "<br>");
out.println("年龄:" + rs.getInt("age") + "<br><br>");
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
<br>
<a href="searchStu.jsp">返回</a>
</body>
</html>
```
其中,我假设你使用MySQL数据库,并且数据库名为test,用户名和密码均为root。如果不是这样,请根据实际情况修改代码中的相关信息。另外,为了安全起见,建议将数据库连接的相关信息存放在配置文件中,并且使用连接池来管理数据库连接。
阅读全文