/* * Created on 2005-8-29 * * TODO To change the template for this generated file go to * Window - PReferences - Java - Code Style - Code Templates */ package zy.pro.wd.servlet;
/** * @author zhangyi * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class ServletDemo extends HttpServlet {
String message; DataSource ds; /** * ConstrUCtor of the object. */ public ServletDemo() { super(); }
/** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here }
/** * 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 {
response.setContentType("text/Html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method<br>");
/** * 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 init() throws ServletException { // Put your code here }
}
在此Servlet中我们定义了两个属性message和ds。我们现在在web.xml中作如下配置: <servlet> <description> This is the description of my J2EE component </description> <display-name> This is the display name of my J2EE component </display-name> <servlet-name>ServletDemo</servlet-name> <servlet-class>zy.pro.wd.servlet.ServletDemo</servlet-class>
<init-param> <description>initialize the field of message</description> <param-name>message</param-name> <param-value> welcome here ,thank you for visiting !!! </param-value> </init-param>