1、设计一个用户的登录注册页面(登录界面),实现用户的登录注册功能 2、创建JavaBean类,该类可以存储餐单相关的数据,比如菜名,价格等 3、创建一个包含所有菜品的JSP页面(菜单界面),用户点击相关的菜名,跳转到相应的界面可以查到相关菜品的相关信息(菜品详情界面),并可将菜品加入购物车 4、设计一个查看购物车的JSP页面(购物车界面),可以显示购物车中的菜品,并显示总价格,也可以修改购物车中的商品。 创建servlet类,主要用于处理菜品显示,查看购物车,添加商品到购物车等操作。
时间: 2023-06-27 15:01:05 浏览: 69
1、以下是一个简单的用户登录注册页面的代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login/Register Page</title>
</head>
<body>
<h1>Login</h1>
<form action="login" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
<br><br>
<h1>Register</h1>
<form action="register" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
```
2、以下是一个简单的JavaBean类的代码,用于存储餐单相关的数据:
```
public class MenuItem {
private String name;
private double price;
public MenuItem(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
}
```
3、以下是一个简单的包含所有菜品的JSP页面的代码,用户可以点击相关的菜名,跳转到相应的界面可以查到相关菜品的相关信息(菜品详情界面),并可将菜品加入购物车:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Menu Page</title>
</head>
<body>
<h1>Menu</h1>
<ul>
<li><a href="menuitem?name=Hamburger">Hamburger</a></li>
<li><a href="menuitem?name=Cheeseburger">Cheeseburger</a></li>
<li><a href="menuitem?name=French Fries">French Fries</a></li>
<li><a href="menuitem?name=Onion Rings">Onion Rings</a></li>
</ul>
</body>
</html>
```
4、以下是一个简单的查看购物车的JSP页面的代码,可以显示购物车中的菜品,并显示总价格,也可以修改购物车中的商品:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cart Page</title>
</head>
<body>
<h1>Cart</h1>
<table>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Actions</th>
</tr>
<c:forEach items="${cart}" var="item">
<tr>
<td>${item.name}</td>
<td>${item.price}</td>
<td><input type="number" min="1" value="${item.quantity}" name="quantity"></td>
<td>${item.price * item.quantity}</td>
<td><a href="removeitem?name=${item.name}">Remove</a></td>
</tr>
</c:forEach>
</table>
<p>Total: ${total}</p>
<form action="checkout" method="POST">
<input type="submit" value="Checkout">
</form>
</body>
</html>
```
创建servlet类,主要用于处理菜品显示,查看购物车,添加商品到购物车等操作。具体代码可以看下面的示例:
```
@WebServlet("/menuitem")
public class MenuItemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private List<MenuItem> items = new ArrayList<MenuItem>();
public void init() throws ServletException {
items.add(new MenuItem("Hamburger", 5.99));
items.add(new MenuItem("Cheeseburger", 6.99));
items.add(new MenuItem("French Fries", 2.99));
items.add(new MenuItem("Onion Rings", 3.99));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
for(MenuItem item : items) {
if(item.getName().equals(name)) {
request.setAttribute("item", item);
RequestDispatcher rd = request.getRequestDispatcher("/menuItem.jsp");
rd.forward(request, response);
return;
}
}
response.sendRedirect("menu");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
double price = Double.parseDouble(request.getParameter("price"));
int quantity = Integer.parseInt(request.getParameter("quantity"));
List<CartItem> cart = (List<CartItem>) request.getSession().getAttribute("cart");
if(cart == null) {
cart = new ArrayList<CartItem>();
request.getSession().setAttribute("cart", cart);
}
boolean found = false;
for(CartItem item : cart) {
if(item.getName().equals(name)) {
item.setQuantity(item.getQuantity() + quantity);
found = true;
break;
}
}
if(!found) {
cart.add(new CartItem(name, price, quantity));
}
response.sendRedirect("cart");
}
}
```
阅读全文