Showing posts with label Enumeration. Show all posts
Showing posts with label Enumeration. Show all posts

Tuesday, October 20, 2009

JSP - list all parameter keys and values

<%@page import="java.util.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP display all parameters</title> 
    </head> 
    <body> 
        <% 
 
            // you can get an enumeratable list 
            // of parameter keys by using request.getParameterNames() 
            Enumeration en = request.getParameterNames();
 
            // enumerate through the keys and extract the values 
            // from the keys! 
            while (en.hasMoreElements()) {
                String parameterName = (String) en.nextElement();
                String parameterValue = request.getParameter(parameterName);
                out.println(parameterName+":"+parameterValue+"<br />");
            }
 
            // now call your jsp file (from a browser and add on some paramters) 
            // file.jsp?a=12341234&b=apple&c=1.21gigawatts 
 
        %> 
    </body> 
</html> 
<!-- 
output: 
    a:12341234 
    b:apple 
    c:1.21gigawatts 
 
--> 
 

Thursday, September 24, 2009

Java - find the working directory


import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        // to find out where your application is running 
        //  (sounds easy but its sometimes tricky) 
        System.out.println("This Java applications working directory is:");
        // Java has a list of Properties that it uses for everything 
        // the working directory property is user.dir 
        // you can check it out with: 
        System.out.println(System.getProperty("user.dir"));
 
        // you can see all the properties with this: 
        System.out.println(System.getProperties());
        // it will just dump them all out in a not 
        //  organized way 
 
        // you can also iterate through all of the properties 
        //  and print them out line by linewith: 
        Properties myProps = System.getProperties();
        for (Enumeration e = myProps.keys(); e.hasMoreElements();/**/){
            String key = (String)e.nextElement();
            String value = myProps.getProperty(key);
            System.out.println(key + " = " + value);
        }
    }
}