1

In spark-java does the req object not have the RequestDispatcher? How can i forward a request instead of doing res.redirect in spark-java ?

I have tried doing

req.raw().getRequestDispatcher("/forwardPath").forward(req,res);

but getting a compile error saying

Error:(89, 82) java: incompatible types: spark.Request cannot be converted to javax.servlet.ServletRequest

Yurets
  • 3,999
  • 17
  • 54
  • 74
Samant
  • 92
  • 1
  • 3
  • 13

2 Answers2

0

Reason:

You are getting this error because forward(req, res) requires instances of javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse instances where Spark provides instances of spark.Request and spark.Response.

Solution:

Instead of using this way for request dispatching, Spark provides a different way of doing this.

You can use the method provided in documentation http://sparkjava.com/documentation#redirects

res.redirect("/forwardPath", 301);

It works for me. Please let me know if this works for you as well.

  • 2
    hi Satendar : the intent is to forward the request to another resource and not redirect. Redirect will still send the response back to the client(browser) and make it do a new request..also this causes the browser url to change ... hence looking for server side forward of the request – Samant Jul 07 '18 at 13:37
0

I suppose you need to provide req.raw and res.raw to the forward method invoked on the request dispatcher. You're currently providing it with Spark domain objects. Hence the error message.

pka
  • 563
  • 4
  • 9