* web.xml 파일에 추가될 내용

    <servlet>

        <servlet-name>PyServlet</servlet-name>

        <servlet-class>org.python.util.PyServlet</servlet-class>

        <init-param>

         <param-name>python/home</param-name>

         <param-value>/opt/usr/local/jython2.5.3</param-value>

        </init-param>

    </servlet>


    <servlet-mapping>

        <servlet-name>PyServlet</servlet-name>

        <url-pattern>*.py</url-pattern>

    </servlet-mapping>




* 수정 전 PyServlet: enter.py

import javax.servlet.http as http class enter(http.HttpServlet): def doPost(self, request, response): session = request.getSession(1) counter = session.getAttribute("counter") try: counter = int(counter) except: # counter is None counter = 0 counter += 1 session.setAttribute("counter", counter) response.contentType = "text/html; charset=utf-8" out = response.outputStream print >> out, """The string you entered is: %s. <br /> You have played this game %s times.\n<hr>""" % ( request.getParameter("thing"), counter) self.doGet(request, response) def doGet(self, request, response): response.contentType = "text/html; charset=utf-8" out = response.outputStream print >> out, """Enter a string: <form method="post"> <input type="text" name="thing"> <input type="submit"> </form>"""


* 수정 후 PyServlet: enter.py
# -*- encoding: utf-8 -*- import javax.servlet.http as http class enter(http.HttpServlet): def doPost(self, request, response): session = request.getSession(1) counter = session.getAttribute("counter") try: counter = int(counter) except: # counter is None counter = 0 counter += 1 session.setAttribute("counter", counter) request.setCharacterEncoding("utf-8") response.contentType = "text/html; charset=utf-8" out = response.outputStream print >> out, """The string you entered is: %s. <br /> You have played this game %s times.\n<hr>""" % ( request.getParameter("thing").encode("utf-8"), counter) self.doGet(request, response) def doGet(self, request, response): response.contentType = "text/html; charset=utf-8" out = response.outputStream print >> out, """Enter a string: <form method="post"> <input type="text" name="thing"> <input type="submit"> </form>"""



* Mac OS X Lion에서 Tomcat 7 & Jython 2.5.3을 사용하여 enter.py를 실행한 장면




Posted by Scripter
,