会话跟踪-Cookie机制-记录用户访问次数

1.cookie是什么?
答:cookie其实是一小段的文本信息,客户端请求服务器,如果服务器需要记录客户端的状态,就使用response给客户端浏览器颁发一个cookie,客户端会把cookie保存起来。当浏览器再次请求时,会把地址和cookie一起提交给服务器,服务器检查该cookie,以此来辨认浏览器的状态,服务器还可以根据需要修改cookie的值。
2.获取浏览器端提交的所有cookie:Cookie[] cookies=request.getCookie();
3.向客户端设置cookie
response.addCookie(cookie1);

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="login.jsp"%>
    

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>

<%
    request.setCharacterEncoding("UTF-8");
    String username="";
    int visitTimes=0;
    Cookie[] cookies=request.getCookies();
    for(int i=0;cookies!=null&&iif("username".equals(cookie.getName())){
            username=cookie.getValue();
        }
        else if("visitTimes".equals(cookie.getName())){
            visitTimes=Integer.parseInt(cookie.getValue());
            cookie.setValue(++visitTimes+"");
        }
    }
    if(username==null||username.trim().equals("")){
        throw new Exception("您还没有登录,请先登录。");
    }
    Cookie visitTimesCookie=new Cookie("visitTimes",Integer.toString(visitTimes));
    response.addCookie(visitTimesCookie); 
    //修改cookie,更改访问次数,服务器端向客户端设置Cookie
%>
<body>
    <form action="login.jsp" method="post">
        <table>
            <tr>
                <td>您的账号td>
                <td><%=username%>td>
            tr>
            <tr>
                <td>登陆次数td>
                <td><%=visitTimes %>td>
            tr>
            <tr>
                <td><%=request.getRequestURI() %>-----URL:<%=request.getRequestURL() %>td>
                
                
                <td><input type="button" value="刷新" onclick="location='<%=request.getRequestURI()%>?ts='+new Date().getTime();" class="button">td>
            tr>
        table>
    form>

body>
html>

//登录界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true" %>
    

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        if("POST".equals(request.getMethod())){
            Cookie usernameCookie=new Cookie("username",request.getParameter("username"));
            Cookie visitTimesCookie=new Cookie("visitTimes","0");
            response.addCookie(usernameCookie);
            response.addCookie(visitTimesCookie);
            response.sendRedirect(request.getContextPath()+"/cookie.jsp");
            return;
        }
    %>
    <form action="login.jsp" method="post">
        <table>
            <tr>
                <td>request.getContextPath():<%=request.getContextPath() %>td>
                
                <td><span style="color: red;"><%=exception.getMessage() %>span>td>
            tr>
            <tr>
                <td>账号:td>
                <td><input type="text" name="username" style="width:200px;">td>
            tr>
            <tr>
                <td>密码:td>
                <td><input type="text" name="password" style="width:200px; ">td>
            tr>
            <tr>
                <td>td>
                <td><input type="submit" value="登录" class="button">td>
            tr>
        table>
    form>
body>
html>

你可能感兴趣的:(javaee)