Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?

Does servlet support urls as follows:

/xyz/{value}/test 

where value could be replaced by text or number.

How to map that in the web.xml?

like image 470
BlackEagle Avatar asked Jan 03 '12 16:01

BlackEagle


People also ask

How do I map a servlet in Web XML?

To map a URL to a servlet, you declare the servlet with the <servlet> element, then define a mapping from a URL path to a servlet declaration with the <servlet-mapping> element.

How is servlet mapping done?

Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.

What is use of url-pattern tag in servlet XML file?

The url-pattern element of a servlet-mapping or a filter-mapping associates a filter or servlet with a set of URLs. When a request arrives, the container uses a simple procedure for matching the URL in the request with a url-pattern in the web. xml file.

Can servlet mapping have multiple url patterns?

Previous versions of the servlet schema allows only a single url-pattern in a filter mapping. For filters mapped to multiple URLs this results in needless repetition of whole mapping clauses.

What is servlet URL parameter?

Java Servlet Url Parameters Example Posted by: Yatin in servletNovember 3rd, 20170Views Servletsare modules of the Java code that run in a server application to answer the client requests. They are not tied to a specific client-server protocol but are most commonly used with HTTP.

What are Java servlets?

Since Servlets are written in the highly portable Java language and follow a standard framework, they provide a means to create the sophisticated server extensions in a server and the operating system in an independent way. Want to be a Servlets Master ? Subscribe to our newsletter and download the Java Servlet Ultimate Guide right now!

What are the benefits of using servlets?

The basic benefits of Servlet are as follows: Less response time because each request runs in a separate thread Servlets are scalable Servlets are robust and object-oriented Servlets are platform-independent Servlets are secure and offer portability Fig. 3: Benefits of using Servlets


1 Answers

It's not supported by Servlet API to have the URL pattern wildcard * in middle of the mapping. It only allows the wildcard * in the end of the mapping like so /prefix/* or in the start of the mapping like so *.suffix.

With the standard allowed URL pattern syntax your best bet is to map it on /xyz/* and extract the path information using HttpServletRequest#getPathInfo().

So, given an <url-pattern>/xyz/*</url-pattern>, here's a basic kickoff example how to extract the path information, null checks and array index out of bounds checks omitted:

String pathInfo = request.getPathInfo(); // /{value}/test String[] pathParts = pathInfo.split("/"); String part1 = pathParts[1]; // {value} String part2 = pathParts[2]; // test // ... 

If you want more finer grained control like as possible with Apache HTTPD's mod_rewrite, then you could look at Tuckey's URL rewrite filter or homegrow your own URL rewrite filter.

like image 92
BalusC Avatar answered Sep 22 '22 03:09

BalusC