How to configure welcome file (HTML/JSP) in Jersey container
Asked Answered
D

8

9

I have a Jersey RESTful web service project. I have configured the Jersey container in the web.xml and everything works fine.

In the same project, I have introduced one HTML page and included in the <welcome-file-list> to handle some other non-REST request. But when I am accessing the URL, the welcome file is not displayed.

After I commented the Jersey container configuration in web.xml and deployed the application, this time I am able to access the welcome file.

Am using Tomcat 7, JDK 7, Jersey 2.2 and Eclipse Juno. How to make the welcome file working when Jersey has configured? Is there any limitations with Jersey or do I need configure in different way to achieve this?

My web.xml:

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com /xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>My Service</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</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.my.rest.service</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>    
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
 </servlet-mapping>      
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file> 
  </welcome-file-list>
</web-app>
Deafanddumb answered 9/10, 2013 at 16:42 Comment(3)
Have you tried adding welcome-file-list tag to web.xml. It should work. Something like below.. <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>Scuta
Please also share your web.xml configurationBlancablanch
Posted web.xml, I have added the welcome file in the list also.Deafanddumb
S
10
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>com.webservice.services</display-name>
    <servlet>
        <servlet-name>Jersey REST 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.webservice.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>


Try URL pattern with different path like given above (/service/*) for REST. It works and welcome file displays when server starts.

Silassilastic answered 10/10, 2013 at 6:55 Comment(1)
Yes, I have changed that. All the REST service request will be handled in the path http://<domain:port>/<context root>/service and the welcome file will be accessed in normal way. It is working fine. Keeping /* in the REST container makes the welcome page as not available resource.And all the requests will go to Jersey container.Deafanddumb
E
7

your current servlet mapping is

<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>

which redirects every request to jersey. so to make welcome page visible you need to make entry like

<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest</url-pattern>

this pattern will call jersey only for urls like

http://localhost:8080/rest/

and thus url

http://localhost:8080/index.html

will not be redirected to jersey servlet.

A project targetting same scenario is hosted on https://github.com/skohli0302/jims

Enwind answered 10/10, 2013 at 8:33 Comment(0)
P
3

In web.xml:

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/somethinghere/*</url-pattern>
</servlet-mapping>

instead of

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
Poindexter answered 12/10, 2014 at 1:46 Comment(0)
C
1

You can have something like

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/index.html</url-pattern>
</servlet-mapping>
Cassiterite answered 18/12, 2016 at 20:45 Comment(0)
V
0

When you use jersey, all the requests are directed to jersey servlet i.e. ServletContainer. So if any request that does not match to any mapped rest class, it throws 404. But you can always add servlet filters to intercept the incoming request. Depending on the incoming HTTP request URL(defualt/welcome etc), you can take a decision to redirect it to the weclome page:

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("/welcome.jsp");
Vullo answered 9/10, 2013 at 16:47 Comment(0)
R
0

I am just curious to know, will the below example work?

HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.sendRedirect("/welcome.jsp"); if it will, where this sendRedirect() to be called? with in a servlet, so if i am not wrong, there should be a servlet, which just redirects the request to the first/default page, right?

Reposeful answered 10/10, 2013 at 4:55 Comment(0)
A
0

you can create the class "API", and insert an anotation in your project. Class ApplicationConfig...

@ApplicationPath("api") //anotation

public class ApplicationConfig extends Application { }

after, i create class "Users" that stay...

Class UserApi

@Path("users")//anotation page User.

public class UserApi {

.... mycode complement page....

@GET

@Path("list")

@Produces("application/json")

public String getUsuarios() throws Exception {

    String json = new Gson().toJson(this.userD.listar());

    return (json);

}

remember that your root project stay ... http://yourprojectpatc.com.br/api/users/list

using the "Postman" modify for Json to to send your data

Absorption answered 21/7, 2018 at 15:16 Comment(0)
K
0
<servlet>
    <servlet-name>Jersey REST 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.webservice.services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
Kenweigh answered 15/10, 2019 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.