📄 clearbasketcontroller.java
字号:
/**
* Class <b>ClearBasketController</b> contains
* the specification of a servlet that
* clears all books to a library user's basket.
*
* @author CTE
* @version 1.0
*/
import library.*;
import java.util.*;
import java.io.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ClearBasketController extends HttpServlet
{
/**
* Init method called once by the servlet container
* @param conf ServletConfig servlet configuration object supplied by servlet container
* @return void
*/
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
}
/**
* method doGet()
* All GET and POST forms are handled by the same function, so process
* GET requests to the doPost method.
* @param request HttpServletRequest request object
* @param response HttpServletResponse response object
* @return void
*/
public void doGet( HttpServletRequest request, HttpServletResponse response )
{
doPost( request, response );
}
/**
* method doPost()
* Process all HTML forms using the doPost method. This clears
* the session scoped JavaBean LibraryBasket.
* @param request HttpServletRequest request object
* @param response HttpServletResponse response object
* @return void
*/
public void doPost( HttpServletRequest request, HttpServletResponse response )
{
// Get the current http session from the request object.
HttpSession mySession = request.getSession();
// Get the session scoped bean named basket from the session.
library.LibraryBasket Basket = (library.LibraryBasket) mySession.getAttribute( "Basket" );
// Get the number of books in the basket.
int numbooks = Basket.getnumBooks();
String [] ISBNS = new String[numbooks];
// Get the isbns of the books in the basket.
ISBNS=Basket.getBooks();
for( int i=0; i<numbooks; i++ )
{
// Get the quantity of this book.
int quantity = Basket.getQuantity( ISBNS[i] );
// Loop on the quantity and remove the book.
for( int j=0; j<quantity; j++ )
Basket.removeItem( ISBNS[i] );
}
// Be sure to place the bean back into the session.
mySession.setAttribute( "Basket", Basket );
try
{
response.sendRedirect( "/library/showbooks.jsp" );
}
catch( Exception e )
{
request.setAttribute( "errorstring", e.toString() );
try
{
getServletConfig().getServletContext().getRequestDispatcher("/library/error.jsp").forward(request, response);
}
catch(Exception e2)
{
// Must display this error message on the console.
System.err.println(e2.toString());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -