两个jsp页面之间跳转需要通过Servlet控制器实现
1 创建数据库表
-- 创建一个序列,使book的id自增长
create sequence book_seq
start with 1
increment by 1
minvalue 1
maxvalue 99999999999999
nocycle
nocache
-- 创建一个序列,使orders的id自增长
create sequence orders_seq
start with 1
increment by 1
minvalue 1
maxvalue 9999999999
nocycle
nocache
-- 用户表
create table user_(
id number primary key,
name varchar2(50) not null,
pwd varchar2(50) not null,
email varchar2(100) not null,
tel varchar2(20) not null,
grade number(2) default 1 not null
)
insert into user_ values(100, 'jiaozl', '111', 'jiaozl@qq.com', '110', 1);
insert into user_ values(101, 'xiaoming', '111', 'xiaoming@qq.com', '120', 1);
insert into user_ values(102, '小红', '111', 'xiaohong@qq.com', '130', 1);
-- 商品表
create table book(
id number primary key,
name varchar2(50) not null,
author varchar2(50) not null,
publishHouse varchar2(100) not null,
price number not null,
nums number default 1000 not null
)
insert into book values(book_seq.nextval, 'jsp应用开发详解', '小风', '电子工业出版社', 59, 100);
insert into book values(book_seq.nextval, 'java web服务开发', '谭美君', '电子工业出版社', 45, 100);
insert into book values(book_seq.nextval, 'Java编程思想', '小红', '机械工业出版社', 99, 100);
insert into book values(book_seq.nextval, 'jsp编程指南', '王芳', '电子工业出版社', 19, 1000);
insert into book values(book_seq.nextval, 'j2ee 1.4应用开发详解', '小贱', '电子工业出版社', 68, 100);
insert into book values(book_seq.nextval, 'j2ee企业级应用开发', '小菲', '电子工业出版社', 56, 1000);
insert into book values(book_seq.nextval, 'j2ee参考手册', '小风', '电子工业出版社', 56, 1000);
insert into book values(book_seq.nextval, 'j2ee web服务开发', '顺平', '电子工业出版社', 550, 100000);
-- 订单表
-- 设计思路:为了避免数据冗余,我们把公有的信息,抽取出来创建一个单独的表,把不共有的信息,建一张单独表。
create table orders(
id number primary key,
userId number references user_(id),
totalPrice number default 0 not null,
orderDate date default sysdate not null
)
create table ordersItem(
id number primary key,
ordersId number references orders(id),
bookId number references book(id),
bookNum number default 0 not null
)
2 创建login、hall、showCart等jsp页面
通过Servlet控制器实现业务化跳转
<body>
<h1>登录界面</h1>
<form action="/myshopping/GotoHallUI" method="post">
<table border="1">
<tr>
<td>用户名</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="passwd" /></td>
</tr>
<tr>
<td><input type="submit" value="登录" /></td>
<td><input type="reset" value="清空重输" /></td>
</tr>
</table>
</form>
</body>
<body>
<h1>欢迎访问购物大厅</h1>
<table border="1">
<tr><th>书名</th><th>价格</th><th>出版社</th><th>点击购买</th></tr>
<%
ArrayList<Book> books = (ArrayList<Book>) request.getAttribute("books");
for(int i =0; i<books.size(); i++ ) {
Book book = books.get(i);
%>
<tr><td><%=book.getName() %></td><td><%=book.getPrice() %></td><td><%=book.getPublishHouse() %></td><td><a href="#">购买</a></td></tr>
<%
}
%>
<tr><td colspan="4"><input type="button" value="查看购物车" /></td></tr>
</table>
<a href="/myshopping">返回重新登录</a>
</body>
<body>
<h1>我的购物车</h1>
<a href="/myshopping/GotoHallUI"> 返回购物大厅 </a>
<form action="/myshopping/ShoppingClServlet?type=update&" method="post">
<table border="1" style="border-collapse: collapse; width: 600px; ">
<tr><th>bookID</th><th>书名</th><th>价格</th><th>出版社</th><th>数量</th><th>删除</th></tr>
<%
ArrayList<Book> books = (ArrayList<Book>) request.getAttribute("books");
for(int i=0; i<books.size(); i++) {
Book book = books.get(i);
%>
<tr>
<%-- 提交表单时,获取表格中信息 -->
<td><%=book.getId() %><input type="hidden" name="id" value="<%=book.getId() %>" /></td>
<td><%=book.getName() %></td>
<td><%=book.getPrice() %></td><td><%=book.getPublishHouse() %></td>
<td><input type="text" name="booknum" value="<%=book.getShoppingNum() %>" />本</td>
<td><a href="/myshopping/ShoppingClServlet?type=del&id=<%=book.getId() %>">删除</a></td>
</tr>
<%
}
%>
<tr>
<td colspan="6"><input type="submit" value="update" ></td>
</tr>
<tr>
<td colspan="6">购物车的总价格:${ totalPrice } 元</td>
</tr>
</table>
</form>
</body>
3 GotoHallUI 实现login.jsp—->hall.jsp
package com.test.controller;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.test.domain.Users;
import com.test.service.UserService;
public class GotoHallUI extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
// 取出从登陆页面传递的用户名和密码
String uid = request.getParameter("uid");
String p = request.getParameter("passwd");
Users loginUser = new Users(Integer.parseInt(uid), p);
UserService userService = new UserService();
Boolean b = userService.checkUser(loginUser);
if(b) {
// 创建购物车
MyCart mycart = new MyCart();
// 在哪里都能访问到
request.getSession().setAttribute("mycart", mycart);
// 获取书籍信息
BookService bookService = new BookService();
ArrayList<com.test.domain.Book> books = bookService.getAllBook();
request.setAttribute("books", books);
// 跳转
request.getRequestDispatcher("WEB-INF/hall.jsp").forward(request, response);;
} else {
request.getRequestDispatcher("WEB-INF/login.jsp").forward(request, response);;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}