[英]How to get the values of dropdownlist, radio button from a jsp and send it to servlet?
I need to get value of dropdown list, radio button and a check box. 我需要获得下拉列表,单选按钮和复选框的值。 I am using request.getParameter()
method but it's returning null
value. 我正在使用request.getParameter()
方法,但它返回null
值。
JSP Code: 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 Code: 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);
}
}
The problem is that your <form>
has enctype="multipart/form-data"
. 问题是你的<form>
有enctype="multipart/form-data"
。 You should only add that attribute in <form>
when trying to upload a file. 您只应在尝试上传文件时在<form>
添加该属性。 Since in your current page you don't need to upload a file, just remove it and everything will work perfectly: 因为在您当前的页面中您不需要上传文件,只需将其删除即可完美地完成所有操作:
<form action="trial.jsp" method="POST" >
<table>
<tr>
<td class="entry">
<!-- rest of your HTML code -->
</form>
In case you want/need to add file upload functionality to your current page, then please refer to How to upload files to server using JSP/Servlet? 如果您想/需要将文件上传功能添加到当前页面,请参阅如何使用JSP / Servlet将文件上传到服务器? in order to solve these problems (I won't reinvent the wheel in this answer). 为了解决这些问题(我不会在这个答案中重新发明轮子)。
For Dropdown List: 对于下拉列表:
<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>
In your sample_servlet, write like this request.getParameter("item");
在你的sample_servlet中,写一下这个request.getParameter("item");
or 要么
if(request.getParameter("item")!=null)
{
selectedItem=Integer.ParseInt(request.getParameter("item"));
}
For Radio Button: 对于单选按钮:
In jsp your code maybe 在jsp中您的代码可能
<input type="radio" name="dish" value="Indian"> Indian</input>
in your servlet page 在您的servlet页面中
String radio = request.getParameter("dish");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.