Java request.getPARameter() 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22407585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
request.getPArameter() returning null
提问by user3420231
I am trying to read the input by the user as soon as he clicks on a button. But unfortunately the request.getParamater()
is always returning a null
value. Can someone please help me I have been hours Trying to sort this out :(
我试图在用户单击按钮后立即读取用户的输入。但不幸的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>
回答by robermann
You are not using any <form>
HTML tag. All input
tag must go inside it, and the button click should do a submit
of the form.
您没有使用任何<form>
HTML 标签。所有input
标签都必须在里面,按钮点击应该做一个submit
表单。
By the way, you are mixing Javascrit client code and Java server code in a way that cannot work, you have different rendering timings.
顺便说一句,您正在以一种无法工作的方式混合 Javascrit 客户端代码和 Java 服务器代码,您有不同的呈现时间。
On this example, assuming you are wanting to have a unique component both for client-side and for server-side, as you are doing, you can add the following to your JSP. For commodity I'm going to separate client-side and server-side logics.
在此示例中,假设您希望在客户端和服务器端都有一个唯一的组件,那么您可以将以下内容添加到您的 JSP 中。对于商品,我将分离客户端和服务器端逻辑。
Client side
客户端
<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>
Server side
服务器端
<%
if("true".equals(request.getParameter("loaded"))){
//TODO
/*
Do here server-side logic...
*/
}
%>
回答by iamsuman
The main reason behind this is you are using the javascript
i.e <script language="javascript">
and you are trying to run the server side code inside it .
you must close the javascript by using </script>
and then try to run the server side code..
这背后的主要原因是您正在使用javascript
ie<script language="javascript">
并且您试图在其中运行服务器端代码。您必须使用关闭 javascript </script>
,然后尝试运行服务器端代码..
回答by iamsuman
You don't need javascript here, go with basic html form tag to submit the details. Also don't use scriplets as you are already aware of jstl. Note below code is only implemented for add, you need to write your code for remove.
您在这里不需要 javascript,使用基本的 html 表单标签来提交详细信息。也不要使用脚本,因为您已经知道 jstl。注意下面的代码仅用于添加,您需要编写代码以进行删除。
<%@ 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>