JSP第八周作业

1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,
主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,
访问不了,直接跳到登陆页。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
</head>

<body>
    <script type="text/javascript">
        function login() {
            if (logindo.admin.value =="") {
                alert("账号不能为空");
                return;
            } else if (logindo.password.value == "") {
                alert("密码不能为空");
                return;
            }
            logindo.submit();
        }
    </script>
    <form action="t02.jsp" name="logindo">
        用户名: 
        <input type="text" name="admin" size=6><br>
         密码:
          <input type="password" name="password" size=6><br>
          <input type="button" value="登录" onclick="login()" />
          </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>

<body>
    <%
        String admin = request.getParameter("admin");
        String password = request.getParameter("password");
        if (admin.equals(password)) {
            session.setAttribute("admin", admin);
            request.getRequestDispatcher("ok.jsp").forward(request,response);
        } else {
            response.sendRedirect("no.jsp");
        }
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
  <%
  String admin=(String)session.getAttribute("admin");
  if(admin==null)
  response.sendRedirect("t01.jsp");
   %>
   欢迎你<%=admin %>
   <a href="t03.jsp">退出登录</a>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
    <%
     
    session.invalidate();
    response.setHeader("refresh", "5;url=t01.jsp");
     
     %>
    退出成功!
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>


<title>My JSP 'index.jsp' starting page</title>

</head>

<body>
    <h1>用户名与密码不一致登陆失败!!</h1>
    <%
    session.invalidate();
    response.setHeader("refresh", "5;url=t01.jsp");%>

</body>
</html>

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

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
  </head>
  
  <body>
  <%
  String admin=(String)session.getAttribute("admin");
  if(admin==null)
  response.sendRedirect("t01.jsp");
   %>
   欢迎你<%=admin %>
    <form action="show.jsp" method="post">
        商品:<br> <br>
    口红 213<input type="checkbox" name="item" value="口红">加入购物车 <br> 
    粉底液 321 <input type="checkbox" name="item" value="粉底液"> 加入购物车 <br> 
    香水 568<input type="checkbox" name="item" value="香水"> 加入购物车<br><br>
    <input type="submit" value="加入购物车">
    </form>
   <a href="t03.jsp">退出登录</a>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>show</title>
    
  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    String itemName[]=request.getParameterValues("item");
    if(itemName==null){
    out.print("未添加购物车");
    }
    else{
    for(int i=0;i<itemName.length;i++){
    out.print("("+(i+1)+")"+itemName[i]+"<br>");
    }
    }
     %>
  </body>
</html>

 

 

posted @ 2022-04-20 20:35  Miraitowa~~~  阅读(29)  评论(0编辑  收藏  举报