How does Java servlet handle a URL that ends with '/'? - java

Usually, these two requests:
localhost:8080/test
localhost:8080/test/
have no difference.
However, when I add a servlet-mapping config:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
then
localhost:8080/test/
Does not work, it would return 404 error, I don't understand that I had added a config like this:
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/rest-servlet.xml,
/WEB-INF/interceptor-servlet.xml,
/WEB-INF/controller-servlet.xml,
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
So, why would the request not go to this servlet, what is the magic in *.html? The others like *.txt, *.jpeg would not cause this problem.

remove
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
leave
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and let Spring distinguish between rest and html requests.

Related

Servlet mapping for specific URL not working in Jersey 2

I am trying to use Jersey 2.x and have a servlet call "myapp", configuration on web.xml is as follows:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.private.myapp.resource
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
and have a servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/instance/create</url-pattern>
<url-pattern>/instance/list</url-pattern>
</servlet-mapping>
when I request to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list its return 404
but when I change servlet mapping as follows
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
then requesting to $SERVER_ROOT/instance/create or $SERVER_ROOT/instance/list response as expected
can anyone tell what I am missing?

allow login page without authentication in web.xml

I am trying to make the login.jsp page public without authentication.
here is my web.xml file :
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class
</servlet>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
what should I add?

How to change url pattern for index.jsp

How can I change the URL pattern for index.jsp. I want to change the URL http://localhost:8084/eclinical/index.jsp to http://localhost:8084/eclinical/home. Below is the web.xml file:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>home</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>about</servlet-name>
<jsp-file>/about.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>about</servlet-name>
<url-pattern>/about</url-pattern>
</servlet-mapping>
You can register a jsp as a servlet using the jsp-file tag in the servlet definition. You can thus provide a path mapping to your jsp in web.xml
<servlet>
<servlet-name>myIndexJsp</servlet-name>
<jsp-file>index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>myIndexJsp</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>

weblogic server not serving html, js and css files

I have deployed a sample spring java application to weblogic server (11g, 10.3.6). I have index.html at the root and I set that as the welcome-file in web.xml. But when I try to access the application, I am getting 'Error 404 -- Not Found'. Also, I noticed same issue with js and css files.
index.jsp works fine at the same location.
Here is my web.xml.
<display-name>hello</display-name>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
The reason for this is the root level mapping(/) for your Spring DispatcherServlet. All the requests are forward to Spring servlet:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I workaround this situation by using *.do for my Spring controllers and update the servlet mapping as mentioned here:
<servlet-mapping>
<servlet-name>appServlet/servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
But this will require you to update the 'calls to your Spring controller' with a .do at the end of the URLs.

GAE 404 NOT_FOUND error

i am trying to deploy my app to GAE but i allways get 404 not found error on all pages(jsp) except for default. RESTful webservices working without problem. I am using netbeans 7.1.1 and gae 1.6.4.1
When I test it locally, everything works fine without any problem. But when I upload it do gae, allways only default page is available. I tryied almost everything.
I tryied to check whether the files are on GAE using appcfg.cmd. I downloaded my files from GAE and found out that noone is missing so the update process was probably successful.
I tryied to upload it with appcfg script instead of netbeans plugin but the process failed because of:
com.google.appengine.tools.admin.JspCompilationException: Failed to compile jsp files.
I cant simply make it working using appcfg script, searech internet for almost whole day and found no possible solution. Uploading with netbeans plugin worked without problem.
So what can be the reason to allways throw 404 on gae? Please help.
Thx very much. If you need any other info, i will be glad to post it here.
Ok,Here is my web.xml
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>facebookLogin</servlet-name>
<servlet-class>Login.FacebookLogin</servlet-class>
</servlet>
<servlet>
<servlet-name>PlanProcess</servlet-name>
<servlet-class>Input.PlanProcess</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>facebookLogin</servlet-name>
<url-pattern>/facebooklogin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>private</servlet-name>
<jsp-file>private.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>private</servlet-name>
<url-pattern>/private</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>editTrainingPlan.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>editPlan</servlet-name>
<url-pattern>/private/trainingplan/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>listplans</servlet-name>
<jsp-file>listTrainingPlans.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>listplans</servlet-name>
<url-pattern>/private/listplans</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>visualize</servlet-name>
<jsp-file>visualize.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>visualize</servlet-name>
<url-pattern>/private/visualize</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>graph</servlet-name>
<jsp-file>graph.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>graph</servlet-name>
<url-pattern>/graph/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>created</servlet-name>
<jsp-file>created.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>created</servlet-name>
<url-pattern>/created</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newRecord</servlet-name>
<jsp-file>newRecord.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newRecord</servlet-name>
<url-pattern>/private/newrecord</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newPlan</servlet-name>
<jsp-file>newPlan.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newPlan</servlet-name>
<url-pattern>/private/newplan</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newExcercise</servlet-name>
<jsp-file>newExcercise.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newExcercise</servlet-name>
<url-pattern>/private/newexcercise</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>Webservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test.InsertToDb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/testinsert</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>recordProcess</servlet-name>
<servlet-class>Input.RecordProcess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>recordProcess</servlet-name>
<url-pattern>/record_process</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>excerciseProcess</servlet-name>
<servlet-class>Input.ExcerciseProcess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>excerciseProcess</servlet-name>
<url-pattern>/excercise_process</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PlanProcess</servlet-name>
<url-pattern>/plan_process</url-pattern>
</servlet-mapping>
After hours of debugging and searching, i found out, that if you want to map jsp files
you have to use
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>**/**xxx.jsp</jsp-file>
</servlet>
instead of
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>xxx.jsp</jsp-file>
</servlet>
The '/' in path is essential, it will work on loaclhost but not deployed on gae. What a crap...
I never mapped JSPs to anything in web.xml. Why do you do that?

Categories