Java 将数据从选择标签发送到 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28107255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
send data from select tag to servlet
提问by iec2011007
I have a simple select tag
我有一个简单的选择标签
Job Category:
<select name="jobCat">
<option value="tech">Technology</option>
<option value="admin">Administration</option>
<option value="biology">Biology</option>
<option value="science">Science</option>
</select>
now when the user selects a option i want to send the data to a servlet dopost method?
现在当用户选择一个选项时,我想将数据发送到 servlet dopost 方法?
The above code resides in abc.jsp and the name of servlet file is pqr.java
以上代码在abc.jsp中,servlet文件名为pqr.java
How to perform the above action?
如何执行上述操作?
I have read something like
我读过类似的东西
<form action="login" method="post">
UserId <input type="text/html" name="userId"/><br><br>
Password <input type="password" name="password"/><br><br>
<input type="submit"/>
</form>
and this i mapped to login servlet by
这我映射到登录servlet
WebServlet("/login")
so when the user presses submit then the data is sent to this servlet. Now i want to achieve the same functionality with the select statement?
所以当用户按下提交然后数据被发送到这个 servlet。现在我想用 select 语句实现相同的功能?
This is the scheduleMeet.jsp file ` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="important.businessService.dto.Employee" %> Insert title here
这是 scheduleMeet.jsp 文件 ` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="important.businessService. dto.Employee" %> 在此处插入标题
</head>
<body>
Job Category:
<form action="scheduleMeet" method="post">
<select name="jobCat">
<option value="tech">Technology</option>
<option value="admin">Administration</option>
<option value="biology">Biology</option>
<option value="science">Science</option>
</select>
</form>
</body>
</html>`
and this is the ScheduleMeetServlet.java ` package important;
这对 ScheduleMeetServlet.java 包很重要;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class scheduleMeetServlet
*/
@WebServlet("/scheduleMeet")
public class scheduleMeetServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String jobCategory = request.getParameter("jobCat");
System.out.println("Job category is: " + jobCategory);
}
}
`
`
采纳答案by kirti
You can do it by using name of the select
您可以使用选择的名称来完成
Your select must be inside the form
您的选择必须在表单内
<form action="login" method="post">
<select name="jobCat">
<option value="tech">Technology</option>
<option value="admin">Administration</option>
<option value="biology">Biology</option>
<option value="science">Science</option>
</select>
UserId <input type="text/html" name="userId"/><br><br> Password <input type="password" name="password"/><br><br> <input type="submit"/> </form>
In your Login servlet, in your servlet post method just use the request.getparameter to get that value
在您的登录 servlet 中,在您的 servlet post 方法中只需使用 request.getparameter 来获取该值
eg
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
String selectedvalue = request.getparameter("jobCat");
// you will get that value in the string selectedvalue
}