web.xml servlet mapping infinite loop - java

I am using appengine and seem to be having some problems with url routing
My web.xml
<servlet>
<servlet-name>ViewServlet</servlet-name>
<jsp-file>viewdata.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>ViewServlet</servlet-name>
<url-pattern>/view/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
The redirection works just fine when I test in the local machine. On upload to appengine when I try to navigate to http://myurl/view/ it redirects infinitely to
http://myurl/view/default.html/default.html...
Is this the right way to redirect in web.xml. Am I missing something here. It work fine on the local machine. Just goes into an infinite loop when uploaded onto gae. Any help would be appreciated...

Note that the <jsp-file> must start with a forward slash (/) if the JSP is in the application's root directory.

Related

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.

How to get my app-engine home page from a Servlet

I have created an app-engine connected android project. The way this works is when you open your webapp directory there is an index.html, which serves as the home page of the api. What I want is to create a Servlet that will generate that home page for me instead of serving it from this webapp/index.html. I have created my servlet. But I am not sure how to refer to it inside web.xml
I imagine I have to replace
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
If you want to keep it simple you can remove the static index.html altogether and create a servlet mapping like this (in web.xml).
<servlet>
<servlet-name>MyWelcomeServlet<servlet-name>
<servlet-class>domain.package.subpackage.class</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyWelcomeServlet</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
Note: removing or renaming the static index.html is important since Google's content delivery network will serve static files before routing requests to app engine instances.

How to intercept a url and redirect it to a jsp page?

I want to develop a application and deploy in WebSphere where the requirement is:
if there are any request like http://appserver1:9080/ - it will reach to a landing jsp page
for example http://appserver1:9080/index.jsp
Is it at all possible to redirect to a page even if I don't mention
the resource name?
If you want to redirect from server's root, this is not about your java code or project configuration, it is about server configuration.
Look here for WebSphere configuration.
For JEE projects, on web.xml, you can define as;
<web-app>
....
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
</web-app>
So
http://localhost:8080/myproject
will load index.jsp
Source for details
From what you are describing, the redirection can be pretty much handled by servlets mapping. Read here:
https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html
You can be able to intercept URL requests and process:
// Servlet definition on your web.xml
<servlet>
<servlet-name>ServletHandler</servlet-name>
<servlet-class>com.servlets.ServletHandler</servlet-class>
</servlet>
// This maps all requests to the above defined servlet for processing:
<servlet-mapping>
<servlet-name>ServletHandler</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I hope this helps
You can define welcome files in web.xml
<web-app>
...
<welcome-file-list>
<welcome-file>index.jsp/welcome-file>
</welcome-file-list>
...
</web-app>
But according to the spec index.html, index.htm and index.jsp are welcome files by default. So you probably don't need to configure anything when the file is called index.jsp.

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 /.

Should url of Ajax's $.post match url in the web.xml

I am trying to send data from js to a servlet.
my js is in webapp\secure folder while the servlet s in java\com\ servlet folder.
I read for sending data using ajax I use
$.post("someservletname", {cityName:"hello"});
In the web.xml
<servlet>
<servlet-name>someservletname</servlet-name>
<servlet-class>java.com.someservletname</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>someservletname</servlet-name>
<url-pattern>/someservletname</url-pattern>
</servlet-mapping>
But when i put a simple sysout.print statement I do not see anything. How do i make sure that the mapping worked properly.Should i give the complete url in the post function call?
Ensure you put in the URL:
context path of your web application
Regards
Philippe

Categories