如何使用Controller Servlet将表单数据发送到数据库?

时间:2015-04-08 07:14:40

标签: mysql jsp html-form

我正在使用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" 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>

我想点击“添加我的评分”时将用户评分发送到我的数据库。我想特别将所有数据发送到这个数据库表:

rating
- rating_id
- rating_value
- product_id
- rating_date

表'rating'与表'product'形成一个联合表,创建表名'product_has_rating':

product_has_rating
product_id
rating_id

我在Controller Servlet中使用jsp转发数据,但我想知道如何使用正确的product_id将rating_value和rating_date发送到Mysql中的表“rating”。我走在好路上了吗?

Controller 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";

2 个答案:

答案 0 :(得分:0)

<form action="rateProduct" method="post" id="formToSend">
            <fieldset class="rating">
    <input type="radio" id="star1"  name="rating" value="1" onchange="javascript:sendIt();"/><label for="star1"></label>
    <input type="radio" id="star2"  name="rating" value="2" onchange="javascript:sendIt();" /><label for="star2"></label>
    <input type="radio" id="star3"  name="rating" value="3" onchange="javascript:sendIt();" /><label for="star3"></label>
    <input type="radio" id="star4"  name="rating" value="4" onchange="javascript:sendIt();" /><label for="star4"></label>
    <input type="radio" id="star5"  name="rating" value="5" onchange="javascript:sendIt();" /><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>

<script>
function sendIt() {
document.getElementById("formToSend").submit();
}
</script>

答案 1 :(得分:0)

enter image description here来自用户的基本输入,用于分隔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>