welcome.html
[html]
<html>
<body>
<form action="./go">
<input type="submit" value="submit here"/>
</form>
</body>
</html>
[/html]
Here the developer just used submit button that is
submit here.
web.xml
[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>
[/xml]
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
[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
}
}
[/java]
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.