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!
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).
The jruby part is confusingly simple ;):
To harvest data that was sent via GET simply define doGet in similar manner.
I'm guessing you're working from the Embedding Jetty document (since you've started with a handler).
I would (instead) check further down that document and follow the Quick Start - Servlets section. Derive your own servlet from
HttpServlet
and implement thedoGet()/doPost()
methods. You will want to return the form as you've done in the above example.I know this isn't really an answer to your question, but in (J)Ruby-land we tend to use mongrel or webrick instead of jetty.
http://mongrel.rubyforge.org/web/mongrel/files/README.html
http://www.webrick.org/