JavaMail Problem
I am using the following code in JSP to send form contents where there are a variable number of form fields:
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
text+="
";
text+=paramValues;
This returns something like:
[Ljava.lang.String;@1f5697a
[Ljava.lang.String;@e00516
[Ljava.lang.String;@16e09a7
[Ljava.lang.String;@13bb3ac
[Ljava.lang.String;@91120c
[Ljava.lang.String;@37f70c
I understand these are memory locations but don't know how to get the actual values (getParameterValues) that were originally typed into the form.
Any ideas? I am fairly new to java!
[nobr]Don't know why you are posting this question to the JavaMail group, as it has nothing to do with JavaMail... but I'm feeling generous :)
The paramValues variable is an array. What you are seeing is address of the reference to the array.
You need to iterate the array to get the values.
For example:
String[] paramValues = request.getParameterValues(paramName);
for(int i = 0; i < paramValues.length; i++) {
text+="<br>";
text+=paramValues[i];
}
[/nobr]
Hi,
Thanks a lot - it works now. How do I award you your 'dukedollars'?
Just for your interest it is sort of related to JavaMail as the following code shows. I thought it may be an email issue of some sort at first.
The code is for a site where there are an unknown number of form fields being emailed to the user, including "basket items" as he wants to process the transactions manually!
CODE:
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
Properties props = new Properties();
props.put("mail.countrychoicecookers.com", "mail.wanadoo.co.uk");
Session s = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress("info@countrychoicecookers.com");
message.setFrom(from);
String toAddress = request.getParameter("to");
InternetAddress to = new InternetAddress(toAddress);
message.addRecipient(Message.RecipientType.TO, to);
String subject = request.getParameter("subject");
message.setSubject(subject);
String text = request.getParameter("text");
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.println("Parameter Name:" + paramName + "<br>");
String[] paramValues = request.getParameterValues(paramName);
out.println("Value:"+paramValues+"<br>");
for(int i=0; i < paramValues.length; i++){
text+="<br>";
text+=paramValues;
}
}
message.setText(text);
Transport.send(message);
%>