4

I wanted to know if there is a way to do similar code in java servlet like I do in express.js

In express I can say for example:

app.get('/:name',function(bla bla)){}

the :/name its a parameter in which the url of the get can be

localhost/kevin
localhost/joe

or whatever... This is great because I can take then for example that name (request.params.name) and so something with it. And it is also great because there is no limit(As far as I know) as to how many routes I can create, it just serves as a placeholder.

Is there a way I can do this using Java servlets?? I want to be able to have an html page that when I click a button it goes to localhost/button1 If I click another button it goes to localhost/button2.. and so on.. But also I'm letting the user create buttons dynamically so I don't want to create jsp pages beforehand, I just want the servletto create one..?

Thanks

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
Kevin Cohen
  • 1,211
  • 2
  • 15
  • 22
  • Kevin, you could look at using JAX_RS which is used for developing RESTFUL java/j2ee applications. https://docs.oracle.com/javaee/6/tutorial/doc/giepu.html – Sudhansu Choudhary Jul 07 '15 at 12:57

1 Answers1

6

Almost. With help of a prefix mapping /foo/* and HttpServletRequest#getPathInfo().

@WebServlet("/name/*")
public class NameServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getPathInfo().substring(1);
        // ...
    }

}

Invoke it as

You can optionally map the servlet on /*, but then it will act like a global front controller which isn't necessarily a good idea as you'd have to take static resources like CSS/JS/images and such into account.

In case you actually intend to create a REST service, rather look at JAX-RS instead of "plain vanilla" servlets. It would further reduce boilerplate code. See also a.o. Servlet vs RESTful.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So I'm having trouble doing this.. My servlet runs here: http://localhost:8080/demo/servlet/login If I go there I have like some text displayed so I see that it is working.. I added what you gave me but if I do http://localhost:8080/demo/servlet/login/name/kevin it cant find that link or if i do http://localhost:8080/demo/servlet/login/name also cant find it... any ideas? Also my mapping in web.xml (url-pattern) is: /servlet/login Maybe change something there? the name of the servlet is login – Kevin Cohen Jul 07 '15 at 13:09
  • 1
    If you intend to use exactly those URLs, and the context path is `/demo`, then the URL pattern must be `/servlet/login/name/*` (which is quite awkward, but that aside). The asterisk is important. The actual servlet name is irrelevant to this all. – BalusC Jul 07 '15 at 13:11
  • It worked! I had to change something on my web.xml (deleted the servlet map url thing) and ran it in localhost/'contextpath'/'nameofservlet'/name/kevin Thanks! – Kevin Cohen Jul 07 '15 at 13:23