第八周

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

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

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

<html>
<head>
<title></title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        if (name.equals(password)) {
            session.setAttribute("name", name);
            request.getRequestDispatcher("index2.jsp").forward(request, response);
        } else {
            response.sendRedirect("index3.jsp");
        }
    %>

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

<html>
  <head>    
    <title></title>
  </head>  
  <body>
  <%
  String name=(String)session.getAttribute("name");
  //如果他是空,说明没登陆,直接访问该页面了
  if(name==null)
      response.sendRedirect("index.jsp");
   %>
  <a href="index.jsp">退出登录</a> <br>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title></title>
</head>
<body>
    登陆失败,请重新登录
    <%
      response.setHeader("refresh", "5;url=index.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=index.jsp");
  %>
退出成功!
  </body>
</html>

 

 

 

 

 

 

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

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

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

<html>
<head>
<title></title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        if (name.equals(password)) {
            session.setAttribute("name", name);
            request.getRequestDispatcher("index2.jsp").forward(request, response);
        } else {
            response.sendRedirect("index3.jsp");
        }
    %>

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

<html>
  <head>    
    <title></title>
  </head>  
  <body>
  <%
  String name=(String)session.getAttribute("name");
  if(name==null)
      response.sendRedirect("index.jsp");
   %>
  <a href="index.jsp">退出登录</a> <br>
  <form action="index5.jsp">
  <input name="book" type="checkbox" value="java web">
  java web   ¥100<br>  
  <input name="book" type="checkbox" value="java">
  java   ¥200<br>  
  <input name="book" type="checkbox" value="android">
  android   ¥300<br>
  <hr>
  <input type="submit" value="加入购物车">
  </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title></title>
</head>
<body>
    登陆失败,请重新登录
    <%
      response.setHeader("refresh", "5;url=index.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=index.jsp");
  %>
退出成功!
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>   
    <title></title>
  </head>  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    String book[]=request.getParameterValues("book");
    if(book==null){
    out.print("购物车为空");
    }
    else{
    for(int i=0;i<book.length;i++){
    out.print("("+(i+1)+")"+book[i]+"<br>");
    }
    }
     %>
  </body>
</html>

 

 

 

posted @ 2022-04-24 16:39  赵佳慧  阅读(10)  评论(0编辑  收藏  举报