-1

I am trying to develop simple shopping cart application. So I have simple database table in Derby DB. From there I retrieved data to a table in JSP. Now i want to transfer that values to servlet. And this is my part of code:

ItemList.JSP

<%
String id = request.getParameter("userId");
String driverName = "org.apache.derby.jdbc.EmbeddedDriver";
String connectionUrl = "jdbc:derby://localhost:1527/";
String dbName = "CartDB";
String userId = "aaa";
String password = "aaa";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<table id="customers">
<%
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="SELECT * FROM electronics_catagory";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td class="white">
<img src="<%=resultSet.getString("image") %>" style="width:300px;height:230px;" alt="product image">
</td>
<td class="white">
Product Name : <%=resultSet.getString("name")%><br>
<span class="smallText">Description : <%=resultSet.getString("description") %></span>
</td>
<td class="white">price <br/> <%=resultSet.getString("price") %></td>
<td class="white">Required QTY : <br> <input type="text" name="qty"></td>
<td class="white">
<form action="#" method="post">
<button class="button"><span>Add to WishList</span></button>
</form>
</td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>

So anyone have any idea how to pass this values (ex:- <%= resultSet.getString("price") %>) to servlet?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Amila
  • 1

1 Answers1

0

If your servlet is mapped on your doGet, you can do it like this (without a form):

<a class="button" href="../YourServletUrl?price=<%=resultSet.getString("price") %>"><span>Add to WishList</span></a>

Then in the doGet method of your servlet, get it like you normally get any form parameter:

String price = request.getParameter("price");
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • thanks mate, an any idea how to pass multiple variables by this method? – Amila May 29 '18 at 10:09
  • yeah just use ampersand `&` for example: `href="../YourServletUrl?price=<%=resultSet.getString("price") %>&name=<%=resultSet.getString("name")%>` so it would look something like this: `../YourServletUrl?price=9000&name=chocolate` – Jonathan Laliberte May 29 '18 at 10:11
  • really though you shouldn't be using scriptlets for all this... they have been discouraged against since 2010 and replaced with EL and JSTL. – Jonathan Laliberte May 29 '18 at 10:12
  • 1
    [2003, actually](http://www.oracle.com/technetwork/articles/javase/code-convention-138726.html). – BalusC May 29 '18 at 10:53
  • Ah even worse then, thanks for the correction. Crazy how people are still using it. Not sure exactly where i read 2010. – Jonathan Laliberte May 29 '18 at 10:56