Practice of JSP Built-in Objects

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
</head>
<body>
<form action="doLogin.jsp" method="post">
    <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
        <tr>
            <td>密&nbsp;&nbsp;码:</td>
            <td><input type="password" name="pwd"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="checkbox" name="mdl"/>7日内免登录</td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" value="重置"></td>
        </tr>
    </table>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
    <%
        //设置请求对象的字符集为utf-8
        request.setCharacterEncoding("UTF-8");

        //获取用户名和密码信息
        String username = request.getParameter("name");
        String password = request.getParameter("pwd");
        /*
        如果输入的用户名、密码不正确,则跳转到login.jsp页面重新输入,
        否则,跳转到welcome.jsp文件
        */
        if(username.equals("lxy") && password.equals("123456")){
            Cookie cookie1 = new Cookie("username", username);
            Cookie cookie2 = new Cookie("password", password);
            cookie1.setMaxAge(7*24*60*60);
            cookie2.setMaxAge(7*24*60*60);
            response.addCookie(cookie1);
            response.addCookie(cookie2);
            session.setAttribute("username", username);
            session.setAttribute("password", password);
            response.sendRedirect("welcome.jsp");
        } else{
            response.sendRedirect("login.jsp");
        }
    %>
</body>
</html>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>
        <%-- 使用request对象获取客户端IP地址,并显示出来 --%>
        IP地址:<%= request.getRemoteAddr()%><br>
        <!-- 显示系统当前时间 -->
        现在时间:<%= new Date().toLocaleString()%>
    </h1>
    <%
        response.setHeader("refresh", "1");//设置每秒刷新一次
    %>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>显示客户端请求信息</title>
</head>
<body>
    <%
        //获取并显示客户端提交请求的方式
        String method = request.getMethod();
        //获取并显示客户端请求的URL
        StringBuffer url = request.getRequestURL();
        //获取并显示客户端请求的URI
        String uri = request.getRequestURI();
        //获取并显示客户端请求的查询字符串
        String str = request.getQueryString();
        //获取并显示客户端提交请求的服务器端路径
        String path = request.getServletPath();
        //获取并显示协议名称
        String host = request.getProtocol();
        //获取并显示服务器名称
        String server = request.getServerName();
        //获取并显示服务器端口号
        int port = request.getServerPort();
        //获取并显示客户端的IP地址
        String addr = request.getRemoteAddr();

        out.println("提交请求的方式:" + method + "<br>");
        out.println("请求的URL地址:" + url + "<br>");
        out.println("请求的URI:" + uri + "<br>");
        out.println("请求查询的字符串:" + str + "<br>");
        out.println("客户端提交请求的服务器路径:" + path + "<br>");
        out.println("协议名称:" + host + "<br>");
        out.println("服务器名称:" + server + "<br>");
        out.println("服务器端口号:" + port + "<br>");
        out.println("远程客户端的IP地址:" + addr + "<br>");
    %>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String ip = request.getRemoteAddr();
        if(ip.startsWith("127", 0)){
            out.print("禁止访问!");
        }else{
            out.print("welcome:" + ip);
        }
    %>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" buffer="4kb"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <p>使用out对象管理缓冲区</p>
    <p>缓冲区大小:<%= out.getBufferSize()%></p>
    <p>缓冲区剩余大小:<%= out.getRemaining()%></p>
    <p>是否自动清空缓冲区:<%= out.isAutoFlush()%></p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        session.invalidate();<%--强制会话失败--%>
        response.sendRedirect("login.jsp");
    %>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        Integer count = (Integer) application.getAttribute("count");
        if(count == null){
            count = 0;
        }
        count++;
        application.setAttribute("count", count);
    %>
    您是给页面的第<%= count%>个访问者
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_47940048/article/details/129757778