JSP第八周作业

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

2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <%
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        if (user.equals(password)) {
            session.setAttribute("user", user);
            request.getRequestDispatcher("main.jsp").forward(request,
                    response);
        } else {
            response.sendRedirect("login.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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'main.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
 <body>
    <%
        String user = (String) session.getAttribute("user");
        if (user == null) {
            response.sendRedirect("login.jsp");
        }
    %>
    欢迎你<%=user%>
    <a href="1.jsp">退出登录</a>
    <form action="2.jsp" method="post">

        <table border="1" cellpadding="0" cellspacing="0" width="500">

            <thead>
                <tr>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>加购</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1号</td>
                    <td>1</td>
                    <td align="center"><input type="checkbox" name="sp" value="1号">
                    </td>
                </tr>
                <tr>
                    <td>2号</td>
                    <td>2</td>
                    <td align="center"><input type="checkbox" name="sp" value="2号">
                    </td>
                </tr>
                <tr>
                    <td>3号</td>
                    <td>3</td>
                    <td align="center"><input type="checkbox" name="sp" value="3号">
                    </td>
                </tr>
                <tr>
                    <td>4号</td>
                    <td>4</td>
                    <td align="center"><input type="checkbox" name="sp" value="4号">
                    </td>
                </tr>
                <tr>
                    <td>5号</td>
                    <td>5</td>
                    <td align="center"><input type="checkbox" name="sp" value="5号">
                    </td>
                </tr>
                <tr>
                    <td colspan="5"><input type="submit" value="加购">
                    </td>
                </tr>
            </tbody>
        </table>
    </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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
 <body>
    <script>
        function yz() {
            if (form.user.value == "") {
                alert('用户名不能为空');
                return;
            }
            if (form.password.value == "") {
                alert('密码不能为空');
                return;
            }
            form.submit();
        }
    </script>
    <form action="index.jsp" method="post" name="form">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="user"></td>
                <td>密码:</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td><input type="button" value="登录" onclick="yz()"></td>
            </tr>
        </table>
    </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>
    <base href="<%=basePath%>">
    
    <title>My JSP '1.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%
        session.invalidate();
        response.sendRedirect("login.jsp");
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("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>
    <base href="<%=basePath%>">
    
    <title>My JSP '2.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String[] sp = request.getParameterValues("sp");
        for (int i = 0; sp != null && i < sp.length; i++) {
            out.print(sp[i] + "<br>");
        }
    %>
</body>
</html>

 

posted @ 2022-04-24 18:47  子桑  阅读(22)  评论(0编辑  收藏  举报