Maverick
Maverick

Reputation: 2760

Clean URLs using jsp/ servlets?

I am planning to make a CMS using jsp and servlets. Could anyone tell me how to implement clean urls using this technologies?

Upvotes: 2

Views: 7669

Answers (4)

BalusC
BalusC

Reputation: 1109875

Make use of HttpServletRequest#getPathInfo() in the servlet which is acting as front controller.

Kickoff example without any trivial validation:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
}

This will make a request on for example http://example.com/context/servlet/foo/bar to display the /WEB-INF/foo/bar.jsp file. The JSP files should be placed in /WEB-INF to prevent them from direct access.

See also:

Upvotes: 3

Truong Ha
Truong Ha

Reputation: 10964

Use URLRewriteFilter or you can write it by yourself, it's quite simple if you know how to use deployment descriptor and filter. For example, you have a servlet that responses content based on request parameter such as a.com?cat=book&post=java (call it showContent servlet) and you want to rewrite the url to a.com/book/java so you should create a filter: filter name: dispatcher mapping: /*

and in that filter, you should handle the string "/book/java" to generate data for cat and post variables. Then just forward it to the showContent servlet to handle the request.

Upvotes: 0

Kurt Du Bois
Kurt Du Bois

Reputation: 7665

You could try using urlrewritefilter: http://code.google.com/p/urlrewritefilter/. This uses a servlet filter and an xml-file to allow your application to have clean url's. The construction of the clean url's would be your own responsibility.

Upvotes: 4

Maurice Perry
Maurice Perry

Reputation: 32831

I use the JSTL <c:url> tag

Upvotes: -1

Related Questions