cartservlet.java
来自「精通NetBeans光盘源代码,很好很好的资料」· Java 代码 · 共 100 行
JAVA
100 行
package shop.cart;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
import javax.ejb.*;
public class CartServlet extends HttpServlet {
private ShoppingCartRemoteHome home;
private ShoppingCartRemote shoppingCart;
public void init() throws ServletException {
home = lookupHome();
super.init();
}
//Process the HTTP Post request
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//能处理中文参数乱码问题
request.setCharacterEncoding("ISO-8859-1");
//得到参数a的数值
String action = (String)request.getParameter("a");
HttpSession session = request.getSession(true);
//如果用户不是第一次访问,通过Session让用户使用已有的实例,否则建一个新的
if(session.getAttribute("cart") == null){
try{
shoppingCart = home.create();
session.setAttribute("cart",shoppingCart);
}catch(Exception ce){
ce.printStackTrace();
}
}
else{
shoppingCart = (ShoppingCartRemote)session.getAttribute("cart");
}
//处理各种请求
if(action.equals("get")){//处理请求购物车的请求
//跳转页面
forward(request,response,"/cart.jsp");
}else if (action.equals("remove")) {//处理删除购物车中商品的请求
String id = (String) request.getParameter("id");
shoppingCart.removeFromCart(id);
forward(request,response,"/cart.jsp");
}else if(action.equals("add")){//处理往购物车中加商品的请求
String id = (String) request.getParameter("id");
String title = (String) request.getParameter("title");
title=new String(title.getBytes("ISO-8859-1"),"GBK");
double price = Double.parseDouble(request.getParameter("price"));
shoppingCart.addToCart(id,title,price,1);
forward(request,response,"/video.jsp");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// Lookup the beans home using JNDI
private ShoppingCartRemoteHome lookupHome() {
ShoppingCartRemoteHome myHome = null;
try {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
ht.put(Context.PROVIDER_URL, "localhost:1099");
Context ctx = new InitialContext(ht);
Object home = ctx.lookup("ShoppingCartJndi");
myHome = (ShoppingCartRemoteHome) PortableRemoteObject.narrow(home, ShoppingCartRemoteHome.class);
} catch (Exception e) {
System.out.println("The client was unable to lookup the EJBHome.");
e.printStackTrace();
}
return myHome;
}
protected void forward(HttpServletRequest request,HttpServletResponse response,String url) throws ServletException,IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?