How can I create a servlet with jruby (running wit

2019-04-02 13:47发布

问题:

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!

回答1:

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>

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

To harvest data that was sent via GET simply define doGet in similar manner.



回答2:

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 the doGet()/doPost() methods. You will want to return the form as you've done in the above example.



回答3:

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/