JSP第六次作业

------------恢复内容开始------------

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>index.jsp</title>
  </head>
  
  <body>
    <form action="index2.jsp" method="post" name="index">
             请输入账号:<input name="name" type="text"/><br/>
             请输入密码:<input name="password" type="password"/><br/>
           <input type="button" value="登录" onclick="denglu()"/>
      <script type="text/javascript">
         function denglu(){
            if(index.name.value==""){
               alert("账号不能为空!");
               return;
            }
             if(index.password.value==""){
               alert("密码不能为空!");
               return;
            }
            index.submit();
         } 
     </script>
   </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>index2.jsp</title>
  </head>
  
  <body>
   <% 
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        if(name.equals(password)){
           session.setAttribute("yhm", name);
           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.jsp</title>
  </head>
  
  <body>
   <%
    String name=(String) session.getAttribute("yhm");
    if(name==null){
        out.print("未登录,3秒后跳转登录页!");
        response.setHeader("refresh", "3;url=index.jsp");
    } else{
        out.print("用户名:"+name+"登陆成功!");   %>
     <form action="tuichu.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>tuichu.jsp</title>
  </head>
  
  <body>
    <h3>退出成功,3秒后返回登录页!</h3>
    <%
    session.invalidate();
    response.setHeader("refresh", "3;url=index.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>no.jsp</title>
  </head>
  
  <body>
    <h3>登陆失败,3秒后返回登陆页</h3>
    <%
    session.invalidate();
    response.setHeader("refresh", "3;url=index.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>no.jsp</title>
  </head>
  
  <body>
    <h3>登陆失败,3秒后返回登陆页</h3>
    <%
    session.invalidate();
    response.setHeader("refresh", "3;url=index.jsp");
   %>
  </body>
</html>

 

 

 

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

 

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

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

    <%
        String uname = (String) session.getAttribute("uname");
        //如果他是空,说明没登陆,直接访问该页面了
        if (uname == null)
            response.sendRedirect("login.jsp");
            
    %>
    
    <form action="show.jsp" method="post">
        商品:<br> <br>
    苹果 5.45<input type="checkbox" name="item" value="苹果">加入购物车 <br> 
    梨 4.34 <input type="checkbox" name="item" value="梨"> 加入购物车 <br> 
    香蕉 4.56<input type="checkbox" name="item" value="香蕉"> 加入购物车<br><br>
    <input type="submit" value="加入购物车">
    </form>
    
    
    
    <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>show</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 12:04  Song、  阅读(2)  评论(0编辑  收藏  举报