public class MyServlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>MyServlet1</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a " + request.getMethod() + ". This is the reply.</p>");
out.println("</body></html>");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
4.編譯servlet:
在c:/WebRoot/WEB-INF/下建立lib目錄,copy C:\Tomcat 4.1\common\lib\servlet.jar到lib目錄下面。
在c:/WebRoot/WEB-INF/下建立classes目錄。
在c:/WebRoot/下面建立complier.bat
內容如下:
javac -classpath ./WEB-INF/lib/servlet.jar; ./src/*.java -d ./WEB-INF/classes
pause
運行complier.bat
重新啟動tomcat,開啟瀏覽器在裡面敲入http://localhost:8080/WebRoot/myservlet1,執行,如果顯示
"The servlet has received a GET. This is the reply."表示執行成功.