code059.java
package pack01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class code059 extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter writer1 = response.getWriter();
writer1.println("<html>");
writer1.println("<head><title>Servlet实例</title></head>");
writer1.println("<body>");
writer1.print("Servlet实例:");
writer1.println(this.getClass());
writer1.println("</body>");
writer1.println("</html>");
writer1.flush(); //缓存立即执行
writer1.close();
}
//运行此类,必须在tomcat服务器的web.xml文件中配置servlet项
/*
* 配置代码举例
*
*<servlet>
* <servlet-name>code059</servlet-name>
* <servlet-class>pack01.code059</servlet-class> //类在哪个包
*</servlet>
*<servlet-mapping>
* <servlet-name>code059</servlet-name> //必须与上方的一致
* <url-pattern>/servlet/pack01.code059</url-pattern> // 浏览器访问路径
*</servlet-mapping>
*</web-app>
*/
}