Methods | Description |
---|---|
init(ServletConfig config) | Initializes the servlet and the return type is void (not return any data type). |
Service(ServletRequest request, ServletResponse response) | Provides a server to the incoming request. Calls server every time to producresponse by using servlet and the return type is void. |
destroy() | Cleans up the servlets service and the return type is void. |
getServletConfig() | Gets the servlet config object and the return type is ServletConfig. |
getServletInfo() | Returns information about servlet such as writer, copyright, version. |
init() | It is a helpful technique for the servlet software engineers, now there is no compelling reason to call super.init(config). |
getServletContext() | Returns the ServletContext object. |
getInitParameter(String name) | Gives back the parameter value for the given parameter name. |
getInitParameterNames() | It returns parameter values which are declared in web.xml file. |
public Enumeration getInitParameterNames() | Returns all the parameters which are declared in web.xml file. |
getServletName() | Returns back the name of the servlet object and the return type is string . |
log(String msg) | Writes the class name and a message to the log and the return type is log. |
<html> <body> <form action="./go"> <input type="submit" value="submit here"/> </form> </body> </html>
Here the developer just used submit button that is submit here.
web.xml
<web-app> <servlet> <servlet-name>app</servlet-name> <servlet-class>ServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>app</servlet-name> <url-pattern>/go</url-pattern> </servlet-mapping> </web-app>
Make sure that URL name should be match with HTML form and should be also same, if the developer use any package name then package name also should be mentioned.
ServletDemo.java
package servletgenericservlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class ServletDemo extends GenericServlet { public void init() throws ServletException { // Put your code here } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.print("<html><body>"); out.print("<h1><b>welcome to Splessons</b></h1>"); out.print("<h2><b>example on GenericsServlets</b></h2>"); out.print("</body></html>"); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } }
The init() method is intended to be called just once. It is called when the servlet is initially made, and not called again for every client demand. Along these lines, it is utilized for one-time initializations, pretty much as with the init strategy for applets. The servlet is typically made when a client first conjures a URL relating to the servlet, however you can likewise indicate that the servlet be stacked when the server is initially begun.
The destroy() method is called just once toward the end of the life cycle of a servlet. This technique allows servlet to close database connections and it makes the object garbage collected.
Output
After compiling the above code following page will be displayed where click on the button.
When click on button following message will be displayed.