程序
package com.zzk.cn; 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 HelloWorldServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L;//父类或者父类的父类实现了序列化的接口 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out=resp.getWriter(); out.println("<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>"); out.flush();//清理 out.close();//关闭 } }
编译自动在COM.ZZK.CN文件夹下生成CLASS
WEB配置文件是这么写的
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <servlet> <servlet-name>Myfirstservlet</servlet-name> <servlet-class>com.zzk.cn.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Myfirstservlet</servlet-name> <url-pattern>/HelloWorldServlet</url-pattern> </servlet-mapping> </web-app>
页面展示
HelloWorldServlet!
页面打开的源文件
<html><head></head><body><center><font color='red'>HelloWorldServlet!</font></center></body></html>