4.Servlet

4.Servlet

1.请求转发中的路径问题

请求转发路径总结:
1.以/开头的路径是绝对路径,不以/开头的是相对路径
2.绝对路径以当前项目部署名为根路径(webapp),绝对路径后不需要写当前项目部署名
3…/代表上一层路径
4.servlet的相对路径是相对于url_pattern中的路径,是虚拟的路径

package com.example.demo;/**
 * @Author:zhoayu
 * @Date:2023/11/5 14:12
 * @Description:com.example.demo
 * @version:1.0
 */

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName Servlet1
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */

@WebServlet(urlPatterns = "/servlet1.do") // urlPatterns指出当前servlet在项目中的位置(可以和当前servlet类的实际位置不同)
public class Servlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过请求转发跳转至firstpage.html

        //请求转发中的路径写法:
        //1.相对路径(相对于当前servlet在项目中的位置,urlPattern决定当前servlet在项目中的位置,当前的urlPattern指出这个servlet在项目的根目录下)
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("firstpage.html");
        requestDispatcher.forward(req,resp);
        //2.绝对路径(永远以项目(webapp)作为基准路径开始找,ps:请求转发不允许跨项目)
        RequestDispatcher requestDispatcher1 = req.getRequestDispatcher("/firstpage.html");
        requestDispatcher1.forward(req,resp);
    }
}
 

2.响应重定向中的路径问题

请求转发和响应重定向

package com.example.demo;/**
 * @Author:zhoayu
 * @Date:2023/11/5 14:27
 * @Description:com.example.demo
 * @version:1.0
 */

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName Servelt2
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/c1/c2/servlet2.do")
public class Servelt2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //响应重定向到a1.html
        //1.相对路径
        resp.sendRedirect("../../firstpage.html");

        //2.绝对路径(以项目的部署路径为基准路径,即tomcat的webapps的路径,而不是具体的web项目)
        //这点和请求转发不一样
        //在响应重定向的绝对路径中,我们要加上项目的部署名
        resp.sendRedirect("/demo_war_exploded/firstpage.html");

        //为了防止拿错项目的部署名 我们可以这样
        ServletContext servletContext = this.getServletContext();
        //contextPath就是当前项目的部署路径:/demo_war_exploded,用这种方式来避免硬编码
        String contextPath = servletContext.getContextPath();
        resp.sendRedirect(contextPath+"/firstpage.html");
    }
}

3.会话管理概念引入

Cookie对象与HttpSession对象简介

Cookie对象与HttpSession对象是维护客户端(浏览器)与服务端的会话状态的两个对戏那个。**由于HTTP协议是一个无状态的协议,所以服务端并不会记录当前客户端浏览器的访问状态,**但是有些时候我们是需要服务端能够记录客户端浏览器的访问状态的,如获取当前客户端浏览器的访问服务端的次数时就需要会话状态的维持。在Servlet中提供了Cookie对象与HttpSession对象用于维护客户端与服务端的绘画状态的维持。二者不同的是Cookie是通过客户端浏览器实现会话的维持,而HttpSession是通过服务端来实现会话的维持。

用银行卡来举例:
举例
**会话管理:**用于解决HTTP协议本身不具备直接记录用户状态的功能,HTTP是一个简单的请求-响应协议,它无法记录访问状态。JAVAEE标准下给我们提供了cookie和session技术来帮助我们记录用户的状态。
cookie就是在用户端保存少量文本数据的一种技术,HttpSession是保存更多数据在服务端的一种技术。他俩共同来记录访问的状态。eg:cookie就是银行卡号,session就是银行存的账户信息(里面可能会存更多信息,比如钱数,用户信息等)。cookie是找到对应HttpSession的凭证。
有了cookie和httpsession后我们能做到的事情:1.记录用户是否有登录过。2.用户权限的控制。3.统计在线人数等(还要配合后面要学的监听器才能实现。)

4.Cookie和Session的引入

Cookie和HttpSession
用户端在访问服务x时,会携带服务x的cookie,在访问别的服务的时候,一般不会跨域携带cookie。(如果要让它跨域携带cookie,需要做一些别的设置)。
cookie和httpsession

5.响应Cookie

