JSP第六次作业

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

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>    
    <title></title>
    <script type="text/javascript">
    function v() {
        if (loginForm.uname.value=="") {
            alert("账号不能为空!");
            return;
        }
        if (loginForm.password.value=="") {
            alert("密码不能为空!");
            return;
        }
        loginForm.submit();
    }
    </script>
  </head>  
  <body>
  <form name="loginForm" action="1.oblogin.jsp" method="post">
  账号:<input type="text" name="uname"><br>
  密码:<input type="password" name="password"> <br> 
  <input type="submit" value="登录" onclick="v()">
  </form>  
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>    
    <title></title>
  </head>  
  <body>
  <%
  String uname=(String)session.getAttribute("uname");if(uname==null)
      response.sendRedirect("login.jsp");
   %>
  <a href="1.out.jsp">退出登录</a> <br>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title></title>
</head>
<body>
    登陆失败,请重试!
    <%
      response.setHeader("refresh", "5;url=login.jsp");
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title></title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        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 {
            response.sendRedirect("false.jsp");
        }
    %>

</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>    
    <title></title>
  </head>  
  <body>
  <%
    session.invalidate();
    response.setHeader("refresh", "5;url=login.jsp");
  %>
退出成功!
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title></title>
</head>

<body>
    
    <%
        String uname = (String) session.getAttribute("uname");
        if (uname == null)
            response.sendRedirect("login.jsp");
    %>

    欢迎你<%=uname%>
    <a href="logout.jsp">退出登录</a>
    <br>
    玉米¥10<a href="buy.jsp?sgname=1">购买</a><br>
           土豆¥9.9<a href="buy.jsp?sgname=2">购买</a><br>


</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title></title>
</head>
<body>
    <%
        ArrayList books = (ArrayList) session.getAttribute("sgs");
        if (sgs == null) {
            sgs = new ArrayList();
            session.setAttribute("sgs",sgs);
        }
        String sgname = request.getParameter("sgname");
        String sg01="玉米,价格:20元";
        String sg02="土豆,价格:30元";
        if (sgname != null) {
            sgname = new String(sgname.getBytes("ISO-8859-1"), "utf-8");
            if(sgname.equals("1")){
                sgs.add(sg01);
            }else{
                books.add(book02);
            }
        }
        String delete = request.getParameter("delete");
        if (delete != null) {
            int i = Integer.parseInt(delete);
           sgs.remove(i);
            
        }
    %>
    购物车中的信息为:
    <br>
    <%
        int i = 0;
        for (i = 0; i <sgs.size(); i++) {
    %>
    <a href="buy.jsp?delete=<%=i%> ">删除</a>
    <%
        out.println(sgs.get(i) + "<br>");
        }
    %>
    <a href="main.jsp">返回主页面</a>
</body>
</html>

 

 

 

posted @ 2022-04-24 17:02  计算机1901万家乐  阅读(15)  评论(0编辑  收藏  举报