首先在myeclipse中建立一个web工程,在底下建一个包,然后写servlet

package com.jspdev.ch7;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class HelloWorldServlet extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse resp) 

throws ServletException, IOException

{

resp.setCharacterEncoding("UTF-8");

resp.setContentType("text/html;charset=UTF-8");

PrintWriter out=resp.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>helloworld</title>");

out.println("</head>");

out.println("<body bgcolor=\"white\">");

out.println("<hr>");

out.println("helloworld");

out.println("你好 世界");

out.println("</body></html>");

}

public void doPost(HttpServletRequest req, HttpServletResponse resp) 

throws ServletException, IOException

{

doGet(req, resp);

}

 

 

}

然后在工程的web.xml中进行配置

<servlet>

<servlet-name>HelloWorldServlet</servlet-name>

<servlet-class>com.jspdev.ch7.HelloWorldServlet</servlet-class>

  </servlet>

  <servlet-mapping>

<servlet-name>HelloWorldServlet</servlet-name>

<url-pattern>/hello</url-pattern>

  </servlet-mapping>

 

最后在浏览器中运行servlet

输入http://localhost:8080/jsp-demo/hello

就OK了!!!