01: /*
02: * @(#)getParameter.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.servlet;
10:
11: import pnuts.servlet.*;
12: import javax.servlet.*;
13: import javax.servlet.http.*;
14: import java.util.*;
15: import java.io.*;
16: import pnuts.lang.Context;
17: import pnuts.lang.PnutsFunction;
18: import pnuts.lang.PnutsException;
19:
20: /*
21: * getParameter(name {, encoding})
22: */
23: public class getParameter extends PnutsFunction {
24:
25: public getParameter() {
26: super ("getParameter");
27: }
28:
29: public boolean defined(int narg) {
30: return (narg == 1 || narg == 2);
31: }
32:
33: protected Object exec(Object[] args, Context context) {
34: int nargs = args.length;
35: if (nargs != 1 && nargs != 2) {
36: undefined(args, context);
37: return null;
38: }
39: String param = (String) args[0];
40: Map map = (Map) context.get(PnutsServlet.SERVLET_PARAM);
41: if (map == null) {
42: HttpServletRequest request = (HttpServletRequest) context
43: .get(PnutsServlet.SERVLET_REQUEST);
44: String enc;
45: if (nargs == 1) {
46: enc = ServletEncoding.getDefaultInputEncoding(context);
47: } else {
48: enc = (String) args[1];
49: }
50: map = (Map) readParameters.getInstance().call(
51: new Object[] { request, enc }, context);
52: }
53: return map.get(param);
54: }
55:
56: public String toString() {
57: return "function getParameter(name {, encoding})";
58: }
59: }
|