In The Chamber of Secrets, Harry and Ron were still in their second year. Though Lockhart seemed completely stupid, he must have at least graduated from Hogwarts and so must have more knowledge of magic than the pair of them. How was he threatened by two far more inexperienced wizards than himself?
I don't actually think he was feeling threatened by them at all, quite the contrary: he thought they were just second years he could easily deal with. He is scared that the other teachers might force him to actually face the monster, hence his hectic packing in order to get away quickly - and the threat the boys pose for him is the fact that they slow him down.
With regards to their abilities compared to his, well he is so careless and overconfident he tells them what he is going to do before he does it - or tries to at least:
[Lockhart] banged the lids of his trunks shut and locked them.
“Let’s see,” he said. “I think that’s everything. Yes. Only one thing left.”
He pulled out his wand and turned to them.
“Awfully sorry, boys, but I’ll have to put a Memory Charm on you now. Can’t have you blabbing my secrets all over the place. I’d never sell another book—”
Harry reached his wand just in time. Lockhart had barely raised his, when Harry bellowed, “Expelliarmus!”
Lockhart was blasted backward, falling over his trunk; his wand flew high into the air; Ron caught it, and flung it out of the open window.
-- Harry Potter and the Chamber of Secrets, Chapter 16, The Chamber of Secrets
Afterwards he acts more scared, but that is to be expected, they are armed whereas he lost his wand. The second he gets his hand on a wand he again boasts first and then attacks, which shows (to me at least, YMMV) he doesn't actually feel threatened by them, but believes them to be inferior to himself. This time only the fact Ron's wand backfires on him saves the boys.
Lockhart is a conman (a particularly bad one, as those that actually know him - particularly the other teachers - see him as the fraud he is, and his ineptitude points the kids there rather quickly as well) and he has the con(fidence) part down, meaning most of the time he puts on an act. So being faced with two wand-wielding kids that force him to meet a monster, he acts the part or he is actually scared, but of the monster not the kids themselves.
Lockhart himself made it clear he wasn’t very good with magic — just phenomenal with memory charms — particularly with obliviate.
In contrast, Harry Potter was a famous wizard who as a baby defeated the most feared and powerful Dark Wizard of all time! And his friend.
Not to mention that the two of them seemed to know what they were about, where Lockhart was perpetually dealing with his own imposter syndrome issues.
I don’t know how well-known outside of Hogwarts it was that Harry confronted Voldemort/Quirrel and three-headed dogs and the like as well, but people were taking notice. Lucius Malfoy, for example.
And Lockhart wasn’t unaware of people around him, continuously watching for whom he may seek to exploit next. Harry and Ron must have seemed much larger than life to him.
In the Harry Potter book series we mainly see Ron, Harry, Hermione, professors, Voldemort, and Voldemort's inner circle. This distorts our view on what a typical wizard is like.
In Harry Potter and the Half-Blood Prince (chapter 6) we learn that most adult wizards can't cast shield charms. Fred and George are therefore making quite a profit from selling protective clothing with shield charms attached.
Magic in the wizarding world is a bit like math in the human world. All of us have lots of experience with it in school, we all have to take advanced math courses, and most of us still struggle with basic algebra.
Pause for a second and let's go back to your question. In Harry's third year he learns the Patronus Charm, considered a very difficult spell. In his fourth year, he learns the shield charm. In his fifth, he actually teaches others the charm. Harry seems to be quite advanced as a magician at an early age. To us as readers, this is easily missed. We're comparing him to Hermione, Dumbledore, Snape, and Voldemort.
In the second book, it is pretty clear that Lockhart is a subpar magician. Harry and Ron, together, may be at his level. Going back to math. Two seventh graders who score 70s regularly in math are likely better than your average adult.
Because he was indeed threatened
The duo might not be experienced (as 2nd year students), but they're armed with wands and there are plenty of things they can do to him since he doesn't have his wand anymore.
He's at their mercy, and they're obviously prepared to do unpleasant things to him if he doesn't do what they demand.
It's perfectly reasonable for an experienced adult wizard to feel threatened under those circumstances.