欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp源码实例2(获取表单参数)

程序员文章站 2023-12-18 20:24:52
package coreservlets;import java.io.*;import javax.servlet.*;import javax.servlet.http...
package coreservlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/** shows all the parameters sent to the servlet via either
* get or post. specially marks parameters that have
* no values or multiple values.
* <p>
* taken from core servlets and javaserver pages
* from prentice hall and sun microsystems press,
* http://www.coreservlets.com/.
* © 2000 marty hall; may be freely used or adapted.
*/

public class showparameters extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html");
printwriter out = response.getwriter();
string title = "reading all request parameters";
out.println(servletutilities.headwithtitle(title) +
"<body bgcolor=\"#fdf5e6\">\n" +
"<h1 align=center>" + title + "</h1>\n" +
"<table border=1 align=center>\n" +
"<tr bgcolor=\"#ffad00\">\n" +
"<th>parameter name<th>parameter value(s)");
enumeration paramnames = request.getparameternames();
while(paramnames.hasmoreelements()) {
string paramname = (string)paramnames.nextelement();
out.print("<tr><td>" + paramname + "\n<td>");
string[] paramvalues =
request.getparametervalues(paramname);
if (paramvalues.length == 1) {
string paramvalue = paramvalues[0];
if (paramvalue.length() == 0)
out.println("<i>no value</i>");
else
out.println(paramvalue);
} else {
out.println("<ul>");
for(int i=0; i<paramvalues.length; i++) {
out.println("<li>" + paramvalues);
}
out.println("</ul>");
}
}
out.println("</table>\n</body></html>");
}

public void dopost(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
doget(request, response);
}
}

上一篇:

下一篇: