ServletAnnotation is used to directly map to servlet program without the use of the web.xml. But these ServletAnnotation run in Tomcat7. Servlet Annotations will not run in previous versions. @WebServlet annotation is used to map servlet program. Name of jar for ServletAnnotation is Java. servlet-3.0.0. v201103241009. jar. Following are the different annotations are utilized in a servlet. The Java.servlet.annotation package will have more annotations that needs to be imported in the code.
The same thing can be done via servlet, as servlet can receive data as string, JSON etc either by POST or GET and in the same way it can return data as JSON, string, XML etc.User need libraries based upon the web service user use, such as SOAP or REST, servlet libraries are almost in-built in it.
package annotation; import java.io.IOException; import javax.servlet.*; import javax.servlet.annotation.*; @WebServlet(value="/hello", name="hello-servlet") public class Simple extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException { res.getWriter().println("Welcome to SPlessons"); } }
Where used @WebServlet annotation and used URL also. Following are the attributes accepted by the @WebServlet.
Attribute | Description |
---|---|
name | The servlet name. |
description | The servlet description. |
displayName | The servlet display name. |
smallIcon | A small icon image for the servlet. |
largeIcon | A large icon image for the servlet. |
initParams | @WebInitParam can be used to pass servlet configuration parameters, these three attribute are optional attributes. |
@WebServlet(value="/hello", name="hello-servlet")
res.getWrite() is used to get the response and that will be printed as follows.
res.getWriter().println("Welcome to SPlessons");
Output
Following is the result.