how to get a path variable value in servlet -
i want path variable in servlet. assume url www.demo.com/123/demo
. want 123
value path without doing string manipulation operation.
note: following servlet doesn't have web.xml configurations. code is:
@webservlet(urlpatterns = { "/demo" }) public class demoservlet extends httpservlet { public demoservlet() { super(); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request,response); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { sysout("demo"); } }
the portion of url referring "context". use request.getcontextpath()
this. in case of example, return /123
. if want exactly 123
have remove leading slash.
from documentation:
returns portion of request uri indicates context of request. context path comes first in request uri. path starts "/" character not end "/" character. servlets in default (root) context, method returns "". container not decode string.
Comments
Post a Comment