如何从java servlet获取参数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23212065/
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
How to get param value from java servlet
提问by WildBill
I'm trying to get param values passed to a Java Servlet but the string returned is not correct. I'm storing the values in a Map and checking if the key exists.
我正在尝试将参数值传递给 Java Servlet,但返回的字符串不正确。我将值存储在 Map 中并检查键是否存在。
Map params;
params = request.getParameterMap();
String id = params.get("id").toString();
String data = params.get("data").toString();
System.out.println("streaming" + data + " with id of " + id);
Yet if I call this servlet via http://localhost:8080/Serv/stream/data?data=hereisdata&id=you
my output looks like this:
然而,如果我通过http://localhost:8080/Serv/stream/data?data=hereisdata&id=you
我的输出调用这个 servlet看起来像这样:
streaming[Ljava.lang.String;@5e2091d3 with id of [Ljava.lang.String;@36314ab8
What am I missing?
我错过了什么?
EDIT: as the suggested answers are not working, I'm including the entire class as I'm likely messing something up within the class:
编辑:由于建议的答案不起作用,我将整个班级都包括在内,因为我可能会在班级中搞砸一些事情:
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import Engine.Streamer;
public class AnalyzerController {
private Map params;
private String pathInfo;
private HttpServletRequest request;
public AnalyzerController(HttpServletRequest request)
{
this.params = request.getParameterMap();
this.pathInfo = request.getPathInfo();
}
public void processRequest()
{
System.out.println("procing with " + pathInfo);
switch(pathInfo){
case "/stream/data":
if(params.containsKey("id") && params.containsKey("data")) processStream();
break;
}
}
private void processStream()
{
System.out.println("we are told to stream");
String data = request.getParameter("data");
String id = request.getParameter("id");
Streamer stream = new Streamer();
stream.streamInput(data, "Analyzer", id);
}
}
This line specifically is throwing the NPE: String data = request.getParameter("data");
这一行特别是抛出 NPE: String data = request.getParameter("data");
采纳答案by SudoRahul
If you look at the docs of the Request#getParameterMap()
, it returns a Map
of the type Map<String, String[]>
. Therefore, you need to take out the first element from the value String[]
array returned from the map.
如果您查看 的文档Request#getParameterMap()
,它会返回 aMap
类型Map<String, String[]>
。因此,您需要String[]
从地图返回的值数组中取出第一个元素。
String id = params.get("id")[0];
Ofcourse, you can avoid all this and directly get the parameters from the request objects using the Request#getParameter()
method.
当然,您可以避免这一切,直接使用该Request#getParameter()
方法从请求对象中获取参数。
String id = request.getParameter("id");
Edit:Looking at your class code, it seems that the instance variable request
is not initialized. Initialize that in the constructor like this:
编辑:查看您的类代码,似乎实例变量request
未初始化。像这样在构造函数中初始化它:
public AnalyzerController(HttpServletRequest request)
{
this.request = request; // Initialize your instance variable request which is used in the other methods.
this.params = request.getParameterMap();
this.pathInfo = request.getPathInfo();
}
回答by Koitoer
Try something like this.
尝试这样的事情。
String data = ((String)params.get("data"));
Or directly from the Request.
或者直接从请求。
String data = request.getParameter("data");
回答by Hitham S. AlQadheeb
You can get the required parameters instead of the whole map
您可以获得所需的参数而不是整个地图
String id = request.getParameter("id");
String data = request.getParameter("data");
回答by user3145373 ツ
You can use request object plus it's method for to get data usinggetParameter()
of you can use getParameterValues()
if multiple data are from page.
您可以使用请求对象加上它的方法来获取数据getParameter()
,getParameterValues()
如果多个数据来自页面,您可以使用它。
String id = request.getParameter("id")
String data = request.getParameter("data")
why are you using Map
?
Any special need of it or any reason ?
你为什么使用Map
?有什么特殊需要或任何原因吗?
or you can use like this :
或者你可以这样使用:
String id = params.get("id")[0];