Cookie是一种保存少量信息至浏览器的一种技术,第一次请求时,服务器可以响应给浏览器一些cookie信息。第二次请求时,浏览器会携带之前的cookie发送给服务器,通过这种机制可以实现在浏览器保留一些用户信息作为凭证,为服务端获取用户状态获得依据。
Cookie对象的特点:

  • Cookie使用字符串存储数据
  • Cookie使用Key与Value结构存储数据
  • 单个Cookie存储数据大小限制在4097个字节
  • Cookie存储的数据中不支持中文(Servlet4.0开始支持)
  • Cookie是与域名绑定,所以不支持跨越一级域名访问
  • Cookie对象保存在客户端浏览器内存或系统磁盘上
  • Cookie分为持久化Cookie(保存在磁盘上)与状态Cookie(保存在内存上)
  • 浏览器在保存同一域名时所返回Cookie的数量是有限的。不同浏览器支持的数量不同,Chrome浏览器为50个。比如A用户访问一个服务的时候,最多给A用户存储这个服务返回的50个服务(每一个用户每一个服务能存储的cookie是50个)
  • 浏览器每次请求时都会把当前访问的域名相关的Cookie在请求中提交到服务端

Cookie

/*
 * Copyright (c) 1997-2018 Oracle and/or its affiliates. All rights reserved.
 * Copyright 2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package javax.servlet.http;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 *
 * Creates a cookie, a small amount of information sent by a servlet to 
 * a Web browser, saved by the browser, and later sent back to the server.
 * A cookie's value can uniquely 
 * identify a client, so cookies are commonly used for session management.
 * 
 * <p>A cookie has a name, a single value, and optional attributes
 * such as a comment, path and domain qualifiers, a maximum age, and a
 * version number. Some Web browsers have bugs in how they handle the 
 * optional attributes, so use them sparingly to improve the interoperability 
 * of your servlets.
 *
 * <p>The servlet sends cookies to the browser by using the
 * {@link HttpServletResponse#addCookie} method, which adds
 * fields to HTTP response headers to send cookies to the 
 * browser, one at a time. The browser is expected to 
 * support 20 cookies for each Web server, 300 cookies total, and
 * may limit cookie size to 4 KB each.
 * 
 * <p>The browser returns cookies to the servlet by adding 
 * fields to HTTP request headers. Cookies can be retrieved
 * from a request by using the {@link HttpServletRequest#getCookies} method.
 * Several cookies might have the same name but different path attributes.
 * 
 * <p>Cookies affect the caching of the Web pages that use them. 
 * HTTP 1.0 does not cache pages that use cookies created with
 * this class. This class does not support the cache control
 * defined with HTTP 1.1.
 *
 * <p>This class supports both the Version 0 (by Netscape) and Version 1 
 * (by RFC 2109) cookie specifications. By default, cookies are
 * created using Version 0 to ensure the best interoperability.
 *
 * @author	Various
 */
public class Cookie implements Cloneable, Serializable {

    private static final long serialVersionUID = -6454587001725327448L;

    private static final String TSPECIALS;

    private static final String LSTRING_FILE =
        "javax.servlet.http.LocalStrings";

    private static ResourceBundle lStrings =
        ResourceBundle.getBundle(LSTRING_FILE);

    static {
        if (Boolean.valueOf(System.getProperty("org.glassfish.web.rfc2109_cookie_names_enforced", "true"))) {
            TSPECIALS = "/()<>@,;:\\\"[]?={} \t";
        } else {
            TSPECIALS = ",; ";
        }
    }
    
    //
    // The value of the cookie itself.
    //
    
    private String name;	// NAME= ... "$Name" style is reserved
    private String value;	// value of NAME

    //
    // Attributes encoded in the header's cookie fields.
    //
    
    private String comment;	// ;Comment=VALUE ... describes cookie's use
				// ;Discard ... implied by maxAge < 0
    private String domain;	// ;Domain=VALUE ... domain that sees cookie
    private int maxAge = -1;	// ;Max-Age=VALUE ... cookies auto-expire
    private String path;	// ;Path=VALUE ... URLs that see the cookie
    private boolean secure;	// ;Secure ... e.g. use SSL
    private int version = 0;	// ;Version=1 ... means RFC 2109++ style
    private boolean isHttpOnly = false;

