004-用模板引擎+Servlet写一个简单的登录网站(一)

如果还不知道模板引擎的,请“”这里哦!

上次,我们讲了模板引擎。那么这一次,我们就来 “避开乱码问题,畅享代码时光 ”吧!
我们来做一个登录的网站

再这之前,我们要了解一下Cookie和Session。

已经会了的小伙伴,就不要看了,以免浪费时间哦!等我下一篇博客发出来

Cookie

Cookie存在浏览器里面。我们可以在这里看到:
这里
这里。点进去:
cookie
cookie其实就是存在浏览器的键值对(一个名称,对应一个字符串)字符串,没那么复杂!
如何设置cookie?很简单:

public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("key", "value");
        resp.addCookie(cookie);
    }
}

前端效果:
cookie
cookie
可以看到,cookie已经设置了。

如何删除它呢?借助IDE代码提示,我们发现,没有类似removeCookie或者deleteCookie的方法。那……
其实这样就可以了:

@WebServlet(urlPatterns = "/cookie")
public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("key", "");
        cookie.setMaxAge(0);
        resp.addCookie(cookie);
    }
}

这个maxAge是cookie持续的时间,设为0,则cookie再0毫秒之后就消失了,等价于删除。
cookie
如果cookie像我这样没有消失,也没关系,值为空就可以了。
拓展:

Cookie c = new Cookie("key", "value");
c.getName(); // 获取键
c.getValue(); // 获取值
c.getMaxAge(); // 获取maxAge(持续时间)

注意!!!不要在cookie里面存对象中文字符串!中文字符串存在乱码问题,cookie不能存储对象!只能是字符串!

Session

session也是键值对,只不过它存在后端。你可以这样设置session:

@WebServlet(urlPatterns = "/cookie")
public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.setAttribute("key", "value");
    }
}

核心代码:

HttpSession session = req.getSession();
session.setAttribute("key", "value");

也可以直接这样:

req.getSession().setAttribute("key", "value");

Session和cookie不同之处有两点:

  1. 存在后端,用户(浏览器)是看不见的
  2. 值可以是任何对象

举例:

req.getSession().setAttribute("key", new Object());

不会报错!
甚至:

req.getSession().setAttribute("key", Object.class);

也不会报错!(不过这个不常用)
我们来试一下行不行。

创建User.java,代码很少,只用于测试!!!:

public class User {
    public String name;
}
@WebServlet(urlPatterns = "/session")
public class SessionDemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
    	User user = new User();
    	user.name = "aoligei";
        req.getSession().setAttribute("user", user);
        System.out.println("set session successfully!");
    }
}

访问localhost:8080/session,你会看到一片空白,很正常,不用管,看后端控制台就可以了:
session
如何取值?也很简单:

String value = (String) req.getSession().getAttribute("key");
System.out.println("session key's value: " + value);

就是要用(String)强制类型转换一下。为什么呢?因为session是可以存任何类型的,取出来默认是一个Object类型的对象,所以要这么做。

我们以User为例:

@WebServlet(urlPatterns = "/session")
public class SessionDemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User getUser = (User) req.getSession().getAttribute("user");
        System.out.println("session user's value: " + getUser);
        System.out.println("user's name: " + user.name);
    }
}

如果usernull,那么说明没有设置user这个session(项目重启后,session会被清空)
效果图:
效果图
OK,大家了解session和cookie了吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值