📄 addbasketcontroller.java
字号:
/**
* Class <b>AddBasketController</b> contains
* the specification of a servlet that
* adds 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 AddBasketController 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 adds a book
* to 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 number of books input parameter.
int numbooks = Integer.parseInt(request.getParameter( "numbooks" ));
// Load the current http session.
HttpSession mySession = request.getSession();
// Load the basket from the session.
library.LibraryBasket Basket = (library.LibraryBasket) mySession.getAttribute( "Basket" );
// Get the isbn and quantity of the book to add to the basket.
for( int i=0; i<=numbooks; i++ )
{
System.err.println( request.getParameter( "book" + i ) );
String bookisbn = request.getParameter( "book" + i );
if (bookisbn!=null)
Basket.addItem(bookisbn);
}
// Set the basket back into the http session.
mySession.setAttribute( "Basket", Basket );
// Send the user back to the index.jsp.
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 + -