📄 addbasketcontroller.java
字号:
package LL;
import LL.LibraryBasket;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddBasketController extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response){
try {
HttpSession mySession=request.getSession();
/**Load the myBasket bean from the HtttpSession.
*This returns a copy of the object in the session
*/
LibraryBasket myBasket=(LibraryBasket) mySession.getAttribute("myBasket");
LibraryBooks libraryBooks=(LibraryBooks) mySession.getAttribute("libraryBooks");
/**
* process the requst from the jsp
*/
String titles[]=request.getParameterValues("title");//选中的书名
//System.out.println(titles.length);
String lTitles[]=libraryBooks.getBookTitle();//所有书名
String bookIsbns[]=libraryBooks.getBookIsbn();//所有书对应的书isbn
if(titles!=null){
//for (int k=0;k<titles.length;k++)
// System.out.println("title"+" "+titles[k]+" 书名结束了 ");
/**
* 对每一个书名,在所有书中查找,找到后,用对应的书isbn来在cart里加一个记录
*/
for(int i=0;i<titles.length;i++){
for(int j=0;j<bookIsbns.length;j++)
if(titles[i].equals(lTitles[j])){
myBasket.addItem(bookIsbns[j]);
}
}
}else{
System.out.println("please choose some books");
}
/**
* Set the bean back into the session for later use. This
* method will associate the myBasket bean with the name "myBasket"
*/
mySession.setAttribute("myBasket",myBasket);
/**
* back to the main page
*/
RequestDispatcher rd;
rd=getServletContext().getRequestDispatcher("/showbooks.jsp");
rd.forward(request, response);
//System.out.println("come here already ^_^");//&@@@@@@@@@@@@@@
}catch(Exception e){
System.out.println(e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -