// File: EchoArgs.java
// Echo arguments sent via HTTP GET/POST
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EchoArgs extends HttpServlet
{
public void init (ServletConfig config) throws ServletException
{ super.init( config );
}
public void doGet( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{ res.setContentType( "text/html" );
PrintWriter out = res.getWriter();
out.println( "<html>" );
out.println( "<head>" );
out.println( "<title>Echo arguments</title>" );
out.println( "</head>" );
out.println( "<body>" );
out.println( "<h1>Arguments sent using GET:</h1>" );
dumpArgs( req, out );
out.println( "</body>" );
out.println( "</html>" );
out.close();
}
private void dumpArgs( HttpServletRequest req, PrintWriter out )
throws ServletException, IOException
{ Enumeration names = req.getParameterNames();
while( names.hasMoreElements() )
{ String key;
key = (String)names.nextElement();
// value = req.getParameter( key );
// out.println("Key: " + key + " Value: " + value + "<BR>" );
String[] values;
values = req.getParameterValues( key );
out.print( "<P>Key: " + key + "<BR>" );
out.print( "Values: ");
for( int i = 0; i < values.length; i++ )
{ out.print( values[i] + ", " );
}
out.println( "</P>" );
}
}
}
|