Frage

I am fairly new to Play and Scala, and stuck with a problem, it goes like: Simple explanation,

www.somesite.com/redirectedFoo //points to below controller

def redirectedFoo = Action{
//The http request needs to be traced here,
//and it needs to be accessed by a Java API method

val obj=new SampleMethod(--need http request obj as argument--)

//my problem is Play request and Http request are totally different 'objects', 
//and the above method uses HttpServletRequest obj, how is this handled in Play
}

Sorry I couldn't provide a proper explanation,

War es hilfreich?

Lösung

For your purposes, Play is the wrong place to be. Play is designed to be able to run outside of Servlet containers, and therefore implements an API for HTTP requests that does not conform at all to the Servlet API.

You have two options:

  1. Write your own function that converts a play.api.mvc.Request to your own implementation of javax.servlet.http.HttpServletRequest. Although HttpServletRequest is quite a big interface with dependencies on even more big classes, the information contained in the Play Request should be enough to construct an HttpServletRequest. Depending on your use you might leave some or most methods unimplemented to reduce the effort. Check out this class as an example.

  2. If you run your Play 2 application from a Servlet and your method allows, you can write your own Servlet application that runs in front of the Play application. It would then call the methods with the Servlet API you need when required, and forwards request to the Play 2 application when it is not needed.

These are both quite hacky but might be acceptable for a single use case. If you need integration with Servlet APIs or depend on a lot of other code that does, you really need to ask yourself whether Play 2 is the right choice. Play 2 can support any Java EE technology, but the Servlet API specifically is something that it does not support well. If you want to use the Servlet API from a modern Scala HTTP framework, you might want to look at Spray and its servlet support.

Andere Tipps

If the SampleMethod just uses a small subset, only read operations for example then it might be a viable to implement a HttpServletRequest wrapper that bridge the sevlet api to the play request. Note that there are many ways they differ so it is not certain that you would be able to bridge everything you need. If it depends on large parts of the servlet API then I think you are out of luck.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top