【问题标题】:How to create and view session keys using user input data with JSP如何通过 JSP 使用用户输入数据创建和查看会话密钥
【发布时间】:2026-01-07 14:30:01
【问题描述】:

我是一名大学生,在作业方面需要帮助。我需要创建一个程序,允许用户输入 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>

任何帮助将不胜感激!

【问题讨论】:

    标签: java html jsp session-cookies


    【解决方案1】:

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

    【讨论】:

    • 当然,我编辑了我的原始问题以添加我使用 session.getAttribute 方法请求属性的文件。
    最近更新 更多