<!-----webXML配置servlet----->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>HeadFirstJspServletChap05</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>helloWorldServlet</servlet-name> <servlet-class>com.java.web.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloWorldServlet</servlet-name> <url-pattern>/helloWorld</url-pattern> </servlet-mapping></web-app>
//向页面输出
package com.java.web;
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 request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); this.doPost(request, response); }@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("gbk"); PrintWriter out=response.getWriter(); out.println("<html>"); out.println("<head><title>初识servlet</title></head>"); out.println("初识servlet!"); out.println("</html>"); out.close(); }}