我目前正在制作一个网站,您可以在其中制作松饼。松饼由Topping和Bottom组成。它通过2个表格,底部和顶部的名称和价格。这是我用来编写每个项目的功能,无论是打顶还是打底:
public String writeTopping(){
String output = "";
String topping;
double price;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection myConn = DriverManager.getConnection("jdbc:mysql://", "asdasd", "asdasdas");
java.sql.Statement mySt = myConn.createStatement();
String rs = "SELECT * FROM topping";
ResultSet myRS = mySt.executeQuery(rs);
while (myRS.next()) {
topping = myRS.getString("topping");
price = myRS.getDouble("price");
topping tempTop = new topping(topping, price);
toppingCake.add(tempTop);
output += "<form method ='POST' action='Topping'><div class='panel panel-primary'><div class='panel-heading'>" + topping + "</div><div class='panel-body'><img src='http://placehold.it/150x80?text=IMAGE' class='img-responsive' style='width:100%' alt='Image'></div><div class='panel-footer'>Price: "+ price +"</div><button type='submit'>Add To Basket</button><input type='hidden' name='topping' value='"+topping+"'></div>";
}
myConn.close();
} catch (SQLException | HeadlessException ex) {
JOptionPane.showMessageDialog(null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
items.add(output);
return output;
}
单击提交时,在任一项上,servlet会设置您选择的顶部属性:
String toppingPage = request.getParameter("topping");
switch (toppingPage) {
case "Chocolate":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Blueberry":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Rasberry":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Crispy":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Strawberry":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Raisin":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Orange":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Lemon":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
case "Blue Cheese":
request.setAttribute("topping", toppingPage);
response.sendRedirect("secondPage.jsp");
break;
}
在最后一页,我会用
显示结果<% out.print(session.getAttribute("topping")); %>
我今天早些时候有这个工作,但是,我改变了一些东西,现在它因某种原因不断返回null。