A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

林文辉

获得Session:  request.getSession();

将商品添加到购物车:

String pid = request.getParameter("pid");
Integer count = Integer.parseInt(request.getParameter("count"));
//封装购物项
CartItems cartItems = new CartItems();
ProductService productService = (ProductService) BeanFactory.getBean("ProductService");
cartItems.setProduct(productService.findByPid(pid));
cartItems.setCount(count);
Cart cart =getCart(request);
cart.addToCart(cartItems);
response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");

销毁:三种情况销毁session:
session过期. 默认过期时间为30分钟.
非正常关闭服务器.如果正常关闭session序列化到硬盘.
手动调用session.invalidate();

验证码的校验:
String code1 =  request.getParameter("code");
String code2 = (String)request.getSession().getAttribute("code");
request.getSession().removeAttribute("code");
//校验验证码是否一致
if(!code1.equalsIgnoreCase(code2)) {
request.setAttribute("msg", "验证码输入错误");
return "/jsp/login.jsp";
                        }

切换验证码:
$(function(){
                $("#ImgCheck").click(function(){
                        $(this).attr("src","${pageContext.request.contextPath}/CheckImgServlet?time="+new Date().getTime());
                });
        });

<%!   %>                :翻译成Servlet中的成员内容. 定义变量,方法,类. -- 不建议.
<%    %>                :翻译成Servlet中service方法内部的内容. 定义类,变量
<%=   %>                :翻译成Servlet中service方法中out.print();

Jsp有9大内置对象:
request
response
session
application
page
pageContext
config
out
exception
jsp4个领域有效范围:
pageContext:当前页面
request:响应之前
session:一次会话
applicatiton或者servletContext:只要服务器正常运行。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马