英文:
null value in Map<String,String>
问题
我有一个关于根据以下代码设置地图的问题。以这种方式,参数的值从用户那里接收,但在根据相关键输入值后,会打印出null。
请指导我。
public class ImportBatchCardRespServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private FileOutputStream out;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// Path path = Paths.get("F:\\");
System.out.println("ImportBatchCardRespServlet-AccessFilesPath: " + getInitParameter("AccessFilesPath")); // vahid-log
Path path = Paths.get(getInitParameter("AccessFilesPath"));
// List<FileItem> items = new ServletFileUpload(new
// DiskFileItemFactory()).parseRequest(request);
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory())
.parseRequest(new ServletRequestContext(req));
Map<String, String> formFields = new HashMap<>();
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select,
// etc).
String fieldName = item.getFieldName();
String fieldValue = item.getString();
System.out.println("req-fieldName: " + fieldName + ", req-fieldValue: " + fieldValue); // vahid-log
formFields.put(fieldName, fieldValue);
System.out.println("map-fieldName: " + formFields.get(fieldName) + ", map-fieldValue: " + formFields.get(fieldValue)); // vahid-log
如果还有其他需要翻译的部分,请提供具体内容。
英文:
I have a problem with setting the map according to the following code. In this way, the value of the parameters is received from the user, but after entering the value in the map according to the relevant key, null is printed.
please guide me
public class ImportBatchCardRespServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private FileOutputStream out;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// Path path = Paths.get("F:\\");
System.out.println("ImportBatchCardRespServlet-AccessFilesPath: " + getInitParameter("AccessFilesPath")); // vahid-log
Path path = Paths.get(getInitParameter("AccessFilesPath"));
// List<FileItem> items = new ServletFileUpload(new
// DiskFileItemFactory()).parseRequest(request);
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory())
.parseRequest(new ServletRequestContext(req));
Map<String,String> formFields = new HashMap<>();
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select,
// etc).
String fieldName = item.getFieldName();
String fieldValue = item.getString();
System.out.println("req-fieldName: "+fieldName+",req-fieldValue: "+fieldValue); //vahid-log
formFields.put(fieldName,fieldValue);
System.out.println("map-fieldName: "+formFields.get(fieldName)+",map-fieldValue: "+formFields.get(fieldValue)); //vahid-log
答案1
得分: 1
formFields.put(fieldName, fieldValue);
将一个键/值对添加到您的HashMap中,其中"fieldName"是键,"fieldValue"是值。
在下一行...
System.out.println("map-fieldName: "+formFields.get(fieldName)+",map-fieldValue: "+formFields.get(fieldValue)); //vahid-log
...看起来你想要测试打印键/值对。
然而,HashMap的"get"方法的参数是键/值对的键,因此
formFields.get(fieldName)
将返回键/值对的值(即fieldValue),而
formFields.get(fieldValue)
可能会返回null,因为没有与值相同的键。
正确的输出应该使用以下检查行可用:
System.out.println("map-fieldName: "+fieldName+",map-fieldValue: "+formFields.get(fieldName)); //vahid-log
干杯!
英文:
formFields.put(fieldName,fieldValue);
adds a value/key pair to your Hashmap, with "fieldName" being the key and "fieldValue" the value.
In the following line...
System.out.println("map-fieldName: "+formFields.get(fieldName)+",map-fieldValue: "+formFields.get(fieldValue)); //vahid-log
... it seems you want to test-print the key/value pair.
However, HashMap's "get" method parameter is the key of the key/value pair, therefore
formFields.get(fieldName)
will return the value of the key/value pair (i.e. fieldValue) and
formFields.get(fieldValue)
will likely return null as there is no key with the same name like the value.
The correct output should be available using this check line instead:
System.out.println("map-fieldName: "+fieldName+",map-fieldValue: "+formFields.get(fieldName)); //vahid-log
Cheers!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论