JSP 기본 연습 - 확장

  1. 두 페이지를 작성하십시오. 하나는 일부 역사 책의 이름과 가격을 표시하고 다른 하나는 일부 컴퓨터 서적의 이름과 가격을 표시합니다. 각 책의 뒷면에는 링크가 있습니다. 구입하고 링크를 클릭하면 책을 장바구니에 담을 수 있습니다. 각 페이지에는 "장바구니 보기" 링크가 있습니다. 링크를 클릭하면 ; 각 콘텐츠 뒤에는 "제거" 링크가 있습니다. 링크를 클릭하여 장바구니에서 책을 제거하십시오.

//historyBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>历史图书</title>
</head>
<body>
        中国近代史纲要  单价:25.5元
        <a href="session.jsp?bookStr=中国近代史纲要  单价:25.5元&i=1">购买</a><br>
        <a href="ShoppingCar.jsp">显示购物车</a>
</body>
</html>

//computerBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>计算机图书</title>
</head>
<body>
    数据结构  单价36.6元<a href="session.jsp?bookStr=数据结构  单价36.6元&i=2">购买</a><br>
    <a href="ShoppingCar.jsp">显示购物车</a>
</body>
</html>

//세션.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%

        ArrayList books = (ArrayList) session.getAttribute("books");
        if(books == null){
            books = new ArrayList();
            session.setAttribute("books", books);
        }
        if(session.getAttribute("book") != null){
            books.remove(session.getAttribute("book"));
        }
        String bookStr = request.getParameter("bookStr");
        if(bookStr != null){
            books.add(bookStr);
        }
        String type = request.getParameter("i");
        if(type.equals("1")){
            response.sendRedirect("historyBook.jsp");
        }else{
            response.sendRedirect("computerBook.jsp");
        }
    %>
</body>
</html>

//ShoppingCar.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车</title>
</head>
<body>
    <%
        ArrayList books = (ArrayList) session.getAttribute("books");
        for(int i=0; i<books.size(); i++){
            String book = (String)books.get(i);
            out.print(book+"<a href=\"carDel.jsp?book="+book+"\">删除</a><br>");
        }
    %>

</body>
</html>

//carDel.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        ArrayList books = (ArrayList) session.getAttribute("books");
        String book = request.getParameter("book");
        books.remove(book);
        response.sendRedirect("01ShoppingCar.jsp");
    %>
</body>
</html>
  1. 로그인 페이지를 작성하고 사용자가 로그인하고 계정 번호와 암호를 입력하고 계정 번호와 암호가 같으면 로그인에 성공한 것으로 간주되며 이 페이지에 온라인 목록(모든 계정이 성공적으로 로그인됨)이 표시됩니다.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
    <form action="01_2session.jsp" method="get">
        账号:<input type="text" name="username"/>
        密码:<input type="password" name="password"/>
        <input type="submit" value="提交"/>
    </form>

</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>当前在线</title>
</head>
<body>
    <%
        ArrayList users = (ArrayList) session.getAttribute("users");
        if(users == null){
            users = new ArrayList();
            session.setAttribute("users", users);
        }
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username != null){
            if(username.equals(password)){
                session.setAttribute("username", username);
                users.add(username);
                out.println("登录成功!<br>");
            }else{
                out.println("登录失败!<br>");
            }
        }
        out.println("当前在线名单:<br>");
        for(int i=0; i<users.size(); i++){
    %>
            <%= users.get(i) + "<br>"%>
    <%
        }
    %>

</body>
</html>

Supongo que te gusta

Origin blog.csdn.net/weixin_47940048/article/details/129758858
Recomendado
Clasificación