java web购物车
怎么看这段代码,零基础CarServerlet.jsppublicclassShopingServletextendsHttpServlet{publicvoiddoGe...
怎么看这段代码,零基础
CarServerlet.jsp
public class ShopingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
String id=request.getParameter("id");
if(id!=null)
{
Book[]book=(Book[])session.getAttribute("book");
if(book!=null)
{
for(int i=0;i<book.length;i++)
{
if(book[i].getBookId().equals(id))
{
book[i].setid();
}
}
session.setAttribute("book", book);
}
}
response.sendRedirect("/test4E/Shopping.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Shopping.jsp
public class ShopingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
String id=request.getParameter("id");
if(id!=null)
{
Book[]book=(Book[])session.getAttribute("book");
if(book!=null)
{
for(int i=0;i<book.length;i++)
{
if(book[i].getBookId().equals(id))
{
book[i].setid();
}
}
session.setAttribute("book", book);
}
}
response.sendRedirect("/test4E/Shopping.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
} 展开
CarServerlet.jsp
public class ShopingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
String id=request.getParameter("id");
if(id!=null)
{
Book[]book=(Book[])session.getAttribute("book");
if(book!=null)
{
for(int i=0;i<book.length;i++)
{
if(book[i].getBookId().equals(id))
{
book[i].setid();
}
}
session.setAttribute("book", book);
}
}
response.sendRedirect("/test4E/Shopping.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Shopping.jsp
public class ShopingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
String id=request.getParameter("id");
if(id!=null)
{
Book[]book=(Book[])session.getAttribute("book");
if(book!=null)
{
for(int i=0;i<book.length;i++)
{
if(book[i].getBookId().equals(id))
{
book[i].setid();
}
}
session.setAttribute("book", book);
}
}
response.sendRedirect("/test4E/Shopping.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
} 展开
展开全部
首先你应该对java有个基础的了解,什么是变量,什么事关键字。我先给你每行都注释下
//公共的类,类名为ShopingServlet 继承父类HttpServlet
public class ShopingServlet extends HttpServlet {
实现父类方法doGet 意识就是通过get请求的就会进入这个方法,下面还有一个doPost方法就是通过post方式请求的会进入蔽戚doPost,至于这两个的区别:族兆doGet安全性差,参数是在浏览器连接中直接显示,然而doPost就是不会显示的安全性要高,这也是最直观的区别
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过request获取session
HttpSession session=request.getSession();
//获取参数为id的值
String id=request.getParameter("id");
//判断id的值是否为null
if(id!=null)
{
//id不为空进入这里面,在获取参数为book的值,book的值为数组类型
Book[]book=(Book[])session.getAttribute("book");
在判断book是否为null
if(book!=null)
{
//部位空进入,进行循环
for(int i=0;i<book.length;i++)
{
//判断book数组中的第i个的BookId是否和之前的参数Id相同
if(book[i].getBookId().equals(id))
{
//相同,就把book数组中的第宏穗陵i个的id赋值为空
book[i].setid();
}
}
把当前book存入session中,变量名为book
session.setAttribute("book", book);
}
}
//跳转到页面/test4E/Shopping.jsp
response.sendRedirect("/test4E/Shopping.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用上面的doGet方法
doGet(request,response);
}
}
下面的代码和这个是重复的,不知道为什么你要发布两遍,你可以对比一下
//公共的类,类名为ShopingServlet 继承父类HttpServlet
public class ShopingServlet extends HttpServlet {
实现父类方法doGet 意识就是通过get请求的就会进入这个方法,下面还有一个doPost方法就是通过post方式请求的会进入蔽戚doPost,至于这两个的区别:族兆doGet安全性差,参数是在浏览器连接中直接显示,然而doPost就是不会显示的安全性要高,这也是最直观的区别
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过request获取session
HttpSession session=request.getSession();
//获取参数为id的值
String id=request.getParameter("id");
//判断id的值是否为null
if(id!=null)
{
//id不为空进入这里面,在获取参数为book的值,book的值为数组类型
Book[]book=(Book[])session.getAttribute("book");
在判断book是否为null
if(book!=null)
{
//部位空进入,进行循环
for(int i=0;i<book.length;i++)
{
//判断book数组中的第i个的BookId是否和之前的参数Id相同
if(book[i].getBookId().equals(id))
{
//相同,就把book数组中的第宏穗陵i个的id赋值为空
book[i].setid();
}
}
把当前book存入session中,变量名为book
session.setAttribute("book", book);
}
}
//跳转到页面/test4E/Shopping.jsp
response.sendRedirect("/test4E/Shopping.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用上面的doGet方法
doGet(request,response);
}
}
下面的代码和这个是重复的,不知道为什么你要发布两遍,你可以对比一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询