Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java request.getPArameter()返回null_Java_Html_Jsp_Parameters_Null - Fatal编程技术网

Java request.getPArameter()返回null

Java request.getPArameter()返回null,java,html,jsp,parameters,null,Java,Html,Jsp,Parameters,Null,我试图在用户单击按钮时立即读取输入。但不幸的是,request.getParamater()总是返回一个null值。有人能帮我吗?我已经花了好几个小时试图解决这个问题:( 函数add() { 打印(); } 函数删除() { } 百思达批发商有限公司。 欢迎光临百思达批发商有限公司网上订购 目前库存的物品 从productinfo中选择名称、代码、价格、数量; 名称 代码 价格/单位 可用数量 输入代码: 输入数量: 您没有使用任何HTML标记。所有输入标记都必须放在它里面,单击按钮应该

我试图在用户单击按钮时立即读取输入。但不幸的是,
request.getParamater()
总是返回一个
null
值。有人能帮我吗?我已经花了好几个小时试图解决这个问题:(


函数add()
{
打印();
}
函数删除()
{
}
百思达批发商有限公司。
欢迎光临百思达批发商有限公司网上订购
目前库存的物品
从productinfo中选择名称、代码、价格、数量;
名称
代码
价格/单位
可用数量


输入代码:
输入数量:
您没有使用任何
HTML标记。所有
输入
标记都必须放在它里面,单击按钮应该执行表单的
提交

顺便说一句,您正在以一种无法工作的方式混合Javascrit客户端代码和Java服务器代码,您有不同的呈现时间

在本例中,假设您希望在客户端和服务器端都有一个唯一的组件,您可以在JSP中添加以下内容

客户端

函数flagLoaded(){
document.forms[0][“loaded”].value=“true”;
}
服务器端
这背后的主要原因是您正在使用
javascript
,即
,并试图在其中运行服务器端代码。
您必须使用
关闭javascript,然后尝试运行服务器端代码。

此处不需要javascript,请使用基本html表单标记提交详细信息。也不要使用scriplets,因为您已经知道jstl。注意,下面的代码仅用于添加,您需要编写用于删除的代码


百思达批发商有限公司。
.错误{
颜色:红色;
}
欢迎光临百思达批发商有限公司网上订购
目前库存的物品
插入销售(代码、数量、价格、名称)值(?、、?、?)
未能保存库存详细信息
从productinfo中选择名称、代码、价格、数量;
无法获取库存详细信息
名称
代码
价格/单位
可用数量


输入代码:
输入数量:
request.getParameter()以字符串形式返回请求参数的值,如果参数不存在,则返回null。您在哪里指定了
code
quantity
?它们在正文的最后一部分中指定,即输入code:etc…我对jsp非常陌生…您能告诉我,我应该在哪里填写表单,它应该是post类型还是post类型吗get please?Thankso我该怎么办?我添加了表单标签,还将按钮更改为sublit…现在我该怎么办?我添加了一些代码。如果对您没有任何意义,您还应该学习HTML和Javascript,除了JSP:)
<%@ 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>
<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...
      */
  }
%>