首页 > 解决方案 > 如何通过 JSP 使用用户输入数据创建和查看会话密钥

问题描述

我是一名大学生,需要帮助完成作业。我需要创建一个程序,允许用户输入 2 个会话键的数据值:艺术家和颜色。该页面需要创建 2 个基于 2 个名称:值对的会话属性。创建这些后,我需要创建另一个使用 session.getAttribute 方法查看值的页面。我相信我遇到了使用 session.setAttribute 方法将输入数据中的值分配给会话数据的问题。

这是我目前拥有的

<!DOCTYPE html>
<html>
    <head>
        <title>Session Creation</title>
        <link rel="stylesheet" href="css-3.css">
    </head>
    <body>
        <div class="center">
            <h1>Enter Session Information</h1>
            <form action="viewSessionData.jsp" method="GET">
                <table class="inline-block">
                    <tr><th id="th-id1" colspan="2">Session Information</th></tr>
                    <tr>
                        <td>Artist:</td>
                        <td><input type="text" name="artistValue"></td>
                        <%  String artistValue = "";
                            session.setAttribute("artistValue", artistValue); %>
                    </tr>
                    <tr>
                        <td>Color:</td>
                        <td><input type="text" name="colorValue"></td>
                        <%  String colorValue = "";
                            session.setAttribute("colorValue", colorValue); %>
                    </tr>
                    <tr>
                        <td></td>
                        <td><br><input type="submit" class="coral_color"
                                       value="Create Session"></td>
                        
                    </tr>
                </table>
            </form>
        </div>                   
    </body>
</html>

以下是另一个文件 viewSessionData.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>JSP Session Tracking</title>
        <link rel="stylesheet" href="css-3.css">
    </head>
    <%@ page import="java.util.*" %>
    <body>
        <div class="center">
        <h1>Session Tracking</h1>
        <table class="inline-block">
            <tr id="th-id1">
                <th>Session info</th>
                <th>Value</th>
            </tr>
            <tr>
                <td>Color</td>
                <td><%= session.getAttribute("colorValue") %></td>
            </tr>
            <tr>
                <td>Artist</td>
                <td><%= session.getAttribute("artistValue") %></td>
            </tr>
        </table>
        </div>            
    </body>
</html>

任何帮助将不胜感激!

标签: javahtmljspsession-cookies

解决方案


这是你的创建属性页面,云你显示你的另一个jsp页面?


推荐阅读