为什么 fieldStorage 在 Jython Servlet post 请求中为空

Why fieldStorage is empty in Jython Servlet post request

我使用 doPost 方法创建了一个简单的 jython servlet 文件。我正在尝试 post 一个表单,但在 servlet 中我没有得到 post 值。下面是我正在尝试的一段代码。

Test.html
---------
<form action='Index.py' method='post'>
   <input type="text" name="username" value="ifti" />
   <input type="text" name="password" value="ifti1233" />
   <input type="submit" name="Submit" value="Submit" />
</form>

Index.py
--------

from javax.servlet.http import HttpServlet
import cgi, cgitb, os, sys, shutil
import urllib2
import org.python.util.PyFilter

cgitb.enable();

class Index(HttpServlet):
    def doPost(self,req,res):   
        res.setContentType("text/html")
        form = cgi.FieldStorage()
        out = res.getWriter()
        keys = form.keys()
        keys.sort()
        pageContent = "<br />number of keys: "+str(len(keys))
        for key in keys:
               pageContent += fieldStorage[ key ].value
        out.println(pageContent)

cgi.FieldStorage 期望其输入来自进程的标准输入或其环境。这就是 Web 服务器和 CGI​​ 请求处理程序合作处理 HTTP 请求的方式。

但是,Servlet 使用不同的协议将原始 HTTP 请求从 Web 服务器传输到请求处理程序,并且 HTTP 请求不容易从标准 input/environment 中读取。这就是为什么您的 cgi.FieldStorage() 没有检测到任何 post 字段。相反,已处理的请求在传递给 doPost()HttpServletRequest object 中提供。您可以调用 HttpServletRequest 上的方法来获取 post 字段和 headers,例如

import cgi
from pprint import pformat
from javax.servlet.http import HttpServlet

class Index(HttpServlet):
    def doPost(self,req,res):
        res.setContentType("text/html")
        out = res.getWriter()

        out.println('<strong>Headers</strong>')

        headers = {}
        for n in req.headerNames:
                headers[n] = req.getHeader(n)
        out.println('<pre>')
        out.println(pformat(headers))
        out.println('</pre><br />')

        out.println('<strong>Form fields</strong>')

        # get the POST data fields
        form = {}
        for field in req.parameterMap:
                form[field] = list(req.parameterMap[field])

#        # Alternative - if you *really* want a FieldStorage object, construct
#        # one from the request body, headers and faked environment variables.
#        # N.B. assumes that the request body is one line of POST data only.
#        from StringIO import StringIO
#        request_body = req.reader.readLine()
#        form = cgi.FieldStorage(fp=StringIO(request_body),
#                    headers=headers,
#                    environ={'REQUEST_METHOD': 'POST',
#                             'CONTENT_LENGTH': len(request_body)})

        out.println('<pre>%s</pre><br />' % pformat(form))

使用您的测试表单,您应该会看到类似这样的输出:

Headers

{'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 'accept-encoding': 'gzip, deflate',
 'accept-language': 'en-au,en-us;q=0.7,en;q=0.3',
 'connection': 'keep-alive',
 'content-length': '45',
 'content-type': 'application/x-www-form-urlencoded',
 'host': 'localhost:8080',
 'referer': 'http://localhost:8080/jython-servlets/test.html',
 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0'}

Form fields

{'password': ['ifti1233'], 'Submit': ['Submit'], 'username': ['ifti']}