• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to forward to servlet from REST @POST method

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys how can I forward to a servlet from a rest @POST method? I'm using jersey. So the idea is I will have a rest front-end controller that accepts json.
1) Parses the json into an object.
2) Decides what properties the object has and then it redirects to another servlet accordingly.

My problem is it doesn't redirect, it stops.
The transferget does forward, but the transferpost does not.



Here is the client code.



Here is the servlet


Also is it safe to have @Context HttpServletRequest request and @Context HttpServletResponse response as instance variables? Thread-safety issue here? Would it be better to put them as arguments in the methods?

public void transferpost(Track track, final @Context HttpServletRequest request, final @Context HttpServletResponse response) throws ServletException, IOException {
}
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Bag wrote:My problem is it doesn't redirect, it stops.
The transferget does forward, but the transferpost does not.



Redirect? Let's get the terminology right, your code forwards. It doesn't redirect, which is a different thing.

And wouldn't your transferpost forward to the servlet's doPost() method? Since that method doesn't do anything, you could easily interpret that as "stopping".
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Bag wrote:how can I forward to a servlet from a rest @POST method?


This seems a really odd design for a REST service. What you you hope to achieve by having a servlet handle it? If the reason is that the same logic is used both by the WS and a servlet that you need for other purposes, then the proper approach would be to put that logic into some other Java class which would be used by both WS and servlet.

Also is it safe to have @Context HttpServletRequest request and @Context HttpServletResponse response as instance variables? Thread-safety issue here?


That is thread-safe, because -in contrast to servlets- JAX-RS service objects are instantiated for each request.
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the whole idea that I have going on is that any request that would come in would go through the rest service first then be forwarded to a servlet.
The reason I do this is I want the rest service to take care of the automatic conversion from json to object. I don't want that conversion
to take place in a servlet.

Now you may think why wouldn't I just have 1 front controller servlet do the parsing instead of the rest service, then forward to the other servlets?
Well with the rest service, it's way more easier to add new paths. Let's say that I changed the model that was being converted
to an object, in this case (Track). I changed it to a Track1 object. All I have to do is add another path to parse that specific object.

If I'm wrong, can someone please tell me why is it incorrect to have a rest front controller forward to servlets?
Alternatives you would suggest?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I don't understand why there are servlets at all. Unless they are also used for some web app that emits HTML to browsers and needs to use the same logic (in which I would still say the approach I outlined in my previous post is a better design), using servlets seems a complication that doesn't add value. Am I missing something?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to agree with Ulf -- it sounds like you are over-complicating things. Are there considerations you have omitted?
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:I guess I don't understand why there are servlets at all. Unless they are also used for some web app that emits HTML to browsers and needs to use the same logic (in which I would still say the approach I outlined in my previous post is a better design), using servlets seems a complication that doesn't add value. Am I missing something?



So the flow is this
1) Any request that comes in automatically gets redirected to the rest service via a filter.
2) REST parses the json and turns it into an object.
3) Based on certain properties in the object, the properties of the object get forwarded to certain servlets for additional processing.
4) The reason im using rest for front end and not a regular servlet is because in a regular servlet there is just a doGet()/doPost() methods. In rest, I can add many paths.

So the alternative of using rest would be...
1) Front Controller servlet retrieves the post body, I have to manually define an ObjectMapper to turn the json into an object, then I forward to other servlets.

The only difference between the 2 different designs that I'm seeing is.
1) Rest takes care of the json to object mapping for me and I can add many paths as I want.

Also, do any of you have a good url or some site that gives a tutorial on how to use REST with like jdbc. Maybe a simple CRUD example with jdbc and rest. Thanks guys.

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Bag wrote:
So the flow is this
1) Any request that comes in automatically gets redirected to the rest service via a filter.


Why a filter and why the redirect? Why aren't the URLs directly mapped to their service? Or are you using the filter as a front controller? If so, why a redirect?

2) REST parses the json and turns it into an object.
3) Based on certain properties in the object, the properties of the object get forwarded to certain servlets for additional processing.


Why a servlet? What's the need for a forward? Why don;t you just call methods as appropriate. I donl;t see the need for servlets here -- the request has already been fielded, there's no need for further processing to be in a servlet.

4) The reason im using rest for front end and not a regular servlet is because in a regular servlet there is just a doGet()/doPost() methods. In rest, I can add many paths.


Are you really creating a RESTful API? It almost sounds like you are using it for side effects?

 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the filter forwards any request to the rest service. The rest service is the front controller. It then delegates to each servlet for further processing(updating database)

I guess I don't know how to properly use databases with rest. I forward to the servlet so database updates can be made.

I'm new to rest so maybe I don't quite fully understand it.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I guess I don't know how to properly use databases with rest. I forward to the servlet so database updates can be made.


I think this goes to the heart of the matter. Why do you think using a DB with a REST APi would be any different than using a DB with a servlet?
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

I guess I don't know how to properly use databases with rest. I forward to the servlet so database updates can be made.


I think this goes to the heart of the matter. Why do you think using a DB with a REST APi would be any different than using a DB with a servlet?



So inside a rest method I would use a Dao class to update a database just like I would do inside a servlets doPost() method? So what is the whole point of using rest? What are the real benefits and situations you would use rest over servlet?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
REST is a specification for a type of web API. If you are not creating a RESTful API, then I cannot tell you why you would want to "use rest". I asked you about that earlier.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would typically use a servlet for building web apps that creates HTML pages for human consumption. You would typically use a REST API for something that's accessed by a programmatic client, like a mobile app, some other web app, a desktop app, or a web page using AJAX. So they're both used to create frontends (the "V" in MVC) for different use cases .
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic