我需要获得下拉列表,单选按钮和复选框的值。我使用request.getParameter()
方法,但它返回null
值。
JSP代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="trial.jsp" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td class="entry">
Highest Qualification :
</td>
<td class="entry">
<select name="mon">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
</select>
<input type="submit" value="Submit" name="submit" />
</td>
</tr>
<tr>
<td class="entry">
<input type="radio" name="gender" value="Male" />Male
<input type="radio" name="gender" value="Female" />Female
</td>
</tr>
</table>
</form>
</body>
</html>
Servlet代码:
public class NewServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bdate = null;
bdate = request.getParameter("mon");
System.out.print("This is" + bdate);
}
}
答案 0 :(得分:2)
问题是您的<form>
有enctype="multipart/form-data"
。您只应在尝试上传文件时在<form>
中添加该属性。由于在您当前的页面中您不需要上传文件,只需将其删除即可完美运行:
<form action="trial.jsp" method="POST" >
<table>
<tr>
<td class="entry">
<!-- rest of your HTML code -->
</form>
如果您希望/需要将文件上传功能添加到当前页面,请参阅How to upload files to server using JSP/Servlet?以解决这些问题(我不会在这个答案中重新发明轮子)。
答案 1 :(得分:1)
对于下拉列表:
<form action="sample_servlet" method="post">
<select name="item">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="Submit">
</form>
在您的sample_servlet中,写下此request.getParameter("item");
或
if(request.getParameter("item")!=null)
{
selectedItem=Integer.ParseInt(request.getParameter("item"));
}
对于单选按钮:
在jsp中你的代码可能是
<input type="radio" name="dish" value="Indian"> Indian</input>
String radio = request.getParameter("dish");