reqest.getParameter() returns null for Select box

2019-08-13 13: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 );
%>

标签: html jsp jstl
1条回答
Anthone
2楼-- · 2019-08-13 14:04

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. ]

查看更多
登录 后发表回答