This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

Greg Bag

Ranch Hand
+ Follow
since Dec 25, 2010
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Greg Bag

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?
6 years ago
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.
6 years ago
Try this instead. Make sure your path is @Path("/Orders") also

6 years ago

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.

6 years ago
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?
6 years ago
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 {
6 years ago
Armitage you're always helping me! Thank you man.
6 years ago
Guys I have 2 questions.

1) How can I make sure every request goes through a front controller servlet?
So I have,,
Process is as follows:
A) Front controller receives json string
B) Parses the json string to a java object using jackson.
C) Puts the parsed object in request scope and forwards it to one of the other controllers(ControllerA or ControllerB)

My question is? How can I make sure every single request that comes in goes through the Can you please provide some code. Thank you.
So if they call first, it should keep the header information and forward it to the

2) Is having a static method call in the doGet() of a servlet thread-safe? So, StringUtilities.isNullOrEmpty(...), is that thread-safe? What if you call that same method by multiple doGet requests simultaneously, since it's static.

6 years ago

Marshall Blythe wrote:

Greg Bag wrote:So is this personDaoImpl thread-safe? If I was to use a single instance variable in the servlet, that wouldn't pose an issue? What if 1000 requests call createPerson at the same time on the same instance of PersonDaoImpl?

It's thread-safe, but you need to consider using the connection pooling features of your DataSource if your servlet is going to be handling hundreds of concurrent requests. The connection pool is usually administered in the JEE container and is transparent to the client application. Check your JDBC driver and JEE container documentation to see how to setup a connection pool.

The code above shows connection pooling.
6 years ago
I still don't understand the thread-safety portion. Let's say there is 1 instance in the servlet for PersonDaoImpl(). 5 different requests come in at the same exact time and call createPerson(firstname, lastname) on the PersonDaoImpl(), can't that corrupt data? Before the firstname/lastname is done updating by the 1st request, the 2nd request changes the firstname/lastname variables.
6 years ago
So is this personDaoImpl thread-safe? If I was to use a single instance variable in the servlet, that wouldn't pose an issue? What if 1000 requests call createPerson at the same time on the same instance of PersonDaoImpl?
6 years ago

E Armitage wrote:First, I would make the createPerson method return something that tells me if the create was successful or not and I wouldn't just hide the exception with that catch block.
You should be fine with regards to concurrency because you are not keeping state in your servlets and you are not keeping a connection as an instance variable. Generally when it comes to connections you should get one when you need it (in the method that needs it) and close it soon after using it.

Great! So what would you do with that exception? Can you give me an example code snippet. I usually have a logger log the exception. But above all, the design is correct yes?
6 years ago
Hello guys, I have a couple questions I would like to ask regarding correct design and concurrency. For the example, I created a simple application that takes parameters via servlet and adds to database. So the process is like so.
1) Send firstname/lastname to servlet
2) Servlet calls PersonDao.createPerson(firstname, lastname).

Classes involved...
PersonDaoImpl(Concrete Class)
AbstractDao(Abstract class)

I would like to know all your opinions if this is a correctly designed, connection-pooled, code. Is that static creation of the data-source correct? Would you change anything in the AbstractDao class that could pose a concurrency issue?

My other question is if there are concurrency issues here, specifically in the servlet. Imagine 1000 requests simultaneously hitting the servlet. What worries me is the PersonDaoImpl.
1000 different threads and each gets it's own stack. So 1000 different instances of PersonDaoImpl. If we go to AbstractDao, it calls getConnection on the datasource.

So questions would be...
Does the getConnection() pose a concurrency issue?
Can the 1000 different requests pose a threat to the datasource object from the above code?
What if there was a private PersonDao personDao = new PersonDaoImpl() as an instance in the servlet. Now what happens?

What I'm really confused on is what is happening inside the doGet when the PersonDaoImpl is instantiated. Can someone give me a walkthrough please. The gist of my question is if the code I have up there is thread-safe.

6 years ago

Sresh Rangi wrote:In one of these example, there are mutations to the object after the final variable is set. I don't think they are guaranteed to be visible. Consider two examples:

Example 1:

Example 2:

In example 1, the set is mutated after the instance variable is set. We have actions:

a) Thread1: - mySet initialization
b) Thread1: - set add
c) Thread2: - set contains

The happens-before relationships are: hb(a, b) and hb(a, c). This allows the orderings a -> b -> c and a -> c -> b.

In example 2, the set is mutated before the instance variable is set. We have actions:

a) Thread1: a - set add
b) Thread1: b - mySet initialization
c) Thread2: c - set contains

The happens-before relationships are: hb(a, b) and hb(b, c). This only allows a -> b -> c.

So reading a final variable only guarantees visibility of actions that happen before it's set.

So which one here is thread-safe? The first or second class or both?