    /**
     * Constructs a cookie with the specified name and value.
     *
     * <p>The name must conform to RFC 2109. However, vendors may
     * provide a configuration option that allows cookie names conforming
     * to the original Netscape Cookie Specification to be accepted.
     *
     * <p>The name of a cookie cannot be changed once the cookie has
     * been created.
     *
     * <p>The value can be anything the server chooses to send. Its
     * value is probably of interest only to the server. The cookie's
     * value can be changed after creation with the
     * <code>setValue</code> method.
     *
     * <p>By default, cookies are created according to the Netscape
     * cookie specification. The version can be changed with the 
     * <code>setVersion</code> method.
     *
     * @param name the name of the cookie
     *
     * @param value the value of the cookie
     *
     * @throws IllegalArgumentException	if the cookie name is null or
     * empty or contains any illegal characters (for example, a comma,
     * space, or semicolon) or matches a token reserved for use by the
     * cookie protocol
     *
     * @see #setValue
     * @see #setVersion
     */
    public Cookie(String name, String value) {
        if (name == null || name.length() == 0) {
            throw new IllegalArgumentException(
                    lStrings.getString("err.cookie_name_blank"));
        }
        if (!isToken(name) ||
                name.equalsIgnoreCase("Comment") || // rfc2019
                name.equalsIgnoreCase("Discard") || // 2019++
                name.equalsIgnoreCase("Domain") ||
                name.equalsIgnoreCase("Expires") || // (old cookies)
                name.equalsIgnoreCase("Max-Age") || // rfc2019
                name.equalsIgnoreCase("Path") ||
                name.equalsIgnoreCase("Secure") ||
                name.equalsIgnoreCase("Version") ||
                name.startsWith("$")) {
            String errMsg = lStrings.getString("err.cookie_name_is_token");
            Object[] errArgs = new Object[1];
            errArgs[0] = name;
            errMsg = MessageFormat.format(errMsg, errArgs);
            throw new IllegalArgumentException(errMsg);
        }

        this.name = name;
        this.value = value;
    }

    /**
     * Specifies a comment that describes a cookie's purpose.
     * The comment is useful if the browser presents the cookie 
     * to the user. Comments
     * are not supported by Netscape Version 0 cookies.
     *
     * @param purpose		a <code>String</code> specifying the comment 
     *				to display to the user
     *
     * @see #getComment
     */
    public void setComment(String purpose) {
        comment = purpose;
    }

    /**
     * Returns the comment describing the purpose of this cookie, or
     * <code>null</code> if the cookie has no comment.
     *
     * @return the comment of the cookie, or <code>null</code> if unspecified
     *
     * @see #setComment
     */ 
    public String getComment() {
        return comment;
    }
    
    /**
     *
     * Specifies the domain within which this cookie should be presented.
     *
     * <p>The form of the domain name is specified by RFC 2109. A domain
     * name begins with a dot (<code>.foo.com</code>) and means that
     * the cookie is visible to servers in a specified Domain Name System
     * (DNS) zone (for example, <code>www.foo.com</code>, but not 
     * <code>a.b.foo.com</code>). By default, cookies are only returned
     * to the server that sent them.
     *
     * @param domain the domain name within which this cookie is visible;
     * form is according to RFC 2109
     *
     * @see #getDomain
     */
    public void setDomain(String domain) {
        this.domain = domain.toLowerCase(Locale.ENGLISH); // IE allegedly needs this
    }

    /**
     * Gets the domain name of this Cookie.
     *
     * <p>Domain names are formatted according to RFC 2109.
     *
     * @return the domain name of this Cookie
     *
     * @see #setDomain
     */ 
    public String getDomain() {
        return domain;
    }

    /**
     * Sets the maximum age in seconds for this Cookie.
     *
     * <p>A positive value indicates that the cookie will expire
     * after that many seconds have passed. Note that the value is
     * the <i>maximum</i> age when the cookie will expire, not the cookie's
     * current age.
     *
     * <p>A negative value means
     * that the cookie is not stored persistently and will be deleted
     * when the Web browser exits. A zero value causes the cookie
     * to be deleted.
     *
     * @param expiry		an integer specifying the maximum age of the
     * 				cookie in seconds; if negative, means
     *				the cookie is not stored; if zero, deletes
     *				the cookie
     *
     * @see #getMaxAge
     */
    public void setMaxAge(int expiry) {
        maxAge = expiry;
    }

