如何在jsp中将选定选项设置为session

时间:2014-08-19 11:32:56

标签: jsp session option

我试图以不同的方式尝试,但无法获得解决方案。请帮忙。我试图将选定的选项添加到我的下一个jsp页面。我试图在会话中获取它但总是返回null。任何其他替代方法也可以。

one.jsp

<%
String year = request.getParameter("finyear"); 
session.setAttribute("FYear", year);
%>

<body>
<form method="post" name="yearForm">
<select id="finyear" name="finyear">
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
</select>
</form>

<a href="two.jsp" >link</a>

</body>

Two.jsp

session.getAttribute("FYear");

它总是返回null。

2 个答案:

答案 0 :(得分:1)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">

function formUrl() {
    var element = document.getElementById("finyear").value;
    window.open("two.jsp?finyear="+element);
}
</script>
</head>
<body>
    <form method="post" name="yearForm">
        <select id="finyear" name="finyear">
            <option value="2014">2014</option>
            <option value="2013">2013</option>
            <option value="2012">2012</option>
            <option value="2011">2011</option>
        </select>
    </form>

    <a href="javascript:formUrl()">link</a>

</body>
</html>

答案 1 :(得分:1)

one.jsp

<body>
<form method="post" action="two.jsp>
<select name="finyear">
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
</select>
<input type="submit" value="Submit" />
</form>

</body>

two.jsp

request.getParameter("finyear");