第八周

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>login</title>
     
  </head>
   
  <body>
   <form  name="form" action="dologin.jsp" method="post">
      账号:<input type="text" name="usernaem"><br>
      密码:<input type="password" name="psw"><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>dologin</title>
     
  </head>
   
  <body>
     <%
    request.setCharacterEncoding("utf-8");
    String usernaem=request.getParameter("usernaem");
    String psw=request.getParameter("psw");
    if(usernaem.equals(psw)){
        session.setAttribute("usernaem", usernaem);
        request.getRequestDispatcher("main.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>main</title>
     
  </head>
   
  <body>
    <h1>登陆成功!</h1>
 
    <%
        String usernaem = (String) session.getAttribute("usernaem");
        if (usernaem == null)
            response.sendRedirect("login.jsp");
             
    %>
     
    <form name="form1" action="logout.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>logout</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>no</title>
 
</head>
 
<body>
    <h1>登陆失败!!</h1>
    <%response.setHeader("refresh", "5;url=login.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>main</title>
     
  </head>
   
  <body>
    <%
        String usernaem = (String) session.getAttribute("usernaem");
        if (usernaem == null)
            response.sendRedirect("login.jsp");
    %>
 
    欢迎光临
 
   
    <hr>
    商品展示:
    <br>
    <form action="gouwuche.jsp" method="post">
        请选择要加入购物车的商品:<br> 
        牛肉饭 ¥30 <input type="checkbox" name="list" value="牛肉饭"> <br>
        黄焖鸡 ¥25 <input type="checkbox" name="list" value="黄焖鸡"> <br>
        汤面 ¥16   <input type="checkbox" name="list" value="汤面"> <br>
        <hr>
        <input type="submit" value="加入购物车">
         <a href="logout.jsp">退出登录</a>
 
    </form>
    
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 
<html>
<head>
 
 
<title>My JSP 'index.jsp' starting page</title>
 
</head>
 
<body>
    <h2>购物车中的商品有:</h2>
    <%
        request.setCharacterEncoding("utf-8");
        String listName[] = request.getParameterValues("list");
        if (listName == null) {
            out.print("你没有选购物品");
        } else {
            for (int i = 0; i < listName.length; i++) {
                out.print("(" + (i + 1) + ")" + listName[i] + "<br>");
            }
        }
    %>
</body>
</html>

 

 

 

 

 

posted on 2022-04-24 02:50  我裂开  阅读(2)  评论(0编辑  收藏  举报

导航