    /**
     * Gets the maximum age in seconds of this Cookie.
     *
     * <p>By default, <code>-1</code> is returned, which indicates that
     * the cookie will persist until browser shutdown.
     *
     * @return			an integer specifying the maximum age of the
     *				cookie in seconds; if negative, means
     *				the cookie persists until browser shutdown
     *
     * @see #setMaxAge
     */
    public int getMaxAge() {
        return maxAge;
    }
    
    /**
     * Specifies a path for the cookie
     * to which the client should return the cookie.
     *
     * <p>The cookie is visible to all the pages in the directory
     * you specify, and all the pages in that directory's subdirectories. 
     * A cookie's path must include the servlet that set the cookie,
     * for example, <i>/catalog</i>, which makes the cookie
     * visible to all directories on the server under <i>/catalog</i>.
     *
     * <p>Consult RFC 2109 (available on the Internet) for more
     * information on setting path names for cookies.
     *
     *
     * @param uri		a <code>String</code> specifying a path
     *
     * @see #getPath
     */
    public void setPath(String uri) {
        path = uri;
    }

    /**
     * Returns the path on the server 
     * to which the browser returns this cookie. The
     * cookie is visible to all subpaths on the server.
     *
     * @return		a <code>String</code> specifying a path that contains
     *			a servlet name, for example, <i>/catalog</i>
     *
     * @see #setPath
     */ 
    public String getPath() {
        return path;
    }

    /**
     * Indicates to the browser whether the cookie should only be sent
     * using a secure protocol, such as HTTPS or SSL.
     *
     * <p>The default value is <code>false</code>.
     *
     * @param flag if <code>true</code>, sends the cookie from the browser
     * to the server only when using a secure protocol; if <code>false</code>,
     * sent on any protocol
     *
     * @see #getSecure
     */
    public void setSecure(boolean flag) {
        secure = flag;
    }

    /**
     * Returns <code>true</code> if the browser is sending cookies
     * only over a secure protocol, or <code>false</code> if the
     * browser can send cookies using any protocol.
     *
     * @return <code>true</code> if the browser uses a secure protocol,
     * <code>false</code> otherwise
     *
     * @see #setSecure
     */
    public boolean getSecure() {
        return secure;
    }

    /**
     * Returns the name of the cookie. The name cannot be changed after
     * creation.
     *
     * @return the name of the cookie
     */
    public String getName() {
        return name;
    }

    /**
     * Assigns a new value to this Cookie.
     * 
     * <p>If you use a binary value, you may want to use BASE64 encoding.
     *
     * <p>With Version 0 cookies, values should not contain white 
     * space, brackets, parentheses, equals signs, commas,
     * double quotes, slashes, question marks, at signs, colons,
     * and semicolons. Empty values may not behave the same way
     * on all browsers.
     *
     * @param newValue the new value of the cookie
     *
     * @see #getValue
     */
    public void setValue(String newValue) {
        value = newValue;
    }

    /**
     * Gets the current value of this Cookie.
     *
     * @return the current value of this Cookie
     *
     * @see #setValue
     */
    public String getValue() {
        return value;
    }

    /**
     * Returns the version of the protocol this cookie complies 
     * with. Version 1 complies with RFC 2109, 
     * and version 0 complies with the original
     * cookie specification drafted by Netscape. Cookies provided
     * by a browser use and identify the browser's cookie version.
     * 
     * @return			0 if the cookie complies with the
     *				original Netscape specification; 1
     *				if the cookie complies with RFC 2109
     *
     * @see #setVersion
     */
    public int getVersion() {
        return version;
    }

    /**
     * Sets the version of the cookie protocol that this Cookie complies
     * with.
     *
     * <p>Version 0 complies with the original Netscape cookie
     * specification. Version 1 complies with RFC 2109.
     *
     * <p>Since RFC 2109 is still somewhat new, consider
     * version 1 as experimental; do not use it yet on production sites.
     *
     * @param v	0 if the cookie should comply with the original Netscape
     * specification; 1 if the cookie should comply with RFC 2109
     *
     * @see #getVersion
     */
    public void setVersion(int v) {
        version = v;
    }

    /*
     * Tests a string and returns true if the string counts as a 
     * reserved token in the Java language.
     * 
     * @param value the <code>String</code> to be tested
     *
     * @return <code>true</code> if the <code>String</code> is a reserved
     * token; <code>false</code> otherwise
     */
    private boolean isToken(String value) {
        int len = value.length();
        for (int i = 0; i < len; i++) {
            char c = value.charAt(i);
            if (c < 0x20 || c >= 0x7f || TSPECIALS.indexOf(c) != -1) {
                return false;
            }
        }

        return true;
    }

