admin管理员组文章数量:1356789
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
1 Answer
Reset to default 6Almost. 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.
本文标签: javascriptURL Routes with Java ServletsStack Overflow
版权声明:本文标题:javascript - URL Routes with Java Servlets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744061681a2584239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论