第一个Servlet程序

HelloServlet.java

package im.weitang;

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 HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 注意要添加下面两行代码,否则可能会出现中文乱码问题 
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");
        out.println("");
        out.println("");
        out.println("");
        out.println("

这是我第一个Servlet程序

"); out.println(""); out.println(""); out.close(); } }

修改web.xml,在最后添加下面这段代码


    hello
    im.weitang.HelloServlet


    hello
    /helloServlet

打开浏览器,在地址栏中输入http://127.0.0.1/helloServlet, 即可查看到结果。

你可能感兴趣的:(第一个Servlet程序)