    /**
     * Overrides the standard <code>java.lang.Object.clone</code> 
     * method to return a copy of this Cookie.
     */
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * Marks or unmarks this Cookie as <i>HttpOnly</i>.
     *
     * <p>If <tt>isHttpOnly</tt> is set to <tt>true</tt>, this cookie is
     * marked as <i>HttpOnly</i>, by adding the <tt>HttpOnly</tt> attribute
     * to it.
     *
     * <p><i>HttpOnly</i> cookies are not supposed to be exposed to
     * client-side scripting code, and may therefore help mitigate certain
     * kinds of cross-site scripting attacks.
     *
     * @param isHttpOnly true if this cookie is to be marked as
     * <i>HttpOnly</i>, false otherwise
     *
     * @since Servlet 3.0
     */
    public void setHttpOnly(boolean isHttpOnly) {
        this.isHttpOnly = isHttpOnly;
    }
 
    /**
     * Checks whether this Cookie has been marked as <i>HttpOnly</i>.
     *
     * @return true if this Cookie has been marked as <i>HttpOnly</i>,
     * false otherwise
     *
     * @since Servlet 3.0
     */
    public boolean isHttpOnly() {
        return isHttpOnly;
    }
}

设置cookie到响应中

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/5 15:46
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName ServletTestCookie
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/servlettestcookie.do")
public class ServletTestCookie extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过响应对象,向浏览器响应一些cookie
        //Cookie构造器要求key和value都是String
        System.out.println("method invoked");
        Cookie cookie = new Cookie("age","10");
        resp.addCookie(cookie);
    }
}

请求ServletTestCookie,响应头中多了cookie信息
cookie设置cookie到响应中

浏览器有了cookie信息后,向当前项目下的任何一个资源发起请求(这里是index.html)
都会携带cookie信息
请求携带cookie
请求携带cookie

一次响应中设置多个cookie
cookie在没有特殊设置的情况下是保存在内存上的,关闭浏览器cookie就没有

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/5 15:46
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName ServletTestCookie
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/servlettestcookie.do")
public class ServletTestCookie extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过响应对象,向浏览器响应一些cookie
        //Cookie构造器要求key和value都是String
        System.out.println("method invoked");
        Cookie cookie = new Cookie("age","10");
        resp.addCookie(cookie);

        //可以设置多个cookie
        Cookie cookie2 = new Cookie("gender", "男");
        resp.addCookie(cookie2);

    }
}

这里gender="男"中文乱码了,cookie里不建议用中文 容易乱码。
设置多个cookie设置多个cookie
再次请求
可以看到两个cookie的信息都携带上了。
再次请求携带多个cookie
状态cookie与持久化cookie
我们的cookie目前是保存在浏览器上的,如果浏览器关闭,那么cookie就会被自动清除(状态cookie,存在内存上)。
如果我们希望浏览器长期保留cookie,就需要手动在服务端进行设置,让他成为一个持久化cookie。(比如很多网站我们一段时间不登陆,再登录仍然可以免密)

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/5 15:46
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName ServletTestCookie
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/servlettestcookie.do")
public class ServletTestCookie extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过响应对象,向浏览器响应一些cookie
        //Cookie构造器要求key和value都是String
        System.out.println("method invoked");
        Cookie cookie = new Cookie("age","10");
        //希望这个cookie让浏览器保留5分钟,5分钟内浏览器重启也不会被清除:持久化cookie
        //设置cookie的存活时间
        cookie.setMaxAge(300); //单位是秒
        resp.addCookie(cookie);

        //可以设置多个cookie
        //默认是状态cookie,浏览器重启就没有
        Cookie cookie2 = new Cookie("gender", "男");
        resp.addCookie(cookie2);
    }
}

我们也可以在浏览器中手动清除所有cookie。eg:清除chrome浏览器上的过去一小时内保存的cookie:
清除cookie

6.读取Cookie

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/5 20:22
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName ServletReadRequestCookie
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/ServletReadRequestCookie.do")
public class ServletReadRequestCookie extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取请求中的cookie,因为浏览器可能会对一个服务存多个cookie,所以返回的是一个cookie数组
        //如果请求不包含cookie,这里的getCookies返回的是一个null
        Cookie[] cookies = req.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies){
                System.out.println(cookie.getName() + "=" + cookie.getValue());
                //age=10
                //gender=男
            }
        }
    }
}

