第八周

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 
<html>
<head>
 
 
<title>My JSP 'index.jsp' starting page</title>
 
</head>
 
<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="dologin.jsp" method="post">
         
    用户名:<input type="text" name="uname" ><br>
    密码: <input  type="password" name="upwd" >
     
        <input type="button" value="登录" onClick="validate()">   
    </form>
 
</body>
</html>
复制代码
复制代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>   
    <title>My JSP 'index.jsp' starting page</title>
  </head> 
  <body>
    
  <%
  String uname=(String)session.getAttribute("uname");
  //如果他是空,说明没登陆,直接访问该页面了
  if(uname==null)
      response.sendRedirect("login.jsp"); 
   %>
   
  欢迎你<%=uname %>
   
  <a href="logout.jsp">退出登录</a>
 
   
   
  </body>
</html>
复制代码
复制代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    if (uname.equals(upwd)){
        //转发到ok页面,但是url没变
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request,response);
    }
    else{
        //重定向回login.jsp,浏览器url也变
        response.sendRedirect("no.jsp");
    }
复制代码
复制代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 
<html>
<head>
 
 
<title>My JSP 'index.jsp' starting page</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>My JSP 'index.jsp' starting page</title>
 
</head>
 
<body>
    <h1>登陆失败!!</h1>
    <%response.setHeader("refresh", "5;url=login.jsp");%>
 
</body>
</html>

 

2

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>

    <%
        String uname = (String) session.getAttribute("uname");
        if (uname == null)
            response.sendRedirect("login.jsp");
    %>

    欢迎你<%=uname%>

    <a href="logout.jsp">退出登录</a>
    <hr>
    商品展示:
    <br>
    <hr>
    <form action="show.jsp" method="post">
        请选择要加入购物车的商品:

<br> <input type="checkbox" name="list" value="苹果"> 苹果 ¥9.9<br> <input type="checkbox" name="list" value="香蕉"> 香蕉 ¥8.8<br> <input type="checkbox" name="list" value="橘子"> 橘子 ¥7.7<br> <hr> <input type="submit" value="加入购物车"> </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 @ 2022-04-24 16:23  刘德璞  阅读(6)  评论(0编辑  收藏  举报