简体   繁体   中英

How to create and view session keys using user input data with JSP

I am college student needing help with an assignment. I need to create a program that allows users to enter data values for 2 session keys: artist and color. The page needs to create 2 session attributes that are based on the 2 name:value pairs. After I create these, I need to create another page that uses the session.getAttribute method to view the values. I believe I am running into issues assigning the values from the input data to the session data using the session.setAttribute method.

This is currently what I have

<!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>

Following is the other file, 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>

Any help would be greatly appreciated!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM