Hide .jsp extension in JSP project using servlet mapping - java

I am writing an application in JSP, and I need to remove the ".jsp" extension from the URL. For example, I need:
http://example.com/search.jsp?q=stackoverflow
To be:
http://example.com/search?q=stackoverflow
I know that this can be done using the ".htaccess" file, but I need some other way. I have tried the following code:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.</url-pattern>
</servlet-mapping>
However, this did not work. Does anyone have some suggestions for ways to accomplish this? Thanks in advance for any help.

With a servlet mapping you need to specify each JSP individually like follows:
<servlet>
<servlet-name>search</servlet-name>
<jsp-file>/search.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>search</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
It's easier if all those JSPs are in a common path. E.g. /app/*.
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>com.example.FriendlyURLServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
with
request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
This assumes the JSPs to be in /WEB-INF folder so that they cannot be requested directly. This will show /WEB-INF/search.jsp on http://example.com/app/search.
Alternatively, you can use Tuckey's URLRewriteFilter. It's much similar to Apache HTTPD's mod_rewrite.

Related

index.html [welcome file] gives HTTP Status 404 – Not Found

please see my project structure here.I am using Spring MVC here is my code.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
when I hit run the project on server I got below error .
HTTP Status 404 – Not Found
as per the concept if we do not specify the welcome list tag then automatically index.html file will be searched.but in my case it is not working however it is working if I change the extension to .jsp.
If any one knows the answer please help me .
Note: I dont want to change the url mapping. I know there are alternatives but I want to know why this is happening and not showing me the result

Serving single page app next to other war file in Tomcat

I have a Tomcat server, serving two .war files. The first one is mapped to context /api, the second one to root /. The latter contains a single-page AngularJS app. It has the following web.xml config:
<servlet>
<servlet-name>webapp</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
When I go to https://my.url/, the index page of the single page app is properly served. So far, so good.
The problem is, when I deeplink to a page in my single page app, for example https://my.url/some/resource, Tomcat will give a 404. Because of the error-page config, it will still return the index page, but still with status 404. So, it kind of works, but not nicely.
Can I get Tomcat to return the index page with a proper 200 status code for all deep links? Of course, calls to /api should still resolve to the other deployed .war. I want to avoid duplicating the AngularJS url definitions in Tomcat, so it should just return the index page for any request that doesn't start with /api/.
For deeplinks you need to use wildcards on your webapp url pattern. Something like:
<url-pattern>/*</url-pattern>
Not sure would it not catch the /api/ calls then. Some more advanced url pattern might be required.

Hide servlet name in the url

In a website I have some links in a navbar like this
<li>PAGE</li>
and I load the linked page with JAVA in JSP page, then I submit data through a form by GET, after this my URL become
www.sitename.com/Servlet
then I'm not able to load the page by
?page=pagename
because I have the Servlet name in the URL.
How I can hide or delete the servlet name?
Use a servlet-mapping in your web.xml:
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
it's easier if all those JSPs are in a common path. E.g. /app/*.
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>com.example.FriendlyURLServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
with
request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
This assumes the JSPs to be in /WEB-INF folder so that they cannot be requested directly. This will show /WEB-INF/search.jsp on http://example.com/app/search.

Tomcat not serving static files

I'm at the end of my rope on this one. I'm try to get a super simple webapp up and I can't seem to get tomcat to not 404 static files.
I'm using the gradle tomcat plugin with tomcat version 7.0.39
My html file is at hey-world/src/main/webapp/index.html
My web.xml looks like this:
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>HeyWorldApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
So I thought this setup would map localhost:8080/hey-world/static/index.html to the file, but it 404's everytime. Is this a problem with some convention in the gradle tomcat plugin?
The URL-patterns used in web.xml/servlet-mapping is often a little simplistic. I think in your case, the /* pattern for Resteasy will work as a catch-all, so that no other mapping will really matter.
For debugging, I suggest you remove the Resteasy-servlet altogether, and see if you can serve static files from a custom URL with your mapping.
If that works, re-enable Resteasy, but on a different URL-pattern (eg. /rest/*).
If that works, well, then everything really works fine, it's just that the URL-mapping for /* blocks anything else from working.
The easiest solution would probably be to server static files as per default (no mapping), and serve rest-stuff from another URL.
Alternatively use two web apps. One with context root /static, one with context root /.

Tomcat 6.x web.xml default and custom servlet routing

I have two servlets defined in the web.xml file, namely the default2 and myservlet. The default2 servlet is used to map the static files like the javascript and css. The myservlet is used for getting dynamic content.
<servlet>
<servlet-name>default2</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:my-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The servlet mapping is defined as follows
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default2</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
When i try to access any files under /resources, i get a 404. Any ideas why this config is not working or change this config to make it work.
Tomcat's default servlet before 6.0.30 actually serves a static resource identified by HttpServletRequest.getPathInfo(), so that /style.css will be returned when /resources/style.css is requested.
Tomcat's behavior has changed from version 6.0.30 onwards. So the original configuration from the question works in newer versions of Tomcat. See "50026: Add support for mapping the default servlet to URLs other than /. (timw)" in the changelog.
Jetty's default servlet uses a full path.
It should work fine. Are those files in real also located in the /resources folder?
Your web.xml looks correct (except I would change your <load-on-startup> constants).
Make sure that your /resources exists and is a publicly visible folder in your project path and not under /WEB-INF folder.
Try changing your url-pattern for myservlet to /, and optionally adding <mvc:default-servlet-handler /> (see here) to your Spring configuration.
Removed wrong portion of the answer as per #BalusC comment.
Set a break point in your servlet and perform a debug session. Look for the path that your servlet is picking up these files at. Make sure they match the location

Categories