JSP第八次作业

1.

<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form name="formName" action="dologin.jsp" method="post">

    <input name="uname"><br>
    <input type="password" name="password"><br>
    <input type="button" value="登录" onclick="login()">
  </form>
  <script type="text/javascript">
    function login(){
      if(formName.uname.value==""||formName.password.value==""){
        alert("账号或密码不能为空!");
        return;
      }
      formName.submit();
    }
  </script>
  </body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String uname = request.getParameter("uname");
    String password = request.getParameter("password");
    if (uname.equals(password)) {
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request,
                response);
    } else {
%>
<script type="text/javascript">
    alert("登录失败");
</script>
<%
        request.getRequestDispatcher("login.jsp").forward(request,
                response);
    }
%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    if (session.getAttribute("uname") == null) {
        response.sendRedirect("login.jsp");
    } else {
        String uname = (String) session.getAttribute("uname");
        out.print("欢迎" + uname + "</br>");
%>
<a href="logout.jsp">退出登录</a>
<%
    }
%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%session.invalidate(); %>
<script type="text/javascript">
    window.location.href="main.jsp";
</script>
</body>
</html>

 

 

 

 2.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form name="formName" action="dologin.jsp" method="post">

    <input name="uname"><br>
    <input type="password" name="password"><br>
    <input type="button" value="登录" onclick="login()">
  </form>
  <script type="text/javascript">
    function login(){
      if(formName.uname.value==""||formName.password.value==""){
        alert("账号或密码不能为空!");
        return;
      }
      formName.submit();
    }
  </script>
  </body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String uname = request.getParameter("uname");
    String password = request.getParameter("password");
    if (uname.equals(password)) {
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request,
                response);
    } else {
%>
<script type="text/javascript">
    alert("登录失败");
</script>
<%
        request.getRequestDispatcher("login.jsp").forward(request,
                response);
    }
%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String uname = (String) session.getAttribute("uname");
    //如果他是空,说明没登陆,直接访问该页面了
    if (uname == null)
        response.sendRedirect("../login/login.jsp");
%>
欢迎你<%=uname%><br>
<br>
<a href="logout.jsp">退出登录</a>
<hr>
商品展示:
<br> 欢迎进入水果商城:
<br>
<hr>
<form action="show.jsp" method="post">
    请选择要加入购物车的商品:<br> <input type="checkbox" name="list" value="荔枝">
    荔枝 9.99/kg<br> <input type="checkbox" name="list" value="桃子">
    桃子 6.88/kg<br> <input type="checkbox" name="list" value="西瓜">
    西瓜 7.99/kg<br>
    <hr>
    <input type="submit" value="加入购物车">

</form>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>购物车添加的商品有:</h2>
<%
    request.setCharacterEncoding("utf-8");
    String listName[] = request.getParameterValues("list");
    if (listName == null) {
        out.print("购物车为空!");
    } else {
        for (int i = 0; i < listName.length; i++) {
            out.print("(" + (i + 1) + ")" + listName[i] + "<br>");
        }
    }
%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 97442
  Date: 2022/4/24
  Time: 20:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%session.invalidate(); %>
<script type="text/javascript">
    window.location.href="main.jsp";
</script>
</body>
</html>

 

 

 

 

 

posted @ 2022-04-24 20:26  王智达  阅读(20)  评论(0编辑  收藏  举报