1.
<%@ 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"%>
<%
    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"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'main.jsp' starting page</title>
</head>

<body>
    <%
        String uname = (String) session.getAttribute("uname");
        //如果他是空,说明没登陆,直接访问该页面了
        if (uname == null) {
            response.sendRedirect("login.jsp");
        }
    %>
    欢迎你<%=uname%><br>
    <a href="loginout.jsp">退出登录</a>
    <br>
    <form action="buy.jsp">
        <hr>
        是否加入购物车?<br> <input name="item" type="checkbox" value="weilong">
        辣条 $1<br> <input name="item" type="checkbox" value="xizhilang">
        果冻 $2<br> <input name="item" type="checkbox" value="menglong">
        雪糕 $3<br>
        <hr>
        <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>My JSP 'buy.jsp' starting page</title>
</head>

<body>

    <h2>购物车</h2>
    <%
        request.setCharacterEncoding("utf-8");
        String itemName[] = request.getParameterValues("item");
        if (itemName == null) {
            out.print("购物车为空");
        } else {
            for (int k = 0; k < itemName.length; k++) {
                out.print("(" + (k + 1) + ")" + itemName[k] + "<br>");
            }
        }
    %>

</body>
</html>

  

 

posted on 2022-04-23 22:13  王运聪  阅读(13)  评论(0编辑  收藏  举报