第八次作业

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  
  
  
  
   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title></title>
    
  </head>
  
  <body>
   <form  name="form" action="Two.jsp" method="post">
      用户名:<input type="text" name="uname"><br>
      密码:<input type="password" name="upwd"><br>
    <input type="submit" value="登录" >    
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>Two</title>
    
  </head>
  
  <body>
     <%
    request.setCharacterEncoding("utf-8");
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    if(uname.equals(upwd)){
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("t.jsp").forward(request,response);
    }else{
    request.getRequestDispatcher("no.jsp").forward(request,response);
    }
     %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>t</title>
    
  </head>
  
  <body>
    <h2>登陆成功!</h2>

    <%
        String uname = (String) session.getAttribute("uname");
        if (uname == null)
            response.sendRedirect("Two.jsp");
            
    %>
    
    <form name="form1" action="n.jsp" method="post">
    <input type="submit" value="退出登录">
    </form>
    
    
   
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>


<title>no</title>

</head>

<body>
    <h1>登陆失败!!</h1>
    <%response.setHeader("refresh", "5;url=third.jsp");%>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>n</title>
    
  </head>
  
  <body>
    <%
    session.invalidate();
    response.setHeader("refresh", "5;url=third.jsp");
     %>
  </body>
</html>

.

 

 

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>third</title>
    
  </head>
  
  <body>
    <h2>登陆成功!</h2>

    <%
        String uname = (String) session.getAttribute("uname");
        //如果他是空,说明没登陆,直接访问该页面了
        if (uname == null)
            response.sendRedirect("Two.jsp");
            
    %>
    
    <form action="t.jsp" method="post">
        商品:<br> <br>
    草莓 10.30<input type="checkbox" name="item" value="草莓">加入购物车 <br> 
    梨 5.41 <input type="checkbox" name="item" value="梨"> 加入购物车 <br> 
    香蕉 8.50<input type="checkbox" name="item" value="香蕉"> 加入购物车<br><br>
    <input type="submit" value="加入购物车">
    </form>
    
    
    
    <form name="form1" action="t.jsp" method="post">
    <input type="submit" value="退出登录">
    </form>
    
    
   
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>Two</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-24 16:44  叶鑫宇  阅读(8)  评论(0编辑  收藏  举报