DumpFormServlet.java /* * Copyright (c) 1998-2005 Servertec. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * THIS NOTICE MUST NOT BE ALTERED NOR REMOVED. * * CopyrightVersion 1.0 */ import java.util.StringTokenizer; import java.util.Enumeration; import java.io.IOException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Cookie; import javax.servlet.ServletOutputStream; import javax.servlet.ServletException; import javax.servlet.ServletConfig; import stec.lang.DString; import stec.iws.Request; import stec.iws.ServletContextImpl; import stec.iws.Utils; public final class DumpFormServlet extends HttpServlet { public final void service(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException { _response.setContentType("text/html"); ServletOutputStream out = _response.getOutputStream(); out.print("<html><head><title>"); out.print("Dump Form Servlet"); out.print("</title></head><body>"); dump_args(out, _request); dumpInitParams(out, _request); dump_server_variables(out, _request); dump_headers(out, _request); dumpFormData(out, _request); dumpCookies(out, _request); out.print("</body></html>"); out.close(); } private final static void dump_args(ServletOutputStream out, HttpServletRequest _request) throws IOException { out.print("<h1>Arguments:</h1>"); out.print("<lit>"); String qs = _request.getQueryString(); if(qs != null) { String args = Utils.getArgs(_request); if(args != null) { int counter = 0; StringTokenizer st = new StringTokenizer(args); while(st.hasMoreElements()) { out.print("args["); out.print(counter++); out.print("] = ["); out.print((String)st.nextElement()); out.print("]<br>"); } } } out.print("</lit>"); } private final static void dump_NPAIR(ServletOutputStream out, String key, String value) throws IOException { out.print(key); out.print(" = ["); out.print(value); out.print("]<br>"); } private final static void dump_server_variables(ServletOutputStream out, HttpServletRequest _request) throws IOException { out.print("<h1>Server Variables:</h1>"); out.print("<lit>"); dump_NPAIR(out, "AUTH_TYPE", _request.getAuthType()); dump_NPAIR(out, "REQUEST_METHOD", _request.getMethod()); dump_NPAIR(out, "PATH_INFO", _request.getPathInfo()); dump_NPAIR(out, "PATH_TRANSLATED", _request.getPathTranslated()); dump_NPAIR(out, "QUERY_STRING", _request.getQueryString()); dump_NPAIR(out, "REQUEST_URI", _request.getRequestURI()); dump_NPAIR(out, "SCRIPT_NAME", _request.getServletPath()); //dump_NPAIR(out, "LOCAL_ADDR", _request.getLocalAddr()); dump_NPAIR(out, "SERVER_PROTOCOL", _request.getProtocol()); dump_NPAIR(out, "REMOTE_ADDR", _request.getRemoteAddr()); dump_NPAIR(out, "REMOTE_HOST", _request.getRemoteHost()); dump_NPAIR(out, "HTTPS", _request.getScheme()); dump_NPAIR(out, "SERVER_NAME", _request.getServerName()); dump_NPAIR(out, "SERVER_PORT", String.valueOf(_request.getServerPort())); out.print("</lit>"); } private final static void dump_headers(ServletOutputStream out, HttpServletRequest _request) throws IOException { out.print("<h1>Headers:</h1>"); out.print("<lit>"); Enumeration values; String key; Enumeration headers = _request.getHeaderNames(); while(headers.hasMoreElements()) { key = (String)headers.nextElement(); values = _request.getHeaders(key); while(values.hasMoreElements()) { dump_NPAIR(out, key, (String)values.nextElement()); } } out.print("</lit>"); } protected final void dumpInitParams(ServletOutputStream out, HttpServletRequest _request) throws IOException { out.print("<h1>Init Parameters:</h1>"); String key; String values[]; ServletContextImpl config = (ServletContextImpl)getServletConfig(); Enumeration e = config.getInitParameterNames(); while(e.hasMoreElements()) { key = (String)e.nextElement(); values = config.getInitParameterValues(key); int length = values.length; for(int i = 0; i < length; i++) { out.print(key); out.print("("); out.print(i); out.print(") = "); out.print(values[i]); out.print("<br>"); } } } protected final static void dumpFormData(ServletOutputStream out, HttpServletRequest _request) throws IOException { out.print("<h1>Form Data:</h1>"); out.print("method = "); out.print(_request.getMethod()); out.print("<br>content length = "); out.print(_request.getContentLength()); out.print("<br>"); String key; String values[]; Enumeration e = _request.getParameterNames(); while(e.hasMoreElements()) { key = (String)e.nextElement(); values = _request.getParameterValues(key); int length = values.length; for(int i = 0; i < length; i++) { out.print(key); out.print("("); out.print(i); out.print(") = "); out.print(values[i]); out.print("<br>"); } } } protected final static void dumpCookies(ServletOutputStream out, HttpServletRequest _request) throws IOException { out.print("<h1>Cookies:</h1>"); Cookie cookies[] = _request.getCookies(); int length = cookies.length; for(int i = 0; i < length; i++) { out.print(cookies[i].getName()); out.print(" = "); out.print(cookies[i].getValue()); out.print("<br>"); } } } ================================================== dumpform.html <html> <head> <title>Dump Form</title> </head> <body> <h1>Dump Form</h1> <hr> <center> <table width=100% cellpadding=5 cellspacing=0 border=1> <tr> <th> GET </th> <th> POST </th> </tr> <tr> <td> <form action="/dumpform" method="get"> <input type="hidden" name="itemid" value="a"> <input type="hidden" name="itemid" value="b"> Name <input type="text" name="name" value=""> <p> Password <input type="password" name="password" value=""> <p> Options <input type="CHECKBOX" name="option" value="o1" CHECKED>Option 1 <input type="CHECKBOX" name="option" value="o2">Option 2 <p> Gender <input type="RADIO" name="gender" value="m">Male <input type="RADIO" name="gender" value="f">Female <input type="RADIO" name="gender" value="o" CHECKED>Other <p> Message <TEXTAREA cols=20 rows=5 name="message"> </TEXTAREA> <p> Favorites <SELECT name="profile" MULTIPLE size=5> <OPTION value="1" SELECTED>Animals <OPTION value="2">Band <OPTION value="3" SELECTED>Cars <OPTION value="4">Clash <OPTION value="5">Door <OPTION value="6">Kinks <OPTION value="7">Led Zeppelin <OPTION value="8">Pink Floyd <OPTION value="9">Stones </SELECT> <p> <input type="submit"> <input type="reset"> </form> </td> <td> <form action="/dumpform" method="post"> <input type="hidden" name="itemid" value="a"> <input type="hidden" name="itemid" value="b"> Name <input type="text" name="name" value=""> <p> Password <input type="password" name="password" value=""> <p> Options <input type="CHECKBOX" name="option" value="o1" CHECKED>Option 1 <input type="CHECKBOX" name="option" value="o2">Option 2 <p> Gender <input type="RADIO" name="gender" value="m">Male <input type="RADIO" name="gender" value="f">Female <input type="RADIO" name="gender" value="o" CHECKED>Other <p> Message <TEXTAREA cols=20 rows=5 name="message"> </TEXTAREA> <p> Favorites <SELECT name="profile" MULTIPLE size=5> <OPTION value="1" SELECTED>Animals <OPTION value="2">Band <OPTION value="3" SELECTED>Cars <OPTION value="4">Clash <OPTION value="5">Door <OPTION value="6">Kinks <OPTION value="7">Led Zeppelin <OPTION value="8">Pink Floyd <OPTION value="9">Stones </SELECT> <p> <input type="submit"> <input type="reset"> </form> </td> </tr> </table> </center> </body> </html>