In this tutorial we are going to see about GenericServlet with example program.
A Servlet is just normal class which implements the Interface Servlet
The following classes implements the Servlet Interface.
GenericServlet defines a generic, protocol-independent Servlet.We can create protocol independent Servlet by extends GenericServlet .To write an HTTP Servlet for use on the Web, extend HttpServlet instead.
GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a Servlet , although it's more common to extend a protocol-specific subclass such as HttpServlet.
GenericServlet makes writing Servlet easier. It provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface. GenericServlet also implements the log method, declared in the ServletContext interface.
To write a generic servlet , you need only override the abstract service method.
Now see how to create GenericServlet with example.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called GenericServletExample as shown in figure. (By default super class in eclipse servlet creation is HttpServlet, change it into javax.servlet.GenericServlet)
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available in GenericServlet. Select appropriate method you need. service() method is mandatory one.
7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml
Servlet Mapping
web.xml
8. Add the required code inside service() method.
GenericServletExample.Java
9. Now save and Run the servlet , Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse)
10.call the URL which is mapped in web.xml.
Output

A Servlet is just normal class which implements the Interface Servlet
1.
javax.servlet.Servlet
The following classes implements the Servlet Interface.
1.
import
javax.servlet.GenericServlet;
2.
import
javax.servlet.http.HttpServlet;
GenericServlet defines a generic, protocol-independent Servlet.We can create protocol independent Servlet by extends GenericServlet .To write an HTTP Servlet for use on the Web, extend HttpServlet instead.
GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a Servlet , although it's more common to extend a protocol-specific subclass such as HttpServlet.
GenericServlet makes writing Servlet easier. It provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface. GenericServlet also implements the log method, declared in the ServletContext interface.
To write a generic servlet , you need only override the abstract service method.
Now see how to create GenericServlet with example.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.

3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called GenericServletExample as shown in figure. (By default super class in eclipse servlet creation is HttpServlet, change it into javax.servlet.GenericServlet)


5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available in GenericServlet. Select appropriate method you need. service() method is mandatory one.

7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml
Servlet Mapping
1.
<
servlet
>
2.
<
servlet-name
>Your Servlet Name</
servlet-name
>
3.
<
servlet-class
>Fully Qulaified Servlet Class Name</
servlet-class
>
4.
</
servlet
>
5.
<
servlet-mapping
>
6.
<
servlet-name
>Your Servlet Name</
servlet-name
>
7.
<
url-pattern
>/URL to Call Servlet</
url-pattern
>
8.
</
servlet-mapping
>
web.xml
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
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_3_0.xsd"
id
=
"WebApp_ID"
version
=
"3.0"
>
03.
<
display-name
>ServetExample</
display-name
>
04.
<
welcome-file-list
>
05.
<
welcome-file
>index.html</
welcome-file
>
06.
<
welcome-file
>index.htm</
welcome-file
>
07.
<
welcome-file
>index.jsp</
welcome-file
>
08.
<
welcome-file
>default.html</
welcome-file
>
09.
<
welcome-file
>default.htm</
welcome-file
>
10.
<
welcome-file
>default.jsp</
welcome-file
>
11.
</
welcome-file-list
>
12.
<
servlet
>
13.
<
servlet-name
>GenericServletExample</
servlet-name
>
14.
<
servlet-class
>com.javatutorialscorner.servlet.GenericServletExample</
servlet-class
>
15.
</
servlet
>
16.
<
servlet-mapping
>
17.
<
servlet-name
>GenericServletExample</
servlet-name
>
18.
<
url-pattern
>/GenericServletExample</
url-pattern
>
19.
</
servlet-mapping
>
20.
21.
</
web-app
>
8. Add the required code inside service() method.
GenericServletExample.Java
01.
package
com.javatutorialscorner.servlet;
02.
03.
import
java.io.IOException;
04.
import
java.io.PrintWriter;
05.
06.
import
javax.servlet.GenericServlet;
07.
import
javax.servlet.ServletConfig;
08.
import
javax.servlet.ServletException;
09.
import
javax.servlet.ServletRequest;
10.
import
javax.servlet.ServletResponse;
11.
import
javax.servlet.annotation.WebServlet;
12.
13.
/**
14.
* Servlet implementation class GenericServletExample
15.
*/
16.
17.
public
class
GenericServletExample
extends
GenericServlet {
18.
private
static
final
long
serialVersionUID = 1L;
19.
private
String msg;
20.
21.
/**
22.
* @see GenericServlet#GenericServlet()
23.
*/
24.
public
GenericServletExample() {
25.
super
();
26.
// TODO Auto-generated constructor stub
27.
}
28.
29.
/**
30.
* @see Servlet#init(ServletConfig)
31.
*/
32.
public
void
init(ServletConfig config)
throws
ServletException {
33.
// TODO Auto-generated method stub
34.
msg =
"Java Tutorials Corner Hello World Servlet Program"
;
35.
}
36.
37.
/**
38.
* @see Servlet#destroy()
39.
*/
40.
public
void
destroy() {
41.
// TODO Auto-generated method stub
42.
}
43.
44.
/**
45.
* @see Servlet#getServletConfig()
46.
*/
47.
public
ServletConfig getServletConfig() {
48.
// TODO Auto-generated method stub
49.
return
null
;
50.
}
51.
52.
/**
53.
* @see Servlet#getServletInfo()
54.
*/
55.
public
String getServletInfo() {
56.
// TODO Auto-generated method stub
57.
return
null
;
58.
}
59.
60.
/**
61.
* @see Servlet#service(ServletRequest request, ServletResponse response)
62.
*/
63.
public
void
service(ServletRequest request, ServletResponse response)
64.
throws
ServletException, IOException {
65.
// TODO Auto-generated method stub
66.
response.setContentType(
"text/html"
);
67.
PrintWriter writer = response.getWriter();
68.
writer.write(
"<html><body><h2>"
+ msg +
"</h2></body></html>"
);
69.
}
70.
71.
}
9. Now save and Run the servlet , Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse)
10.call the URL which is mapped in web.xml.
Output

0 comments:
Post a Comment