JSP第八周作业

1.登陆 login.jsp

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

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

<html>
<body>
<script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="logle.jsp" method="post">
        
    用户名:<input type="text" name="uname" ><br> 
    密码: <input  type="password" name="upwd" ><br>
    
        <input type="button" value="登录" onClick="validate()">    

    </form>

</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>
  <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("main.jsp").forward(request,response);
            }
        else
            request.getRequestDispatcher("no.jsp").forward(request,response);
    %>
  </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>
  <body>
    登录失败<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>
  <body>
    <%
    String uname=(String)session.getAttribute("uname");
    if(uname==null)
      response.sendRedirect("index.jsp");
    %>
     欢迎你<%=uname %>
      <a href="MyJsp.jsp">退出登录</a>
  
  </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>
  
  <body>
    <%
    session.invalidate();
    response.setHeader("refresh", "5;url=index.jsp");
     %>
退出成功

  </body>
</html>

2.购物车

和上一题一起,在main.jsp中做一个商品展示,里面显示3个商品名和价格 ,每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车show.jsp  。(选作:在购物车show.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>
  <body>
    <%
    String uname=(String)session.getAttribute("uname");
    if(uname==null)
      response.sendRedirect("index.jsp");
    %>
     欢迎你<%=uname %>
      <a href="MyJsp.jsp">退出登录</a>
      <h2 align="center">商品列表</h2>
   <form action="show.jsp" method="post">
  <p style="font-family:宋体;font-size:20;color:blue">
       商品名 A1,价格87  <input type="checkbox"  name="list" value="A1"><br>
       商品名 A2,价格17  <input type="checkbox" name="list" value="A2"><br>
        商品名 A3,价格23  <input type="checkbox" name="list" value="A3"><br>
   <input type="submit" value="加入购物车"><br>     
       <a href="show.jsp">购物车</a><br>
       </form>
  </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>
    <title>My JSP 'show.jsp' starting page</title>
  </head>
  <body>
  <%
        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 @ 2022-04-24 21:11  F85  阅读(1)  评论(0编辑  收藏  举报