public class Demo3 extends HttpServlet { private static final String DEFAULT_USER="Guest"; /** * Constructor of the object. */ public Demo3() { super(); }
/** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here System.out.println(this.getServletInfo()); System.out.println("fin");
}
/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user =request.getParameter("user"); if(user==null) { user=Demo3.DEFAULT_USER; } response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.append("<!DOCTYPE HTML>\r\n") .append("<HTML>\r\n") .append(" <HEAD>\r\n") .append(" <TITLE>A Servlet</TITLE>\r\n") .append(" </HEAD>\r\n") .append(" <BODY>\r\n") .append(" Hello, ").append(user).append("!<br/><br/>\r\n") .append(" <form action=\"demo3\" method=\"POST\">\r\n") .append(" Enter your name:<br/>\r\n") .append(" <input type=\"text\" name=\"user\"/><br/>\r\n") .append(" <input type=\"submit\" value=\"Submit\"/>\r\n") .append(" </form>\r\n") .append(" </BODY>\r\n") .append("</HTML>\r\n");
}
/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }
/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here System.out.println("start"); System.out.println(this.getServletName()); }