7.关于Cookie跨域(了解)

**域名分类:**域名分为顶级域、顶级域名(一级域名)、二级域名
域名分类
域名等级的区别:
一级域名比二级域名更高级,二级域名是依附于一级域名之下的附属分区域名,即二级域名是一级域名的细化分级。例如:baidu.com为一级域名,news.baidu.com为二级域名。
Cookie不支持一级域名的跨域,支持二级域名的跨域
1.访问zaomianbao.com获得的cookie
zaomianbao.com
2.访问blog.zaomianbao.com获得的cookie
blog.zaomianbao.com
3.访问test.blog.zaomianbao.com
test.blog.zaomianbao.com

8.Cookie记录访问次数

案例:通过cookie判断用户是否访问过当前Servlet
需求:当客户端第一次访问Servlet时返回:“您好,欢迎您第一次访问!”,第二次访问时返回:“欢迎您回来”

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/5 20:38
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName Servlet3
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/servlet3.co")
public class Servlet3 extends HttpServlet {

    //注意flag变量的位置,Servlet3对象的生命周期为(第一次被访问到->Tomcat容器关闭)
    //service方法每次被访问到都会被执行,这里我们要把flag变量作为Servlet3的类属性
    boolean flag = false;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如果是第一次访问当前Servlet,向浏览器响应一个cookie("servlet3","1")
        //如果是多次访问,就在次数上+1
        Cookie[] cookies = req.getCookies();

        //未登录过
        if(!flag){
            System.out.println("欢迎您第一次访问");
            flag = true;
            Cookie cookie = new Cookie("servlet3", "1");
            resp.addCookie(cookie);
        }

        //登录过
        if(cookies != null && flag){
            for (Cookie cookie : cookies){
                String name = cookie.getName();
                if("servlet3".equals(name)){
                    //创建Cookie,次数+1
                    int value = Integer.parseInt(cookie.getValue());
                    Cookie cookie1 = new Cookie("servlet3", String.valueOf(value + 1));
                    resp.addCookie(cookie1);
                    System.out.println("欢迎您第" + (value+1) + "次访问");
                }
            }
        }
    }
}

9.HttpSession的使用

我们一般不会在cookie上放太多信息,也不会放和安全相关的数据,因为cookie是保存在用户端的,它会面临着信息被泄露/篡改的风险。我们只是在cookie上保留一些简单的信息,或者是寻找后台数据的作为标志的数据。cookie并不适合保留大量的安全的数据,这些数据我们需要用HttpSession保存。
HttpSession对象
HttpSession是一种保存少量信息至服务器端的一种技术。第一次请求时,服务器会创建HttpSession,我们可以在HttpSession对象中保存一些关于用户的状态信息,并将HttpSession的JSESSIONID以Cookie形式响应给浏览器。第二次请求时,浏览器会携带之前的JSESSIONID的Cookie,发送给服务器,服务器根据JSESSIONID获取对应的HttpSession对象,通过这种技术可以解决HTTP协议本身无法记录用户状态的情况。
HttpSession对象
HtttpSession对象的特点
1.HttpSession对象保存在服务端
2.HttpSession可以存储任何类型的数据
3.HttpSession使用Key与Value结构存储数据,其中value是Object类型
4.HttpSession存储数据大小无限制(单个Cookie存储数据大小限制在4097个字节)

**一次会话:**客户端和服务端,前者的cookie消失或者后者的HttpSession消失都认为是一次会话的结束。

示例代码HttpSessionTest:

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/5 21:01
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName HttpSession
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/httpsessiontest.do")
public class HttpSessionTest extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得HttpSession对象,HttpSession是一种保存更多数据在服务器端的一种技术
        //一般保存当前登录用户的权限or其他信息(针对这一次会话做一些记录)
        /*
        * getSession方法执行内容
        * 1.从requst中尝试获得JSESSION的cookie
        * 2.如果获取失败,认为上次会话已经结束,在这里要开启一个新的会话,创建一个新的HttpSession对象并返回
        *   2.1.将新的HttpSession对象的JSESSIONID以cookie的形式设置到response对象的响应头中,响应给浏览器
        * 3.如果获取成功,尝试根据JSESSIONID在服务器内找对应的HttpSession对象
        *   3.1.如果找到了HttpSession对象,就返回
        *   3.2.如果没找到(比如服务器清除了HttpSession对象),创建新的HttpSession对象放在response的响应头中设置JSESSIONID并返回给浏览器
        * */
        HttpSession session = req.getSession();
        //向HttpSession对象中存放一些数据(key:String,value:Object)
        session.setAttribute("name","zhaoyu");
        session.setAttribute("password","12345678");
        session.setAttribute("level","A");
    }
}

