Job Recruitment Website - Recruitment portal - How to make a simple shopping cart with java and jsp

How to make a simple shopping cart with java and jsp

Page jsp:

& lt%@? Peggy? language="java "? content type = " text/html; ? charset=utf-8 "

page encoding = " utf-8 " % & gt;

& lt%@? taglib? prefix="c "? uri= "

& lt%@? taglib? uri= "

& lt! DOCTYPE? html? Public? “-//W3C//DTD? XHTML? 1.0? Transition //EN "? "

& lthtml? xmlns= "

& lthead & gt

& ltmeta? . kaka.web

Import? Java . io . io exception;

Import? Java . io . printwriter;

Import? Java . util . ArrayList;

Import? Java . util . list;

Import? javax . servlet . servlet exception;

Import? javax.servlet..Kaka . entity . items;

Import? Com.kaka.entity.product;

Import? com . Kaka . service . product service;

Import? com . Kaka . service . impl . productserviceimpl;

Public? Class? Family car? Extension? HttpServlet? {

Private? Static electricity Final? Dragon? serialVersionUID? =? 1L;

ProductService? ps? =? New? ProductServiceImpl();

@ Overlay

Protected? Invalid? doPost(HttpServletRequest? req? HttpServletResponse? resp)? Throwing? ServletException,? IOException? {

//Get the id of the goods

String? proId? =? req . getparameter(" proId ");

resp . set content type(" text/html; charset = UTF-8 ");

Printmaker? A writer? =? resp . get writer();

if(null? ! =? proId? & amp& amp? ! "".Equal to (proId)){

//Return that the shopping cart was added successfully.

//system . out . println(" = = = = = = = = = = = = = "? +? proId);

//Query goods according to their id.

Try it? {

Integer? pId? =? integer . parse int(proId);

Product? Product? =? PS . findproductbyid(pId);

if(null? ! =? Product) {

//After finding the goods, construct the relevant parameters of the goods into a shopping detail and put it in the shopping cart.

Articles? It? =? New? items();

it . set proid(product . get proid());

it . set proname(product . getproname());

it . set proprice(product . get proprice());

it . setproimg(product . getproimg());

//First, judge whether there is a shopping cart within the conversation range.

List & ltItems & gt? Shops, cars? =? (list & ltItems & gt)req.getSession (). get attribute(" shopCar ");

if(null? ==? Shop car)

//Shopping cart

Shops, cars? =? New? ArrayList & ltItems & gt();

}

//Before adding the goods to the shopping cart, judge whether the shopping details have been included in the shopping cart. If yes, just modify the purchase quantity.

if(shopCar.contains(it)){

int? Index =? shop car . index of(it); //Find the location of the shopping details in the shopping cart.

Articles? Articles? =? shopcar . get(index); //Get the shopping details in the shopping cart.

items . set pronum(items . getpronum()+ 1);

}? Or what? {

shopcar . add(it);

}

//Put the shopping cart into the session for access.

req.getSession()。 setAttribute("shopCar ",shop car);

//Return

writer . print(true);

}? Or what? {

writer . print(false);

}

}? Catch? (exception? e)? {

e . printstacktrace();

writer . print(false);

}

}? Or what? {

writer . print(false);

}

writer . flush();

writer . close();

}

@ Overlay

Protected? Invalid? doGet(HttpServletRequest? req? HttpServletResponse? resp)? Throwing? ServletException,? IOException? {

DoPost (request, resp);

}

}

Background management servlet?

Parcel? com . Kaka . web;

Import? Java . io . io exception;

Import? Java . io . printwriter;

Import? Java . util . ArrayList;

Import? Java . util . list;

Import? javax . mail . fetch profile . item;

Import? javax . servlet . servlet exception;

Import? javax.servlet..Kaka . entity . items;

Import? Com.kaka.entity.product;

Import? com . Kaka . service . product service;

Import? com . Kaka . service . impl . productserviceimpl;

Public? Class? HomeCarManager? Extension? HttpServlet? {

Private? Static electricity Final? Dragon? serialVersionUID? =? 1L;

ProductService? ps? =? New? ProductServiceImpl();

@ Overlay

Protected? Invalid? doPost(HttpServletRequest? req? HttpServletResponse? resp)? Throwing? ServletException,? IOException? {

resp . set content type(" text/html; charset = UTF-8 ");

Printmaker? A writer? =? resp . get writer();

//Get parameters

String? proId? =? req . getparameter(" proId ");

String? num? =? req . getparameter(" num ");

if(null? ! =? proId? & amp& amp? null? ! =? figure

& amp& amp? ! "".Is equal to (proId)? & amp& amp? ! "".Equal to (number)) {

Try it? {

Integer? pId? =? integer . parse int(proId);

Floating? pNum? =? float . parse float(num);

//Obtain the corresponding detailed items according to the id of the goods.

//? First, judge whether there is a shopping cart within the conversation range.

List & ltItems & gt? Shop, car? =? (list & ltItems & gt)? req.getSession()。 get attribute(" shopCar ");

For (items? It? :? Shop car)

if(it.getProId()==? pId){

it . setpronum(pNum);

}

}

writer . print(true);

}? Catch? (exception? e)? {

e . printstacktrace();

}

}? Or what? {

//Delete operation

Try it? {

Integer? pId? =? integer . parse int(proId);

//Obtain the corresponding detailed items according to the id of the goods.

//? First, judge whether there is a shopping cart within the conversation range.

List & ltItems & gt? Shop, car? =? (list & ltItems & gt)? req.getSession()。 get attribute(" shopCar ");

Articles? Articles? =? null

For (items? It? :? Shop car)

if(it.getProId()==? pId){

Articles? =? It;

Break;

}

}

if(null? ! =? Project) {

shopcar . remove(items);

req.getSession()。 SetAttribute("shopCar ");

}

writer . print(true);

}? Catch? (exception? e)? {

e . printstacktrace();

}

}

writer . flush();

writer . close();

}

@ Overlay

Protected? Invalid? doGet(HttpServletRequest? req? HttpServletResponse? resp)? Throwing? ServletException,? IOException? {

DoPost (request, resp);

}

}