Adrián Molčan
Adrián Molčan

Reputation: 139

How to shorten URL address in JSP

I have a question about shortening of URL address when working with Java Servlets. I need to shorten address e.g. www.IPaddress.com:8181/JSP/Index.jsp just to www.IPaddress.com without port number (8181) and .jsp extension. Is it even possible to do something like this? I developed this Servlet application in Eclipse IDE and I am working with Apache Tomcat 8.

Upvotes: 3

Views: 784

Answers (1)

utpal416
utpal416

Reputation: 927

Usually this kind of mapping can be achieve by creating a virtual host in some web server like apache and map it to redirect different host based on the requirement.

Using only tomcat I tried below steps and achieve your requirement .

  1. Modify your tomcat's server.xml to remove the context path as blank for the host like below

    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="PATH_OF_YOUR_TOMCAT/webapps/YOU_APPLICATION_NAME"/>

  2. You have to run your tomcat using port 80 to remove the port from URL. So you can either change the existing port in your server.xml to 80 or add one more connector in your tomcat's server.xml with port 80. i.e in this case your tomcat will listen to both 8181 and 80. Make sure you restart tomcat using admin as port 80 is a privilege port.

    <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    

    <Connector port="8181" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

After following above 2 steps You should be able to access your application using http://localhost/index.jsp

  1. Now You can use UrlRewriteFilter to hide the JSP extension in both request and response or can use servlet mapping like below

    <servlet> <servlet-name>ServletName</servlet-name> <jsp-file>some.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>ServletName</servlet-name> <url-pattern>/somelink</url-pattern> </servlet-mapping>

You can also put your index.jsp as a welcome file list in web.xml like below

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

Now You should be able to access your application using below as per your requirement http://localhost

Upvotes: 1

Related Questions