reqest.getParameter() returns null for Select box in JSP

这一生的挚爱 提交于 2019-12-24 09:59:24

问题


I have the following code in a jsp file (on Adobe CQ) but, it returns null. Not sure why. I am expecting the out.println line to return 40 since it is the default selected value.

<select id="itemsperpage" name="itemsperpage">
<option value="20">20</option>
<option value="40" selected>40</option>
<option value="100">100</option>
<option value="200">200</option>
</select>
<%
String itemsPerPage = request.getParameter("itemsperpage");
out.println("Items: " + itemsPerPage );
%>

回答1:


your code will always return null. try to see page source after running your application. value of Items is always null.

try following code: (in this code I am sending a request on every time the value of combobox is changed)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body onload="form1.submit();">
<form action="#" name="form1">
<select id="itemsperpage" name="itemsperpage" onchange="submit();">
<option value="20">20</option>
<option value="40" selected>40</option>
<option value="100">100</option>
<option value="200">200</option>
</select>
</form>
<%
String itemsPerPage = request.getParameter("itemsperpage");
out.println("Items: " + itemsPerPage );
%>
</body>
</html>

[Note: i will suggest you to not use scriplets in your jsp file, instead you can go for AJAX , JSTL etc. ]



来源:https://stackoverflow.com/questions/14728297/reqest-getparameter-returns-null-for-select-box-in-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!