zoukankan      html  css  js  c++  java
  • JSP学习_01

    JSP cookie是存储在客户机的文本文件,保存了大量的轨迹信息。
    通常有三个步骤可以识别回头客:
    1)服务器脚本发送一系列cookie到浏览器,如姓名、年龄、ID等
    2)浏览器在本地存储这些信息以备不时之需
    3)下一次浏览器发送请求到服务器时,连同这些cookie信息一块发送给服务器,然后服务器使用这些信息来识别用户或者干别的事
    cookie通常早HTTP请求头中设置
    Set-Cookie:信息头中包含有键值对信息,一个GMT格林尼治时间,一个地址,一个域名
    JSP中的request对象通过getCookies()方法获得cookie信息,返回一个cookie对象的数组

    JSP设置cookie的三个步骤:
    1)创建cookie对象: Cookie cookie = new Cookie("key","value")
    2)设置有效期,单位时间为秒:cookie.setMaxAge(60*60*24*)//24小时
    3)将cookie发送至HTTP响应头中:response.addCookie(cookie)

    JSP session
    JSP是一种无状态的协议,意味着用户每次检索网页时,就要单独打开一个服务链接,因此服务器不会记录下用户先前请求的任何信息,有三种方法维持客户端和服务器端的会话。
    1)cookie:服务器可以为每一个客户建立一个唯一的session ID作为cookie代表每个客户端,但不是每个客户端的浏览器都支持cookie,不建议用这种方式维持会话
    2)隐藏表单域:一个网络服务器可以发送一个隐藏的HTML表单和一个唯一的session ID;<input type="hiden" name="sessionid" value="123">,浏览器每发送一个请求,sessionid
    就可以保存为不同浏览器的轨迹信息,隐藏表单不支持通用会话跟踪
    3)session对象:jsp利用servlet提供的HttpSession接口来识别一个用户,存储这个用户所有的访问信息

    删除session数据:
    移除一个特定的属性:public void removeAttribute(String name)
    整个session无效:public void invalidate()
    设置会话超时:public void setMaxInactiveInterval(int interval)
    配置web.xml文件:如果使用的是tomcat,可以这样配置部署描述符文件web.xml
    <session-config>
    <session-timeout>15</session-timeout>
    </session-config>
    超时以分钟为单位,tomcat中的默认超时时间为30分钟

    JSP文件上传
    JSP和HTML form共同使用,实现文件的上传任务,上传的可以是文本、图片和任何文档
    步骤:上传表单--上传处理servlet--上传跳转
    三个文件:
    upload.jsp:上传表单
    UploadServlet:上传处理servlet
    message.jsp:上传成功后跳转页面
    最主要的是处理请求的servlet
    UploadServle.java
    import ...

    /**
    * Servlet implements class UploadServlet
    */
    @WebServlet("/UploadServlet")
    public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    // 上传文件存储目录
    private static final String UPLOAD_DIRECTORY = "upload";
    // 上传配置
    private static final int MEMEORY_THRESHOLD = 1024 * 1024 * 4; //4MB
    private static final int MAX_FILE_SIEZ = 1024 * 1024 * 40; //40MB
    private static final int MAX_REQUEST_SIEZ = 1024 * 1024 * 40; //50MB

    // 上传配置及保存文件
    protected void doPost(HttpServletRequest request, HttpServletResponse response)rhrows ServletException, IOException{
    // 检测是否为多媒体上传
    if(!ServletFileUpload.isMultipartContent(request)){
    // 如果不是停止上传
    PrintWriter writer = response.getWriter();
    writer.println("错误!表单提交的格式内容必须为enctype='multipart/form-data'");
    writer.flush();
    return;
    }
    // 配置上传参数
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // 设置内存临界值,超出后则自动存储在临时目录中
    factory.setSizeThreshold(MEMEORY_THRESHOLD);
    // 设置临时存储目录
    factory.setRepository(new File(System.setProperty("java.io.tempdir")));
    //
    ServletFileUpload upload = new ServletFileUpload(factory);
    // 设置最大文件上传值
    upload.setFileSizeMax(MAX_FILE_SIEZ);
    // 设置最大请求值
    upload.setSizeMax(MAX_REQUEST_SIEZ);
    // 中文处理
    upload.serHeaderEncoding("UTF-8");
    // 构造临时路径存储上传的文件,相对当前应用的目录
    String uploadPath = getServletContext.getRealPath("./") + File.separator + UPLOAD_DIRECTORY;
    // 如果目录不存在,则创建目录
    File uploadDir = new File(uploadPath);
    if(!uploadDir.exists()){
    uploadDir.mkdir();
    }

    try{
    // 解析请求的内容,提取文件数据
    List<FileItem> formItems = upload.paraRequest(request);
    if(formItems != null && formItems.size() > 0){
    // 迭代表单数据
    for(FileItem item : formItems){
    // 处理不在表单中的数据
    if(!item.isFormField()){
    String fileName = new File(item.getName()).getName();
    String filePath = uploadPath+File.separator+fileName;
    File storeFile = new File(filePath);
    // 控制台输出上传路径名
    System.out.println(filePath);
    // 保存文件到硬盘
    item.write(storeFile);
    request.setAttribute("message","上传成功");
    }
    }
    }
    }catch(Exception e){
    requset.setAttribute("message","上传失败"+e.getMessage());
    }
    // 跳转到message.jsp;
    getServletContext().getRequestDispatcher("message.jsp").forward(request,response);
    }
    }


    <%= %>只能得到当前面定义的值
    即你在这个页里有<% int a=100%>
    你在下面才可以使用<%=a%>
    ${ }这个是el表达式(表达式语言)
    可以从上下文中得到值。

    JSP页面重定向
    要将文档移动到一个新的位置时,就需要重定向,使用response对象的sendRedirector()方法
    方法签名:
    public void response.sendRedirector(String location) throws IOException{}
    这个方法将状态码和新的网页位置作为响应发回给浏览器

  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/demo-deng/p/8036226.html
Copyright © 2011-2022 走看看