Mysql 如何使用控制器Servlet将表单数据发送到数据库?
我正在使用html表单构建此评级系统:Mysql 如何使用控制器Servlet将表单数据发送到数据库?,mysql,jsp,forms,Mysql,Jsp,Forms,我正在使用html表单构建此评级系统: <form action="rateProduct" method="post"> <fieldset class="rating"> <input type="radio" id="star1" name="rating" value="1"/><label for="star1"></label> <input type="radio"
<form action="rateProduct" method="post">
<fieldset class="rating">
<input type="radio" id="star1" name="rating" value="1"/><label for="star1"></label>
<input type="radio" id="star2" name="rating" value="2" /><label for="star2"></label>
<input type="radio" id="star3" name="rating" value="3" /><label for="star3"></label>
<input type="radio" id="star4" name="rating" value="4" /><label for="star4"></label>
<input type="radio" id="star5" name="rating" value="5" /><label for="star5"></label>
</fieldset>
<input name="ratingId" value="${selectedProduct.id}" type="hidden">
<input class="validate_rating" id="addRating" onclick="addedRating()" value="<fmt:message key='RateProduct'/>" type="submit">
<p id="voted" style="font-size:smaller;"></p>
</form>
“评级”表与“产品”表构成一个联合表,后者创建了一个表名“product\u has\u rating”:
product_has_rating
product_id
rating_id
我在jsp中使用控制器Servlet转发数据,但我想知道如何使用正确的产品id将rating_值和rating_日期发送到Mysql中的rating表。我走的是好路吗
控制器Servlet:
// if rateProduct action is called
} else if (userPath.equals("/rateProduct")) {
// get input from request
String productId = request.getParameter("productId");
String rating = request.getParameter("rating_value");
userPath = "/product";
用户在中的基本输入用于分离jsp文件
//////////////////////////索引文件////////////////////////////////////
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MySQL Database Website</title>
</head>
<body>
<h1>Hello World!</h1>
<!DOCTYPE html>
<html>
<head>
<title>Entry form</title>
</head>
<body>
<h1>Entry Form</h1>
<form name="InputForm" action="response.jsp" method="post">
<table>
<tbody>
<tr>
<td>Enter your name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Enter screen name:</td>
<td><input type="text" name="screenName" /></td>
</tr>
<tr>
<td>
Database Username:
</td>
<td>
<input type="text" name="username" size="30"/>
</td>
</tr>
<tr>
<td>
Database Password:
</td>
<td>
<input type="password" name="password" size="30"/>
</td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="clear" value="Clear" />
</form>
</body>
</html>
<sql:setDataSource
var="myDS"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javabase"
user="root" password="secret1245Q!"
/>
<sql:query var="listUsers" dataSource="${myDS}">
SELECT * FROM users;
</sql:query>
<div align="center">
<table border="1" cellpadding="5">
<caption><h2>List of users</h2></caption>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Profession</th>
</tr>
<c:forEach var="user" items="${listUsers.rows}">
<tr>
<td><c:out value="${user.id}" /></td>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.email}" /></td>
<td><c:out value="${user.profession}" /></td>
</tr>
</c:forEach>
</table>
</div>
/////////////////输出文件///////////////
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MySQL Database Website</title>
</head>
<body>
<h1>Hello World!</h1>
<!DOCTYPE html>
<html>
<head>
<title>Entry form</title>
</head>
<body>
<h1>Entry Form</h1>
<form name="InputForm" action="response.jsp" method="post">
<table>
<tbody>
<tr>
<td>Enter your name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Enter screen name:</td>
<td><input type="text" name="screenName" /></td>
</tr>
<tr>
<td>
Database Username:
</td>
<td>
<input type="text" name="username" size="30"/>
</td>
</tr>
<tr>
<td>
Database Password:
</td>
<td>
<input type="password" name="password" size="30"/>
</td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="clear" value="Clear" />
</form>
</body>
</html>
<sql:setDataSource
var="myDS"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javabase"
user="root" password="secret1245Q!"
/>
<sql:query var="listUsers" dataSource="${myDS}">
SELECT * FROM users;
</sql:query>
<div align="center">
<table border="1" cellpadding="5">
<caption><h2>List of users</h2></caption>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Profession</th>
</tr>
<c:forEach var="user" items="${listUsers.rows}">
<tr>
<td><c:out value="${user.id}" /></td>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.email}" /></td>
<td><c:out value="${user.profession}" /></td>
</tr>
</c:forEach>
</table>
</div>