JavaLearner
JavaLearner

Reputation: 11

How to use username based url in JSP?

I am trying to create user based JSP web application. And in this app, I want to add profile feature like social networking website and it will look like site/Username and site/UserProfile etc.

Believe me, I don't want make any other unwanted social networking website. But in my site I want to add feature like site/Username/about to show about page of that particular person "Username" and other features like site/Username/contact, site/Username/skills etc. If person is logged in then I want to use site/Username/settings, site/Username/account, site/Username/edit etc. So can anyone tell me how can I create this webapp with JAVA/JSP? Using servlet, I can do site/settings, site/accounts, site/edit etc but I don't want to do this. I want to do as I described (with username feature). Can anyone suggest me what should I do and if there is any framework that can help me with please let me know. Right now, I am using only jsp/java with eclipse so do I need to change IDE?

Thank you in advance.

Upvotes: 0

Views: 302

Answers (2)

Anurag Anand
Anurag Anand

Reputation: 498

I don't think you need to use any API for any stuff other than servlet to get it done. All you need is to use request.getPathInfo() .

you will have to create a controller servlet or a filter for example site as you have specified in your question. This site servlet will act as a controller, you will use site/Anurag/contact , where Anurag is the name of a user, in this case it is me. request.getPathInfo() will return /Anurag/contact.

you can deal it like this

String pathInfo = request.getPathInfo();
String[] pathParams = {};
if(validations for pathInfo){
 pathInfo  = pathInfo.substring(pathInfo.indexOf("/")+1);    
 pathParams  =  pathInfo.split("/");
}

pathParams[0] will have username

pathParams[1] will have the name of operation the user wants to perform

if(pathParams[1].equalsIgnoreCase("contact")){
    then redirect to View for contact
}

OR

if(pathParams[1].equalsIgnoreCase("setting")){
    then redirect to View for setting
}

It can be implemented in this way. I think stackoverflow redirects to Questions in the same way.May be it uses APIs,I don't know, but it will clear the message that I want to convey.

When you hit the Questions button on the page, it displays this URL http://stackoverflow.com/questions .

You can directly write down the question number after this URL and it will take you to that question. http://stackoverflow.com/questions/26920116 . This is way it is passes the question number.

In the same way This Usename specific URL can be implemented. You will have to make site a controller.

Upvotes: 0

Jamie White
Jamie White

Reputation: 1620

There are various Web Frameworks that enable you to develop RESTFul Web Applications (e.g. Struts, JSF, Spring MVC etc). I personally prefer Spring MVC, you can read more about it here

Upvotes: 1

Related Questions