我试图在用户点击按钮后立即阅读输入。但遗憾的是request.getParamater()
始终返回null
值。有人可以帮助我,我已经花了几个小时试着解决这个问题:(
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<script language="javascript">
function add()
{
<%
Integer quantity = 500;
Integer code = 1000;
//String codes = request.getParameter("code");
String codes = (String) request.getParameter("code");
String quanti = (String) request.getParameter("quantity");
if (codes != null && quanti != null) {
quantity = Integer.parseInt(quanti);
code = Integer.parseInt(codes);
}
out.println(code);
out.println(quantity);
String connectionURL = "jdbc:mysql://localhost:3306/products";
Connection connection = null;
PreparedStatement pstatement = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
int updateQuery = 0;
// check if the text box is empty
if (code != null && quantity != null) {
// check if the text box having only blank spaces
if (codes != "" && quanti != "") {
try {
/* Create a connection by using getConnection()
method that takes parameters of string type
connection url, user name and password to connect
to database. */
connection = DriverManager.getConnection(connectionURL, "root", "170293m");
// sql query to insert values in the secified table.
String queryString = "INSERT INTO sales (code, quantity, price, name) VALUES (?, ?, ?, ?)";
/* createStatement() is used for create statement
object that is used for
sending sql statements to the specified database. */
pstatement = connection.prepareStatement(queryString);
pstatement.setInt(1, code);
pstatement.setInt(2, quantity);
pstatement.setDouble(3, 50);
pstatement.setString(4, "aw ras");
updateQuery = pstatement.executeUpdate();
if (updateQuery != 0) {
out.println("Error in query");
}
} catch (Exception ex) {
out.println("Unable to connect to batabase.");
} finally {
// close all the connections.
pstatement.close();
connection.close();
}
}
}%>
print();
}
function remove()
{
}
</script>
<title>BestWholesaler LTd.</title>
</head>
<body>
<h1>Welcome to BestWholesaler Ltd. Online Ordering</h1>
<h2>Items Currently in Stock</h2>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/products"
user="root" password="170293m"/>
<sql:query dataSource="${snapshot}" var="result">
SELECT Name, Code, PricePU, Quantity from productinfo;
</sql:query>
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Code</th>
<th>Price/Unit</th>
<th>Quantity Available</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.Name}"/></td>
<td><c:out value="${row.Code}"/></td>
<td><c:out value="${row.PricePU}"/></td>
<td><c:out value="${row.Quantity}"/></td>
</tr>
</c:forEach>
</table>
<br>
<br>
Enter Code: <input type="text" name="code" id="code" value="" />
<br>
Enter Quantity: <input type="text" name="quantity" id="quantity" value="" />
<input type="button" value="ADD" name="add" onclick="add()" />
<input type="button" value="REMOVE" name="remove" onclick="remove()" />
</body>
</html>
答案 0 :(得分:0)
您没有使用任何<form>
HTML标记。所有input
代码都必须放在其中,按钮点击应该是表单的submit
。
顺便说一句,您正在以一种无法工作的方式混合Javascrit客户端代码和Java服务器代码,您有不同的渲染时间。
在此示例中,假设您希望在客户端和服务器端都拥有唯一的组件,就像您一样,您可以将以下内容添加到JSP中。对于商品,我将分离客户端和服务器端逻辑。
<script>
function flagLoaded(){
document.forms[0]["loaded"].value = "true";
}
</script>
<form name="input" action="myself.jsp" method="get" onsubmit="flagLoaded()">
<!-- put here all your client-side's inputs -->
<!-- ... TODO ... -->
<input type="hidden" name="loaded" value="false">
<input type="submit" value="Submit">
</form>
<%
if("true".equals(request.getParameter("loaded"))){
//TODO
/*
Do here server-side logic...
*/
}
%>
答案 1 :(得分:0)
这背后的主要原因是您正在使用javascript
,即<script language="javascript">
,并且您正在尝试在其中运行服务器端代码。
您必须使用</script>
关闭javascript,然后尝试运行服务器端代码..
答案 2 :(得分:0)
您在这里不需要javascript,请使用基本的html表单标签提交详细信息。你也不知道jstl,也不要使用scriplet。请注意下面的代码仅用于添加,您需要编写代码以进行删除。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/products" user="root" password="170293m" />
<title>BestWholesaler LTd.</title>
<style>
.error {
color: red;
}
</style>
<body>
<h1>Welcome to BestWholesaler Ltd. Online Ordering</h1>
<h2>Items Currently in Stock</h2>
<!-- add button was clicked -->
<c:if test="${null != param.add}">
<c:catch var="error">
<sql:update dataSource="${snapshot}" scope="page" var="result">
INSERT INTO sales (code, quantity, price, name) VALUES (?, ?, ?, ?)
<sql:param value="${param.code}" />
<sql:param value="${param.quantity}" />
<sql:param value="50" />
<sql:param value="aw ras" />
</sql:update>
</c:catch>
</c:if>
<!-- error occured -->
<c:if test="${null != error}">
<div class='error'>
Failed to save stock details
</div>
</c:if>
<c:catch var="error">
<sql:query dataSource="${snapshot}" var="result">
SELECT Name, Code, PricePU, Quantity from productinfo;
</sql:query>
</c:catch>
<c:if test="${null != error}">
<div class='error'>
Failed to get stock details
</div>
</c:if>
<c:if test="${null == error}">
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Code</th>
<th>Price/Unit</th>
<th>Quantity Available</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td>
<c:out value="${row.Name}" />
</td>
<td>
<c:out value="${row.Code}" />
</td>
<td>
<c:out value="${row.PricePU}" />
</td>
<td>
<c:out value="${row.Quantity}" />
</td>
</tr>
</c:forEach>
</table>
</c:if>
<br/>
<br/>
<form method='post'> <!-- no action required, still you can give the same jsp file name -->
Enter Code:
<input type="text" name="code" />
<br/>Enter Quantity:
<input type="text" name="quantity" />
<input type="submit" value="ADD" name="add" />
</form>
</body>
&#13;