1

I want to create a blogging like CMS in jsp. So I have a "post.jsp" which will dynamically display the content. I have a database which stores some url pattern and their content.

For example database stores:

url: www.example.com/abc
content: hello

url: www.example.com/xyz
content: world

I want to do that when a user will enter "www.example.com/abc" it will run "post.jsp" and check in database whether the url is present or not. If present then it will display "hello" else 404 error/

Similarly if user will enter "www.example.com/xyz" it will again run "post.jsp" and if the url present then it will display "world" else 404 error.

Every time the url should run "post.jsp" if no extension like html or jsp is present in the url pattern. If a url "www.example.com/contact.jsp" is entered by user then it should run contact.jsp without entering "post.jsp" because the url pattern contains a .jsp extension.

Hope you understand my requirement. How to do this?

Marged
  • 10,577
  • 10
  • 57
  • 99
Dev
  • 378
  • 2
  • 16
  • So basically you want to establish some javacode that gets called for any url that is opened ? The url will then be split and everything after the slash will be searched for in a database ? – Marged May 27 '15 at 21:18
  • exactly..but the javacode will run only if the url don't have any extension – Dev May 27 '15 at 21:22

1 Answers1

1

I would create a mapping for 404 in my web.xml and define a JSP / servlet to be called whenever a URL is called that is not found. In that Java code I would do something similar to this (pseudo code !):

String request = getRequestUrl();

if( !request.endsWithIgnoreCase( ".jsp" ) && !request.endsWithIgnoreCase( ".htm") {
  String tagToSearch = request.getEverythingAfterSlash();
  String content = Db.searchFor( tagToSearch );

  Response.write( content );
}

And please make sure that no-one is able to inject dangerous SQL by using this mechanism ! How this needs to be implemented depends quite a bit on which environment you use (Java version, Servlet version, Framework, Application server)

Marged
  • 10,577
  • 10
  • 57
  • 99
  • how this code will run every time a url is entered? because this code will be written in "post.jsp" and user entered url will not contain any post.jsp – Dev May 27 '15 at 21:35
  • i mean how to map 404 page to a jsp page in web.xml – Dev May 27 '15 at 21:37
  • 1
    This is what you specify in the web.xml. You put code into post.jsp and e.g. Tomcat will know to call this code when post.jsp gets called. And you define code (that can be the same as that in post.jsp) and map it to any other url that gets called. And in that case Tomcat will also know what to do. You should read more about url mappings to better understand this, http://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html might help in that case – Marged May 27 '15 at 21:38
  • Just saw that stackoverflow suggests this related link: http://stackoverflow.com/questions/14300404/how-to-show-the-requested-url-in-a-jsp-error-page?rq=1 – Marged May 27 '15 at 21:40
  • thanks bro. i understood what you are saying.. basically every url (those are not present) will go to 404 and there it will check in the database whether its present or not. am i right? – Dev May 27 '15 at 21:42
  • 1
    exactly. And then you can decide for which url you provide which content. – Marged May 27 '15 at 21:43
  • if i will redirect to 404 and then do the display stuff then url will be changed to the 404 page.. but i dont want to change the url until displaying the content. how to do that? – Dev May 28 '15 at 09:11
  • perhaps you can define a "catch-all" servlet-mapping for this. Just an idea, it should be possible from looking at the specs. But I never tried that myself. – Marged May 28 '15 at 09:20