最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - URL Routes with Java Servlets - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Feb 27, 2024 at 10:04 Harrison 2,39512 gold badges23 silver badges46 bronze badges asked Jul 7, 2015 at 12:48 Kevin CohenKevin Cohen 1,3513 gold badges16 silver badges25 bronze badges 1
  • Kevin, you could look at using JAX_RS which is used for developing RESTFUL java/j2ee applications. docs.oracle./javaee/6/tutorial/doc/giepu.html – Sudhansu Choudhary Commented Jul 7, 2015 at 12:57
Add a ment  | 

1 Answer 1

Reset to default 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

  • http://localhost:8080/context/name/kevin
  • http://localhost:8080/context/name/joe
  • ...

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.

发布评论

评论列表(0)

  1. 暂无评论