用web代码 1、 设计一个用户的登录注册页面(登录界面),实现用户的登录注册功能 2、 创建JavaBean类,该类可以存储餐单相关的数据,比如菜名,价格等 3、 创建一个包含所有菜品的JSP页面(菜单界面),用户点击相关的菜名,跳转到相应的界面可以查到相关菜品的相关信息(菜品详情界面),并可将菜品加入购物车 4、 设计一个查看购物车的JSP页面(购物车界面),可以显示购物车中的菜品,并显示总价格,也可以修改购物车中的商品。 创建servlet类,主要用于处理菜品显示,查看购物车,添加商品到购物车等操作。
时间: 2023-06-24 20:09:27 浏览: 104
JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
5星 · 资源好评率100%
以下是一个简单的示例代码:
1. 登录注册页面(login.jsp):
```
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="loginServlet" 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>
<p>Not a member yet? <a href="register.jsp">Register here</a></p>
</body>
</html>
```
2. JavaBean类(Menu.java):
```
public class Menu {
private String name;
private double price;
// getters and setters
}
```
3. 菜单页面(menu.jsp):
```
<!DOCTYPE html>
<html>
<head>
<title>Menu</title>
</head>
<body>
<h1>Menu</h1>
<ul>
<li><a href="menuDetails.jsp?name=Spaghetti">Spaghetti</a></li>
<li><a href="menuDetails.jsp?name=Pizza">Pizza</a></li>
<li><a href="menuDetails.jsp?name=Burger">Burger</a></li>
<!-- more menu items -->
</ul>
</body>
</html>
```
4. 菜品详情页面(menuDetails.jsp):
```
<!DOCTYPE html>
<html>
<head>
<title><%= name %></title>
</head>
<body>
<h1><%= name %></h1>
<p>Price: <%= price %></p>
<form action="addToCartServlet" method="post">
<input type="hidden" name="name" value="<%= name %>">
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" value="1">
<input type="submit" value="Add to Cart">
</form>
</body>
</html>
```
5. 查看购物车页面(cart.jsp):
```
<!DOCTYPE html>
<html>
<head>
<title>Cart</title>
</head>
<body>
<h1>Cart</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<% for (CartItem item : cartItems) { %>
<tr>
<td><%= item.getName() %></td>
<td><%= item.getPrice() %></td>
<td>
<form action="updateCartServlet" method="post">
<input type="hidden" name="name" value="<%= item.getName() %>">
<input type="number" name="quantity" value="<%= item.getQuantity() %>">
<input type="submit" value="Update">
</form>
</td>
<td><%= item.getSubtotal() %></td>
<td>
<form action="removeFromCartServlet" method="post">
<input type="hidden" name="name" value="<%= item.getName() %>">
<input type="submit" value="Remove">
</form>
</td>
</tr>
<% } %>
</tbody>
<tfoot>
<tr>
<td colspan="3">Total:</td>
<td><%= cart.getTotal() %></td>
<td></td>
</tr>
</tfoot>
</table>
</body>
</html>
```
6. Servlet类(LoginServlet.java, AddToCartServlet.java, UpdateCartServlet.java, RemoveFromCartServlet.java):
```
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// check username and password in database
if (isValidUser) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("menu.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
public class AddToCartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
int quantity = Integer.parseInt(request.getParameter("quantity"));
// add item to cart
response.sendRedirect("cart.jsp");
}
}
public class UpdateCartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
int quantity = Integer.parseInt(request.getParameter("quantity"));
// update item quantity in cart
response.sendRedirect("cart.jsp");
}
}
public class RemoveFromCartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
// remove item from cart
response.sendRedirect("cart.jsp");
}
}
```
阅读全文