如何将Scriptlet值从JSP传递给Servlet

时间:2018-05-29 06:32:30

标签: jsp servlets scriptlet

我正在尝试开发简单的购物车应用程序。所以我在Derby DB中有简单的数据库表。从那里我将数据检索到JSP中的表。现在我想将这些值传递给servlet。这是我的代码部分:

  

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>

所以任何人都知道如何将这些值(例如: - <%= resultSet.getString("price") %>)传递给servlet?

1 个答案:

答案 0 :(得分:0)

如果您的servlet映射到您的doGet上,您可以这样做(没有表单):

String price = request.getParameter("price");

然后在你的servlet的doGet方法中,像通常得到任何表单参数一样得到它:

Const premiere_ligne_J = 6

Sub import_donnees_J(chemin_tem)

Application.Calculation = xlCalculationManual

Dim dataJ As Worksheet
Set dataJ = ThisWorkbook.Worksheets("Import data Sheet 1")
Dim Ctr

Application.DisplayAlerts = False


For Ctr = 1 To Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Count

Workbooks.Open (chemin_tem)
tem = ActiveWorkbook.Name

Workbooks(tem).Activate
Application.DisplayAlerts = True

Set templateJ = Workbooks(tem).Sheets("Import data Sheet 1")
dernier_client = templateJ.Range("A" & Rows.Count).End(xlUp).Row

ligne = premiere_ligne_J

For client = premiere_ligne_J To dernier_client

    'Copying data
    For col = colJ_pdl_data To colJ_rapport_precision_data
        dataJ.Cells(ligne, col) = templateJ.Cells(client, col)
    Next col

ligne = ligne + 1 
suite::
Next client

Workbooks(tem).Close SaveChanges:=False
Next Ctr
Application.Calculation = xlCalculationAutomatic
End Sub`

This fuction is almost the same for the 3 sheets to import.

The main program calls these functions 
Call Import1.import_donnees_J(chemin_tem)
Call Import2.import_donnees_V(chemin_tem)
Call Import3.import_donnees_B(chemin_tem)

Chemin_tem is defined as below : 
chemin_tem = CStr(Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1))
相关问题