jsp第八周作业

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

<%@ 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></title>

  </head>
  
  <body>
<form action="MyJsp.jsp" method="post" name="login">
        用户名:<input type="text" name="uname" size="20"><br>
        密 码:<input type="password" name="upwd" size="20"><br>
        <input type="button" value="登录" onClick="validate()">
    </form>
    <script type="text/javascript">
        function validate() {
            if (login.uname.value == "") {
                alert("用户名不能为空!");
                return;
            }
            if (login.upwd.value == "") {
                alert("密码不能为空!");
                return;
            }
            login.submit();
        }
    </script>
     
  </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></title>
  

  </head>
  
  <body>
    <%
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //获取请求数据
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        //判断密码和用户名是否相同
        if (uname.equals(upwd)) {
            session.setAttribute("uname",uname);
            request.getRequestDispatcher("b.jsp")
                    .forward(request, response);
        } else {
            response.sendRedirect("c.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></title>
    
  </head>
  
  <body>
   <%
        String uname = (String) session.getAttribute("uname");
        //如果他是空,说明没登陆,直接访问该页面了
        if (uname == null) {
            response.sendRedirect("text.jsp");
        }
    %>
    欢迎你<%=uname%><br>
    <a href="a.jsp">退出登录</a>
    <br>
  </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></title>
    

  </head>
  
   <body>
     <%
        session.invalidate();//结束会话
        response.setHeader("refresh", "5;url=text.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></title>
    
    
  </head>
  
  <body>
    <h1>登陆失败!!</h1>
    <%response.setHeader("refresh", "5;url=text.jsp");%>
  </body>
</html>

 

 

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

 
<body>
    <script type="text/javascript">
        function val() {
            if (loginForm.account.value == "") {
                alert("账号不能为空");
                return;
            } else if (loginForm.pas.value == "") {
                alert("密码不能为空");
                return;
            }
            loginForm.submit();
        }
    </script>
    <form action="log.jsp">
        账号<input type="text" name="account">
        密码<input type="password" name="pas">
    <input type="submit" value="登录" />
    </form>
  </body>
<body>
      <% 
      request.setCharacterEncoding("utf-8");
      String uname = request.getParameter("account");
      String upwd = request.getParameter("pas");
      if(uname.equals(upwd)){
      session.getAttribute("uname");
      response.sendRedirect("main.jsp");
      }else{
         response.sendRedirect("no.jsp");
      }
    %>
  </body>

 <body>
  <input type="button" 
    onclick="window.location.href='index.jsp';" value="退出登录">
  <form action="buy.jsp" method="post">
        <table border="0" cellspacing="30">
          <tr>
              <td>笔记本</td>
            <td><input type="checkbox" name="item" value="computer"></td>
            <td>¥5000</td>
          </tr>
          <tr>
              <td>香水</td>
            <td><input type="checkbox" name="item" value="perfume"></td>
            <td>¥200</td>
          </tr>
          <tr>
              <td>鲜花</td>
            <td><input type="checkbox" name="item" value="flower"></td>
            <td>¥50</td>
          </tr>
          <tr>
              <td>苹果</td>
            <td><input type="checkbox" name="item" value="apple"></td>
            <td>¥20</td>
          </tr>
        </table>
        <input type="submit" value="添加购物车">   
      </form>
  </body>

<body>
      登陆失败<br>用户名或密码错误
  </body>

<body>
  <h2>购物车的商品有:</h2>
  <%
        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>

 

 

posted @ 2022-04-24 16:55  计算机1902佟阳  阅读(27)  评论(0编辑  收藏  举报