3

i want to know what should i put inside Servletname tag and url path in servlet-mapping in web.xml in eclipse.i tried googling it but i didnt find answer. Please suggest me in this, i am creating rest services in Java.

 <servlet>
<servlet-name>FirstWebserviceApplication</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.restservice.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FirstWebserviceApplication</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
allDroid
  • 405
  • 1
  • 10
  • 21

2 Answers2

3

Element : servlet-name The servlet-name element contains the canonical name of the servlet. Each servlet name is unique within the web application. Used in: filter-mapping, servlet, servlet-mapping

Element : url-pattern The url-pattern element contains the url pattern of the mapping. This is how you want your servlet class to be accessed. if you put /hello then your servlet will invoke when user access http://yourdomain/yourappname/hello

Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33
  • http://javapapers.com/java/java-restful-web-services-with-json-and-jersey/ i have followed this link to create rest services ,so i dont know much about this as this is my first work in eclipse.where could i find servlet-name and url-pattern ?is it just hard coding or where could i find it – allDroid Dec 18 '15 at 10:26
0

You define servlets as a part of a Web application in several entries in the J2EE standard Web Application deployment descriptor, web.xml. The web.xml file is located in the WEB-INF directory of your Web application.

The first entry, under the root servlet element in web.xml, defines a name for the servlet and specifies the compiled class that executes the servlet. (Or, instead of specifying a servlet class, you can specify a JSP.) The servlet element also contains definitions for initialization attributes and security roles for the servlet.

The second entry in web.xml, under the servlet-mapping element, defines the URL pattern that calls this servlet.

NOTE : - You can give servlet a name as per your convenience and naming convention, you need only to make sure servlet name is unique within the web application

to answer your question if you are creating REST servies using jersey put web.xml contents as below

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JSON RESTful Services</display-name>
 <servlet>
    <servlet-name>JSON RESTful 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.javapapers.webservices.rest.jersey</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JSON RESTful Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

you can also check this post http://www.tutorialsdesk.com/2014/09/jersey-restful-webservices-tutorial.html Jersey RESTful Webservices example

Prakash Hari Sharma
  • 1,414
  • 2
  • 17
  • 22
  • http://javapapers.com/java/java-restful-web-services-with-json-and-jersey/ i have followed this link to create rest services ,so i dont know much about this as this is my first work in eclipse.where could i find servlet-name and url-pattern – allDroid Dec 18 '15 at 10:23
  • @allDroid The web.xml file is located in the WEB-INF directory of your Web application. – Prakash Hari Sharma Dec 18 '15 at 10:29
  • yes i have found web.xml page but i dont know what to put in servlet-name and url-pattern.Where should i find them ?or is it hard coding? – allDroid Dec 18 '15 at 10:33
  • in above URL contents of web.xml are also given serach for web.xml – Prakash Hari Sharma Dec 18 '15 at 10:36
  • can u give me a example for this i didnt get what u explained – allDroid Dec 18 '15 at 10:38