T
teser3
I have this JSP where I have alot of fields with conditions.
I would like to make it more efficient and use a for loop.
Here is an example (showing 2 fields for example only):
<%@ page language="java" import="java.util.*" %>
<%
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";
if (!firstname.equals(""))
{
errors.put("firstname",firstname);
}
if (!lastname.equals(""))
{
errors.put("lastname",lastname);
}
out.println(errors.get("firstname"));
out.println(errors.get("lastname"));
%>
It prints out Joe Miller
Now my attempt below to put this in a loop prints out null null:
<%@ page language="java" import="java.util.*" %>
<%
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";
//String[] keys = {"firstname", "lastname"};
String[] keys = {firstname, lastname};
for(int i = 0;i < keys.length;i++)
{
if(!keys.equals(""))
{
errors.put(keys,keys);
}
}
out.println(errors.get("firstname"));
out.println(errors.get("lastname"));
%>
Please advise.
I would like to make it more efficient and use a for loop.
Here is an example (showing 2 fields for example only):
<%@ page language="java" import="java.util.*" %>
<%
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";
if (!firstname.equals(""))
{
errors.put("firstname",firstname);
}
if (!lastname.equals(""))
{
errors.put("lastname",lastname);
}
out.println(errors.get("firstname"));
out.println(errors.get("lastname"));
%>
It prints out Joe Miller
Now my attempt below to put this in a loop prints out null null:
<%@ page language="java" import="java.util.*" %>
<%
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";
//String[] keys = {"firstname", "lastname"};
String[] keys = {firstname, lastname};
for(int i = 0;i < keys.length;i++)
{
if(!keys.equals(""))
{
errors.put(keys,keys);
}
}
out.println(errors.get("firstname"));
out.println(errors.get("lastname"));
%>
Please advise.