第八周作业

1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
 
<body bgcolor=#ccffcc>
    <script type="text/javascript">
        function validate() {
            if (loginForm.uname.value == "") {
                alert("账号不能为空!");
                return;
            }
            if (loginForm.upwd.value == "") {
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>
 
 
    <form name="loginForm" action="index1.jsp" method="post">
 
        用户名:<input type="text" name="uname"><br> 密码: <input
            type="password" name="upwd"> <input type="button" value="登录"
            onClick="validate()">
 
 
 
 
 
    </form>
</body>
</html>
  


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
 
<body >
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        if (uname.equals(upwd)) {
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("index2.jsp").forward(request,
                    response);
        } else {
            response.sendRedirect("index3.jsp");
        }
    %>
</body>
</html>
  


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
 
<body bgcolor=#FFccff>
    <%
        String uname = (String) session.getAttribute("uname");
        //如果他是空,说明没登陆,直接访问该页面了
        if (uname == null)
            response.sendRedirect("index.jsp");
    %>
    欢迎你<%=uname%><br>
    <br>
    <a href="index4.jsp">退出登录</a>
</body>
</html>
  


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
   
  <body bgcolor=#bgcclo>
    <h1>登陆失败!!</h1>
    <%response.setHeader("refresh", "5;url=index.jsp");%>
  </body>
</html>
  


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
 
<body>
    <%
        session.invalidate();
        response.setHeader("refresh", "5;url=index.jsp");
    %>
    <h1>退出登录成功!</h1>
</body>
</html>
  

 

 

 

posted @ 2022-04-24 14:22  计算机1901王怡  阅读(4)  评论(0编辑  收藏  举报