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

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

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 configure Spring 4 WebSocket with sockjs using xml configuration

Hi my current websocket configuration is.
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/chat">
<websocket:sockjs></websocket:sockjs>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
and my url pattern configuration is
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
if i change to pattern to '/' then it works fine else
the give an error
GET http://localhost:8080/demoapp/chat/info 404 (Not Found)
what is wrong please suggest.
thanks
When using the *.html pattern, you're only mapping those requests to the DispatcherServlet - all other requests (in this case, all SockJS/websocket/etc requests) are ignored and an HTTP 404 error is returned by the servlet container.
So yes, you should be using "/" as a mapping pattern otherwise it won't work.
It can be hard to start writing a websocket application - a lot of new concepts and things to pay attention to. But the programming model is actually quite close to Spring MVC.
Here are a few pointers to help you:
Some "light reading" of the reference documentation (schemas are really helpful)
Try the official getting started guide on websocket
Take a look at well designed example applications such as a portfolio app or a chat room app
How about
add this code
<async-supported>true</async-supported>
on web.xml file
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
I'm use like this code
It work and doesn't show Path error

Google App Engine /del url error

I'm getting an error when I try to delete via link /del. It gives me this error, as shown bellow:
Error: Not Found
The requested URL /del?eid=5769015641243648 was not found on this server.
I think it has something to do with my servlet mapping or what?
<servlet>
<servlet-name>CreateNewEvent</servlet-name>
<servlet-class>com.Norbu.EventsPortal.ServletCreateEvent</servlet-class>
</servlet>
<servlet>
<servlet-name>DeleteEvent</servlet-name>
<servlet-class>com.Norbu.EventsPortal.ServletDeleteEvent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateNewEvent</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<sevlet-mapping>
<servlet-name>DeleteEvent</servlet-name>
<url-pattern>/del/*</url-pattern>
</sevlet-mapping>
<welcome-file-list>
<welcome-file>EventsPortal.jsp</welcome-file>
</welcome-file-list>
This is the delete link code that I'm using:
<a href="/del?eid=<%=eventid%>">
This is the warning message in the log file
No handlers matched this URL.
Is there anyone that could help me solving the issue I'm having?
Check spelling. You have sevlet-mapping

SpringMVC Map Servlet to Root without Removing Content Servlet

Currently, we have "root" (/) mapped to a static index.html page, but we want to upgrade to a jsp to have dynamic content. Trying to figure out how to do this. We have content that is mapped to the default content server (e.g. /css), so we don't want to change too much.
We tried:
Changing the .html to .jsp. This resulted in a blank page.
Changing the .html to .jsp and then moving the file into the WEB-INF directory. This resulted in a 404.
Trying to subclass the DefaultServlet class that content servlet is currently mapped to. This through a 500, with a class assertion error (it checked to see if it was the same class).
Adding another servlet to that url, but it overwrote the current one.
I've searched StackOverflow, but still haven't found an answer that works.
Thanks!
If I understand your question correctly, this is trivial using Spring MVC:
<mvc:default-servlet-handler/>
And in web.xml:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/your-applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Is this what you have tried already?
Just set up a controller method mapped to / that returns a view name, which is your jsp file. And make sure your view resolver is set up correctly. Any of the spring mac tutorial hellos world programs out there will show how.

Hide .jsp extension in JSP project using servlet mapping

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.

Categories