示例代码HttpSessionTest2:

package com.example.demo5;/**
 * @Author:zhoayu
 * @Date:2023/11/5 21:30
 * @Description:com.example.demo5
 * @version:1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @ClassName HttpSessionTest2
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
@WebServlet(urlPatterns = "/httpsessiontest2.do")
public class HttpSessionTest2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取HttpSession对象
        HttpSession session = req.getSession();
        String name = (String) session.getAttribute("name");
        String value = (String) session.getAttribute("password");
        String level = (String) session.getAttribute("level");

        System.out.println(name); // 输出zhaoyu
        System.out.println(value); // 输出12345678
        System.out.println(level); // 输出A

        //我们也可以在这里设置这个HttpSession对象的最大不活动时间,单位是second
        //如果设为负数,则这个HttpSession的最大不活动时间是无限
        /**
         * Specifies the time, in seconds, between client requests before the 
         * servlet container will invalidate this session. 
         *
         * <p>An <tt>interval</tt> value of zero or less indicates that the
         * session should never timeout.
         *
         * @param interval An integer specifying the number of seconds 
         */
        public void setMaxInactiveInterval(int interval);
        session.setMaxInactiveInterval(1800);
        
        //获取HttpSession对象的其他信息:
        System.out.println("HttpSession对象创建时间:" + session.getCreationTime()); //返回的是一个时间戳
        System.out.println("HttpSession对象最后一次被访问时间:" + session.getLastAccessedTime()); //返回的是一个时间戳
        System.out.println("HttpSession对象的最大不活动时间:" + session.getMaxInactiveInterval()); //返回一个时间戳
    }
}

第一次请求httpsessiontest.do时,由于还没有HttpSession对象,此时getSession()方法会创建一个HttpSession对象,并将这个对象对应的JSESSIONID以cookie的形式设置在响应头中返回给浏览器
获得JSESSIONID
第二次请求httpsessiontest2.do时,会携带上JSESSIONID的cookie信息,并在服务器端根据JSESSIONID找到对应的HttpSession对象,获得我们之前往HttpSession对象中设置的name, password,level等信息。
请求携带JSESSIONID
ps:这里请求和响应的JSESSIONID不同是因为我们中间修改了代码,重新在Tomcat上部署了项目。正常是一样的。
输出HttpSession中的数据
流程总结:
**同一次会话:**当客户端和服务端的JESSIONID和HttpSession对象一直是能对应上的,这多次请求和响应就都处于同一个会话中。
哪些情况会结束会话:
1.浏览器某次请求没有携带JSESSIONID(可能由于浏览器关闭,cookie清空;或者手动清空浏览器的cookie),此时由于请求中没有JSESSIONID,getSession()方法会创建一个新的HttpSession对象,和之前的HttpSession对象就没关系了(之前的HttpSession对象因为失去引用,应该会被垃圾回收掉)。HttpSession session = req.getSession();
2.服务端丢失HttpSession对象(服务器重启;HttpSession对象到达最大不活动时间,默认是30分钟(tomcat的conf/web.xml中的30中配置的);手动销毁HttpSession对象)。如果服务端丢失了HttpSession对象,当请求过来时(请求仍会携带JSESSIONID,浏览器不知道服务器的HttpSession对象怎么样了),但是服务端的getSession()方法根据请求的JSESSIONID找不到对应的HttpSession对象,就会重新创建一个HttpSession对象,并将对应它的JSESSIONID以cookie的形式设置在响应头中返回给浏览器。
ps:浏览器/服务器任何一段不能维持JSESSIONID<->HttpSession,就算是一个会话的结束
流程总结
我们可以在自己web项目的WEB-INF/web.xml中进行HttpSession对象最大不活动时间的设置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <session-config>
    	<!--设置session的最大不活动时间为60min-->
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>

