黑马程序员技术交流社区

标题: 【石家庄校区】day17-Session&JSP [打印本页]

作者: 账号随机生成    时间: 2018-5-21 11:49
标题: 【石家庄校区】day17-Session&JSP
day17-Session将商品添加到购物车
查看某个商品详情的时候,添加到购物车,需要将商品添加到购物车.
Session的概述
Cookie本身是有大小和个数的限制,Session没有限制.Cookie的数据保存在客户端,Session数据保存在服务器端.(Cookie和Session的区别)
Session的执行原理:基于Cookie的.
使用Session:
步骤分析代码实现public class CartServlet extends HttpServlet {        private static final long serialVersionUID = 1L;        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                // 接收商品名称:                String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");                // 创建Map集合用于保存购物信息.Map<String,Integer> Map的key是商品的名称 value是购买的数量.                Map<String,Integer> map = (Map<String, Integer>) request.getSession().getAttribute("cart");                if(map == null){                        map = new LinkedHashMap<String,Integer>();                }                // 判断购物车中是否已经买了该商品.                if(map.containsKey(name)){                        // map中已经有该商品:// * 如果购物车中已经有该商品: 获得到Map中该商品的数量+1。 存回到Map集合中.                        Integer count = map.get(name);                        count++;                        map.put(name, count);                }else{                        // map中没有该商品.// * 如果购物车中没有改商品: 将商品添加到Map集合中 数量1.                        map.put(name, 1);                }                                                // * 将Map集合保存到session中.                request.getSession().setAttribute("cart", map);                response.setContentType("text/html;charset=UTF-8");                response.getWriter().println("<h3><a href='/day11/demo2/product_list.jsp'>继续购物</a> | <a href='/day11/demo2/cart.jsp'>去结算</a></h3>");        }Session是域对象Session何时创建和销毁?作用范围针对自己项目配置session过期时间: 单位是分钟<session-config>    <session-timeout>5</session-timeout>    </session-config>进行一次性验证码的校验
在登录的页面中,需要有一个验证码的校验.
使用Session保存生成的验证码.
步骤分析:代码实现protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                // 校验验证码程序:                String code1 = request.getParameter("code");                String code2 = (String) request.getSession().getAttribute("code");                request.getSession().removeAttribute("code");                if(!code1.equalsIgnoreCase(code2)){                        request.setAttribute("msg", "验证码输入错误!");                        request.getRequestDispatcher("/demo2/login.jsp").forward(request, response);                        return ;                }   ...}使用JS控制图片切换<script type="text/javascript">         function changeImg(){                 document.getElementById("img1").src="/day11/CheckImgServlet?time="+new Date().getTime();         } </script>使用JQ控制图片切换<script>    $(function(){     $("#img1").on("click",function(){         $(this).prop("src","/day11/CheckImgServlet?time="new Date().getTime())     });       });</script>JSP在JSP的页面中显示商品的信息.
数据库中存放了很多的商品信息,现在将商品的信息全部显示到页面.
JSP的概述
什么是JSP:Java Server Pages(Java服务器端的页面)
为什么要学习JSP:SUN公司推出的Servlet自身有缺陷,没有办法与ASP,PHP进行竞争.推出了动态网页开发技术JSP.
使用JSP: JSP = HTML + Java代码 + JSP自身的东西.
执行JSP的过程:JSP翻译成Servlet,编译这个Servlet的类,生成class文件.得到执行.
JSP的脚本
%! %>        :翻译成Servlet中的成员内容. 定义变量,方法,类. -- 不建议.
<% %>        :翻译成Servlet中service方法内部的内容. 定义类,变量
<%= %>        :翻译成Servlet中service方法中out.print();
JSP的注释 (了解)
什么样的代码用什么注释,实在不清楚可以用快捷键"Ctrl+Shift+/"会自动匹配合适的注释.
JSP的指令
指令的语法:
<%@ 指令名称 属性名称=”属性值” 属性名称=”属性值” ...%>
JSP中page指令:<%@ page %> -- 设置JSP的.
<%@page import="java.util.ArrayList"%><%@page import="java.util.List"%><error-page>          <error-code>404</error-code>          <location>/404.jsp</location>  </error-page>  <error-page>          <error-code>500</error-code>          <location>/500.jsp</location>  </error-page>JSP中的include指令:只是JSP包含其他的页面.<%@ include file="logo.jsp" %><%@ include file="menu.jsp" %><h1>BODY部分</h1><%@ include file="footer.jsp" %>JSP中的taglib指令:指示JSP引入标签库.
<%@ taglib uri="标签的URI的路径" prefix="标签的别名" %>
JSP的内置对象
JSP的内置对象: 在JSP中可以直接使用的对象.
JSP中有9大内置对象:
header 1header 2header 3
requestHttpServletRequestgetParameter(),setAttribute(String name,Object value);
responseHttpServletResponsesetHeader(String name,String value);getOutputStream();getWriter();
sessionHttpSessionsetAttribute();getAttribute();
applicationServletContextsetAttribute();getAttribute();
pageObjecttoString();wait();
pageContextPageContextsetAttribute();getAttribute();
configServletConfiggetServletName();getServletContext();
outJspWriterwrite(),print();
exceptionThrowablegetMessage(),getCause(); 设置isErrorPage=”true”
page内置对象 :真实对象是Object,就是JSP翻译成Servlet后的类的引用.
out内置对象 :out和response.getWriter是不是同一个对象?区别是什么? * 不是out真实对象JspWriter ,response获得Writer是PrintWriter.
pageContext内置对象:
JSP的动作标签 列出6个
*****静态包含和动态包含的区别?(<%@ include%>和<jsp:include>)






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2