所有知识点都来自互联网,进行总结和梳理,侵权必删。
引用来源:尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版
首先创建正常的Java项目,之后选择add framework 选择增加web相关。(这样完成了需要的src文件夹和web文件夹(有了Web.xml文件))。
随后需要设置Tomcat服务器,在右上角!
一个功能需要:
html页面,通过action设置。
<form action="http://localhost:8089/13_cookie_war_exploded/loginServlet" method="get">
用户名:<input type="text" name="username" value="${cookie.username.value}"><br>
密码:<input type="password" name="password" ><br>
<input type="submit"value="登录">
</form>
servlet文件完成功能,并响应(需要再Web.xml进行配置)。
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
是服务器与浏览器之间交互的一个键值对对象。
涉及了如何创建Cookie,如何获取,如何修改,设置持续时间。
一个应用:免用户名登录。
protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("path1", "path1");
cookie.setPath(req.getContextPath() + "/abc");
//通知客户端
resp.addCookie(cookie);
resp.getWriter().write("path1已创建");
}
protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("life3600", "life3600");
cookie.setMaxAge(60*60);
//通知客户端
resp.addCookie(cookie);
}
protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = CookieUtils.findCookie("key1", req.getCookies());
if (cookie != null){
cookie.setMaxAge(0);
//通知客户端
resp.addCookie(cookie);
resp.getWriter().write("key1的Cookie已经被删除");
}
}
protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("defaultLife", "defaultLife");
cookie.setMaxAge(-1);
//通知客户端
resp.addCookie(cookie);
}
protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 方案一
// 重复覆盖
// Cookie cookie = new Cookie("key1", "newvalue1");
// resp.addCookie(cookie);
// resp.getWriter().write("key1的Cookie值已经修改好了");
// Cookie[] cookies = req.getCookies();
//方案二 查找修改通知 cookie对值的要求很高,汉字和一些括号。如果需要的话,要求Base64编号。
Cookie cookie = CookieUtils.findCookie("key1", req.getCookies());
if (cookie != null){
cookie.setValue("newnewvalue1");
resp.addCookie(cookie);
}
}
protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies){
resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]
");
}
//如何获取想要的cookie 这个需要封装到Utils
Cookie iWantCookie = CookieUtils.findCookie("key1", cookies);
if (iWantCookie != null){
resp.getWriter().write("找到cookie啦");
}
}
protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1 创建Cookie对象
Cookie cookie = new Cookie("key1", "value1");
//2 通知客户端保存Cookie
resp.addCookie(cookie);
resp.getWriter().write("Cookie创建成功");
}
String username = req.getParameter("username");
String password = req.getParameter("password");
if ("wzg168".equals(username)&&"123456".equals(password)){
//
Cookie cookie = new Cookie("username", username);
cookie.setMaxAge(60*60*24*7);
resp.addCookie(cookie);
System.out.println("登录成功");
}else{
//fail
System.out.println("登录失败");
}
相关jsp交互页面代码。其实就是cookie.username.value起到了作用。
<form action="http://localhost:8089/13_cookie_war_exploded/loginServlet" method="get">
用户名:<input type="text" name="username" value="${cookie.username.value}"><br>
密码:<input type="password" name="password" ><br>
<input type="submit"value="登录">
</form>