我们可以通过httpSession.invalidate()来手动设置HttpSession对象不可用(手动销毁),这种手动注销主要在注销/退出登录的场景使用。

HttpSession session = req.getSession();
session.invalidate();

设置HttpSession对象的最大不活动时间

10.单次会话登录案例

案例:通过HttpSession判断用户是否登录
需求:实现登录一次即可,在一次会话内,可以反复多次访问WEB-INF/welcome.html,如果没有登录过,跳转到登录页,登录成功后,可以访问项目结构。
组件介绍:
index.html:登录信息页面
welcome.html:登录成功后可以访问的资源
LoginServlet:用来校验登录的,登录成功将用户信息存在HttpSession中,否则返回登录页
WelcomeServlet:用来向welcome.servlet中跳转的,同时验证登录,登录过,可以直接跳转,否则返回登录页
User:用来存储一个用户的信息的实体类对象

welcome.html,放到WEB-INF下,让浏览器不能直接访问它(暂时用这种手段控制浏览器不能直接访问它,后面有更好的手段)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
this is welcome page
</body>
</html>

login.html,放到WEB-INF外,登录页让大家都能访问

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="get" action="loginservlet.do">
    用户名:<input type="text" name="username"><br/>
    密码:<input type="password" name="password"><br/>
    <input type="submit">
</form>
</body>
</html>

User实体类

package com.example.demo5.example;/**
 * @Author:zhoayu
 * @Date:2023/11/5 22:36
 * @Description:com.example.demo5.example
 * @version:1.0
 */

/**
 * @ClassName User
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
//User实体类
//正常来说,这个实体类的信息应该通过ORM框架和数据库交互拿到,这里我们就简化一些不和数据库交互了
public class User {

    private Integer uid;
    private String realname;
    private String username;
    private String password;

    public User() {
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(Integer uid, String realname, String username, String password) {
        this.uid = uid;
        this.realname = realname;
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", realname='" + realname + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

LoginServlet

package com.example.demo5.example;/**
 * @Author:zhoayu
 * @Date:2023/11/5 22:30
 * @Description:com.example.demo5.example
 * @version:1.0
 */

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName LoginServlet
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */

@WebServlet(urlPatterns = "/loginservlet.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户名和密码
        //如果用户名和密码为zhaoyu,1234567(这里简化一下不和数据库交互了),就登录成功,跳转至welcome.html,否则回到login.html
        String username = req.getParameter("username");
        String password = req.getParameter("password");


        if("zhaoyu".equals(username) && "1234567".equals(password)){
            //登录成功,跳转至welcome.html,并将用户信息放在HttpSession中,后续就不用再输入用户名密码了
            User user = new User(null,null,username,password);
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
            //响应重定向
            resp.sendRedirect(req.getContextPath()+"/welcomeservlet.do");
        }else{
            //登录失败,回到login.html
            resp.sendRedirect(req.getContextPath()+"/login.html");
        }
    }
}

WelcomeServlet

package com.example.demo5.example;/**
 * @Author:zhoayu
 * @Date:2023/11/5 22:25
 * @Description:com.example.demo5.example
 * @version:1.0
 */

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName WelcomeServlet
 * @Description //TODO 
 * @Author zhaoyu
 * @Date 2023/11/5
 */
//跳转至welcome.html(请求转发)
@WebServlet(urlPatterns = "/welcomeservlet.do")
public class WelcomeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断如果登录过,就允许跳转到welcome.html (HttpSession中如果有登录过的信息)
        //如果没有登录过,就回到登录页login.html重新登录
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");

        if(user != null){
            //登录过,允许跳转
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/welcome.html");
        }else{
            //没登录过,回到登录页
            resp.sendRedirect("login.html");
        }
    }
}

第一次访问http://localhost:8080/demo5_war_exploded/welcomeservlet.do,由于我们没有登录过,没有对应的HttpSession对象,因此走响应重定向,定向到了login.html
第一次访问
输入错误用户名,继续响应重定向到login.html
输入错误密码
输入正确用户名密码,响应重定向跳转到welcomeservlet.do
输入正确用户名密码
再次访问welcomeservlet.do,发现不用再输入用户名密码了,因为服务器已经给我们创建了HttpSession对象,从请求中也可以看到请求写到了JSESSIONID
再次请求welcomeservlet.do
如果cookie清掉我们再访问welcomeservlet.do,就会再次跳转到login.html要求我们重新登录了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值