【问题标题】:how get session values from div of another page如何从另一个页面的 div 获取会话值
【发布时间】:2012-07-08 13:37:12
【问题描述】:

我有 3 个 jsp 页面,即 a.jsp、b.jsp 和 c.jsp,在 a.jsp 中我有一个 div,其中 b 和 c 将加载,现在我的要求是我必须发送一个值形式 b .jsp 并在 c.jsp 中检索,知道吗? 检查我的代码

a.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--DVID=00014EAD-->
<html>
<head>

<title>Tabs in HTML with CSS</title>
<script src="resources/js2/jquery-1.6.2.min.js"></script>
<style type="text/css">
<!--
#tabs {
    border-bottom: .5em solid #0033CC;
    margin: 0;
    padding: 0;
}

#tabs li {
    display: inline;
    border-top: .1em solid #03c;
    border-left: .1em solid #03c;
    border-right: .1em solid #03c;
}

#tabs li a {
    text-decoration: none;
    padding: 0.25em 1em;
    color: #000;
}

#page1 #tabs li#tab1 a,#page2 #tabs li#tab2 a,#page3 #tabs li#tab3 a,.page4 li#tab4 a
    {
    padding: 0.25em 1em;
    background-color: #03c;
    color: #fff;
}
-->
</style>

<script type="text/javascript">

    function profile() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "b.jsp", true);
        xmlhttp.send();
    }

    function fields() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "c.jsp", true);
        xmlhttp.send();
    }
</script>
</head>

<body id="page3">
    <ul id="tabs">
        <li id="tab1"><a href="#" onclick="profile()">Tab 1</a></li>
        <li id="tab2"><a href="#">Tab 2</a></li>

    </ul>

    <div id="myDiv"></div>
</body>
</html>

b.jsp

<html>
        <head><title>getQueryString() method of request object.</title></head>
                <body>
                <form method="get">
                        <table border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                        <td>User Name: </td>
                                        <td><input type="text" size="20" name="name" />
                                </tr>

                                <tr>
                                        <td> </td>
                                        <td><input type="button" id="NextBtn" value="Next" >
                                        </td>
                                </tr>
                        </table>
                       <%String name = request.getParameter("name");
                       session.setAttribute("name", name);
                     %>
                </form>

        </body>
</html>

c.jsp

<html>
<body>

<%

out.println("Name : " + (String)session.getAttribute("name") + "<br/>");

%>
</body>
</html>

【问题讨论】:

  • 你为什么不使用jQuery's Ajax methods?另外,我建议您从问题中的代码中删除 &lt;style&gt; 块,因为它与您的要求无关(但它占用了大量空间)。

标签: jquery ajax jsp session


【解决方案1】:

我注意到两个错误:
在 b.jsp 中,您必须更正此问题:

<form method="get" action="c.jsp">


在您的 c.jsp 中,您必须更正此问题:

<%= (String)session.getAttribute("name") %> <br>


【讨论】:

  • 使用这个表单操作,我们将页面重定向到 c.jsp,但我只想在 a.jsp 内
  • 我想要在 c.jsp 中输入 b.jsp 的名称,但是 c.jsp 应该加载到 a.jsp 中的 div 中
  • 谢谢你的想法,但我的问题仍然没有解决
猜你喜欢
  • 2015-05-14
  • 1970-01-01
  • 2017-02-01
  • 2011-06-30
  • 2015-03-31
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多