-2

I am working on a Servlet/JSP project, the website have users and I need to display a user's profile using the notation myDomain.com/Joe instead of myDomain.com/users.jsp?user=Joe.

An instance of my case is this current page, the link is displaying myDomain.com/posts/thePost:

https://softwareengineering.stackexchange.com/questions/355478/what-method-approach-is-used-to-query-a-users-page-on-a-website-using-backslas

Not:

https://softwareengineering.stackexchange.com/questions.jsp?question=what-method-approach-is-used-to-query-a-users-page-on-a-website-using-backslas

Generally speaking my idea is that I need to first fetch the "/Joe" part and send it to the doGet or dopost method inside a servlet called "get_user.java", then the servlet shall send back the information associated with "/Joe" to the JSP page, but of course I could be wrong.

I tried finding answers regarding this and most answers are very broad, for instance This one is taking about "RESTfull" pages, I don't know if the "Restfull" part have anything to do with this or not because I am not familiar with RESTfull pages.

Any way the question is not about what restfull pages are, but how to accomplish what I described above, a strategy description will help me here

  • 1
    What you're looking for is called either "URL Rewriting" or "URL Routing" (depending on what your specific goals are). – Robert Harvey Aug 11 '17 at 2:17
  • 1
    See servlet mapping under web.xml file – Narender Parmar Aug 11 '17 at 5:42
2

A naive, quick approach would be to use apache mod_proxy in front of your application server, to route requests from external /(.*) to internal /users.jsp?user=$1

But that's hard to manage eventually.

You can use Spring MVC to create a "path variable", as this SO response explains: Spring mvc @PathVariable.

This will allow you to have mydomain.com/users/Joe, then the Spring controller will redirect to the JSP view using "Joe" as a model variable that you can use however you wish. That might require you to start using Spring which is non-trivial.

If you're limited in what technology you can add, then you should mimic Spring's behaviour by reading the request object, parsing the path url, and routing to the correct jsp, as you originally guessed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.