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 :(
<%@ 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>