问题描述
我对jruby和java很新,并希望在使用jetty作为Web服务器时在jruby中创建一个servlet。我不确定我是否正确使用以下代码显示输入表单到目前为止。我想我现在必须扩展HttpServlet类来处理发布的数据,但我不知道如何在这种情况下执行此操作,如果可以在同一个脚本中执行此操作。
I am pretty new to jruby and java and want to create a servlet in jruby while using jetty as web server. I am not sure if I am on the right way with the following code which shows the input form so far. I guess I have to extend now the HttpServlet class to handle the posted data but I don't know how to do this in this case and if it is okay to do this in the same script.
require 'java'
Dir["./jetty-6.1.18/lib/*jar"].each { |jar| require jar }
Dir["./Java/lib/jsdk2.1/javax/*jar"].each { |jar| require jar }
include_class 'javax.servlet.ServletException'
include_class 'javax.servlet.http.HttpServlet'
include_class 'javax.servlet.http.HttpServletRequest'
include_class 'javax.servlet.http.HttpServletResponse'
include_class 'org.mortbay.jetty.Server'
include_class 'org.mortbay.jetty.handler.AbstractHandler'
include_class 'org.mortbay.jetty.servlet.Context'
include_class 'org.mortbay.jetty.servlet.ServletHolder'
def main
handler = Handler.new
server = Server.new(8080)
server.setHandler(handler)
server.start()
end
class Handler < AbstractHandler
def handle(target, request, response, dispatch)
response.setContentType("text/html")
response.setStatus(HttpServletResponse::SC_OK)
response.getWriter().print('
<form action="RequestProcessing" method="post" enctype="multipart/form-data">
<p>Select a file:<br>
<input name="file" type="file" size="20" maxlength="1000" accept="text/*">
</p>
<input type="submit" value=" Send"/>
</form>')
request.setHandled(true)
end
end
class RequestProcessing < HttpServlet
# So what do we do here?
end
main
我会感谢任何提示。非常感谢提前!
I would be thankful for any hints. Many thanks in advance!
推荐答案
我得到了一些外部帮助,可以提出正确的解决方案。为了提供完整但简单的设置,我使用html文件进行数据输入(但这可以在jetty中完成,如上所述)。
I got some external help and can present a proper solution. To offer a complete but simple set-up I use a html file for the data input (but this could be done in jetty as done above).
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Data input</title>
</head>
<body>
<form action="http://localhost:8080/" method="post">
<textarea name="input" cols="4" rows="20"></textarea>
</p>
<input type="submit" value=" Send"/>
</form>
</body>
</html>
jruby部分容易混淆;):
The jruby part is confusingly simple ;):
require 'java'
Dir["./Java/jetty-6.1.18/lib/*.jar"].each { |jar| require jar }
Dir["./Java/lib/jsdk2.1/javax/*.jar"].each { |jar| require jar }
include_class 'javax.servlet.http.HttpServlet'
include_class 'org.mortbay.jetty.Server'
include_class 'org.mortbay.jetty.servlet.Context'
include_class 'org.mortbay.jetty.servlet.ServletHolder'
def main
server = Server.new(8080)
context = Context.new(server, '/', 0)
servlet = TestServlet.new()
holder = ServletHolder.new(servlet)
context.addServlet(holder, '/')
server.start()
end
class TestServlet < HttpServlet
def doPost(request, response)
input = request.getParameter('input')
response.writer.println("
<html>
<head><title>Output</title></head>
<body>
Raw input: <pre>#{input}</pre>
</body>
</html>")
request.handled = true
end
end
main
要收集通过GET发送的数据,只需以类似的方式定义doGet。
To harvest data that was sent via GET simply define doGet in similar manner.
这篇关于如何使用jruby(使用jetty运行)创建servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!