0

I'm only a beginner but I have gone through one beginners Java training course where we also covered some aspects of REST so I'm starting to understand things a little bit better. I would really like to understand more about servlets and how web.xml is used to set them up. The below is a web.xml that I was using on my project while I was on this training course.

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<servlet>
    <servlet-name>jersey-servlet</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>org.arpit.java2blog.controller</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Righ now I can only understand some of it. I can understand some of what <servlet-name>, <servlet-class>, <servlet-mapping> and <url-pattern>do.

But I'm really struggling to understand what <init-param>, <param-name> and <param-value> in generally do. I've been trying to google to find some information that would explain these in very simplistic manner but I have not had much joy.

I really want to understand what these two sections below do:

`<init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>org.arpit.java2blog.controller</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>`

I can understand bit of what the first section relates to because my controller classes are in package <param-value>org.arpit.java2blog.controller</param-value>. But what does and why is the param-name as com.sun.jersey.config.property? I cannot find any package with this name.

I understand the the second init=param section has something to do with the jersey-json jars and that's about it.

I would be genuinely grateful if someone could explain some of these things in a very simplistic English if possible (English is not our native language in Estonia). I'm working really hard to become a better in programming and I could use bit of help.

Kind regards.

Rain
  • 63
  • 1
  • 10
  • It's setting up [jersey](https://jersey.java.net/). – Elliott Frisch Mar 22 '17 at 21:36
  • Are you clear with the generic use of these in a web.xml or is it this specific case your are facing issue with? – Shivam Aggarwal Mar 24 '17 at 09:50
  • @ShivamAggarwal - Thank you. The answer is bit of both. I'm having trouble of setting up a project with REST, Maven and Tomcat. So I thought I should try to learn more about it but I'm struggling really to understand how POM and web.xml work. I had some help setting up things up with this project before when I was using Eclipse. Now I'm trying to set it up on IntelliJ but now I'm on my own. I created a new post with this specific problem at http://stackoverflow.com/questions/43031725/cannot-get-rest-response-with-postman-java-intellij-tomcat-9-maven – Rain Mar 26 '17 at 17:00

1 Answers1

0

Those are the initialization parameters for the servlet class:

com.sun.jersey.spi.container.servlet.ServletContainer.

If you take a look source code here:

...If the initialization parameter "com.sun.jersey.config.property.resourceConfigClass" or "javax.ws.rs.Application" is not present and a initialization parameter "com.sun.jersey.config.property.packages" is present (see com.sun.jersey.api.core.PackagesResourceConfig.PROPERTY_PACKAGES) a new instance of com.sun.jersey.api.core.PackagesResourceConfig is created. The initialization parameter "com.sun.jersey.config.property.packages" MUST be set to provide one or more package names. Each package name MUST be separated by ';'. The package names are added as a property value to a Map instance using the property name (@link PackagesResourceConfig#PROPERTY_PACKAGES}. Any additional initialization parameters are then added to the Map instance. Then that Map instance is passed to the constructor of com.sun.jersey.api.core.PackagesResourceConfig.

And the second one for PojoMappingFuture

Community
  • 1
  • 1
HRgiger
  • 2,750
  • 26
  • 37