📄 catalogservlet.java
字号:
package examples;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
/**
* 本servlet向客户展示图书目录
*/
public class CatalogServlet extends HttpServlet {
private CatalogHome catalogHome = null;
/**
* 初始化servlet实例
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
/*
* 获取并记录部署信息
*/
String initCtxFactory = getInitParameter(Context.INITIAL_CONTEXT_FACTORY);
String providerURL = getInitParameter(Context.PROVIDER_URL);
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, initCtxFactory);
env.put(Context.PROVIDER_URL, providerURL);
/*
* 获取初始的JNDI上下文
*/
Context ctx = new InitialContext(env);
/*
* 查找Home对象
*/
catalogHome = (CatalogHome ) ctx.lookup("CatalogHome");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 回应客户端的HTTP请求
*/
public void service (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Vector books=null;
try {
/*
* 获取客户的HttpSession和所用的购书车
*/
HttpSession session = request.getSession(false);
if (session == null) {
/*
* 如果和客户的会话不存在
* 则把他重定向到登录界面
*/
response.sendRedirect(response.encodeRedirectURL("/BookStore/login"));
return;
}
Object obj = session.getAttribute("cart");
if (obj == null) {
/*
* 如果和客户的会话不存在
* 则把他重定向到登录界面
*/
response.sendRedirect(response.encodeRedirectURL("/BookStore/login"));
return;
}
Cart cart = (Cart) obj;
String bookIDToAdd = request.getParameter("Buy");
books=(Vector)session.getAttribute("books");
String bookId=(String)request.getParameter("bookId");
/*
* 客户点击'购买'
*/
if (bookIDToAdd != null) {
/*
* 把书加入购书车
*/
try {
BookItem item=getBookItem(books,bookIDToAdd);
cart.add(new LineItem(item,1,0));
// 设置购买标记
request.setAttribute("BookPurchased", item.getName());
// 把购买请求转发给catalog JSP
this.getServletContext().getRequestDispatcher("/catalog.jsp").forward(request, response);
return;
} catch (Exception e) {
throw new ServletException(e.toString());
}
}
/*
* 客户点击'察看图书细节'
*/
else if(bookId!=null){
//查出图书信息并发送给请求对象
request.setAttribute("bookItem",getBookItem(books,bookId));
this.getServletContext().getRequestDispatcher("/bookInfo.jsp").forward(request, response);
return;
}
else {
if(books==null){
Catalog catalog=catalogHome.create();
books=catalog.getBookItemList();
session.setAttribute("books", books);
}
// 转发请求至 catalog JSP
this.getServletContext().getRequestDispatcher("/catalog.jsp").forward(request, response);
return;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 返回代表给定的某本图书的 Bookitem
*/
private BookItem getBookItem(Vector books,String bookIDToAdd){
int size=books.size();
for(int i=0; i<size; i++){
BookItem item=(BookItem)books.elementAt(i);
if(item.getBookID().equals(bookIDToAdd)){
return item;
}
}
return null;
}
public String getServletInfo() {
return "The Catalog servlet adds books to the user's " +
"cart and prints the catalog.";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -