I'm trying to serve static content (a HTML form that calls a Jersey REST resource) from the same webapp as the servlet that handles the requests to the resource. As I understand I can filter requests to static content away from the Jersey servlet. My web.xml is as follows, but at the moment I am unable to access the static content nor the resource...both were working separately.
<filter>
<filter-name>my-filter</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/*.html</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>my-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>my-service</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>com.mydomain.ws.myservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my-service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
FWIW your original problem was probably because the param-value for the WebContentRegex was not a regular expression. Ok techincally it was, but it is not matching what you probably want. You should try something like /.*.html instead.
I setup my services such that the rest service are under their own subpath separate from the static content:
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Related
I want to add swagger to an existing servlet project and I have done my web.xml as shown
<servlet>
<servlet-name>DefaultServletConfig</servlet-name>
<servlet-class>io.swagger.servlet.config.DefaultServletConfig</servlet-class>
<init-param>
<param-name>swagger.resource.package</param-name>
<param-value>bsnl.inbound</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/comm</param-value>
</init-param>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>ApiDeclarationServlet</servlet-name>
<servlet-class>io.swagger.servlet.listing.ApiDeclarationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ApiDeclarationServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
It is working for only one class in a package and only for the GET method, it is not calling the post method. But I want to configure for multiple packages.
If anyone having any idea, please help me.
This is web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<web-app>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>servlets.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/reg/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>servlets.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/reg/signup</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dashboard</servlet-name>
<servlet-class>servlets.Dashboard</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dashboard</servlet-name>
<url-pattern>/user/dashboard</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>servlets.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/reg/logout</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>edit</servlet-name>
<servlet-class>servlets.EditProfile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>edit</servlet-name>
<url-pattern>/user/controlpanel/edit</url-pattern>
</servlet-mapping>
<!--####################################################-->
<filter>
<filter-name>regAuth</filter-name>
<filter-class>filters.RegPathAuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>regAuth</filter-name>
<url-pattern>/myWebApp/reg/login.html</url-pattern>
<url-pattern>/myWebApp/reg/signup.html</url-pattern>
</filter-mapping>
<filter>
<filter-name>regAccess</filter-name>
<filter-class>filters.RegPathAccessFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>regAccess</filter-name>
<url-pattern>/myWebApp/reg/login</url-pattern>
<url-pattern>/myWebApp/reg/signup</url-pattern>
</filter-mapping>
<filter>
<filter-name>userAuth</filter-name>
<filter-class>filters.UserPathAuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>userAuth</filter-name>
<url-pattern>/myWebApp/user/*</url-pattern>
<url-pattern>/myWebApp/user/controlpanel/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>userAccess</filter-name>
<filter-class>filters.UserPathAccessFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>userAccess</filter-name>
<url-pattern>/myWebApp/user/dashboard.html</url-pattern>
<url-pattern>/myWebApp/user/controlpanel/edit.html</url-pattern>
</filter-mapping>
</web-app>
I have checked everything and debugged the project several times and haven't figured out the problem. the init method of all 4 filters will be invoked but when a request is sent, the doFilter method won't be invoked. I can't find any clue what's the problem.
So, How can I fix this problem? Where am I doing wrong?
If you need filters implementation, let me know.
Regards
i have tried without giving servlet-name tag in my web.XML but it did not worked for me.
we are adding servlet-class & url tags in deployment descriptor file here what is the exact use of adding servlet-name tag in deployment descriptor file (web.XML).
Cant we run without servlet-name tag in our web.xml
The <servlet-name> is a unique identifier for a single Servlet. Some deployment descriptor elements some times need to refer to a specific servlet. They do that through the value of the <servlet-name>. For example, you might have multiple <servlet-mapping> elements, one for each extension. You might also have a Filter which you want applied only to a specific servlet.
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.pack.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.pack.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<servlet-name>myServlet</servlet-name>
</filter-mapping>
What is the difference between the two URL mappings : /* and / ?
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
What I understood after reading the specs is that :
The pattern /* will force everything through MyServlet.
The pattern / will make DefaultServlet as the default servlet in the app .
Both almost means the same to me . Please let me know if there is any specific difference ?
Thanks for the links , going through them I have compiled this answer . Let us see a sample web.xml :
Case 1:
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
`
In this case all requests /context/ , /context/anything and /context/sample.do will invoke servlet2 .
Case 2:
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
`
In this case requests like /context/, /context/anything invokes servlet2 and /context/sample.do will invoke servlet1.
I am new to Restlets. Trying to configure the web.xml (on JBoss). I have 2 entries, one for a servlet (got nothing to do with webservices) other for webservices, using Restlet. Here are the entries..
<servlet>
<servlet-name>AuthenticationServlet</servlet-name>
<servlet-class>com.safeid.web.server.api.servlet.AuthenticationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AuthenticationServlet</servlet-name>
<url-pattern>/authenticate/*</url-pattern>
</servlet-mapping>
<!-- Start of Entries for the REST Web Services. -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>com.safeid.web.server.SafeIDRouterApplication</param-value>
</context-param>
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>com.noelios.restlet.ext.servlet.ServerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- END of Entries for the REST Web Services.-->
Both don't work together. In the above setup the Restlet works. However when I change the
RestletServlet
/*
to something like
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/credential/*</url-pattern>
</servlet-mapping>
the Restlet stop working and the AuthenticationServlet works fine. What am I missing here?
I had a similar frustration. Perhaps what I found out may help.
I had Router entries in my Application class like this:
router.attach("/users", UsersResource.class);
And things worked fine when my servlet mapping was like this:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
When I changed it to something like this:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/users/*</url-pattern>
</servlet-mapping>
it stopped working.
The problem is that the servlet container "consumes" or removes the part of the URL that it matched. In this case, it removes "/users". So if you were using a url like this:
http://www.mywebsite.com/users
you would have to change it to be:
http://www.mywebsite.com/users/users
Of course, you can make the url-pattern be whatever you want:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
and then you'd access it like this:
http://www.mywebsite.com/rest/users
The url-pattern gets stripped off, and you get whatever is leftover in your Application class for your own routing purposes.
HTH
Looks like you're missing the init-params as in the example below.
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>my.class.that.extends.Application.MyApplication</param-value>
</init-param>
</servlet>
You need a class that extends org.restlet.Application (at least in Restlet 2.0 anyway).