直接上圖,不廢話!!!
第一:首先在Eclipse的包資源管理器中,單機滑鼠右鍵,在彈出的快捷鍵選單中選擇“新建”/Servlet命令,在彈出的對話方塊中輸入新建的Servlet所在的包和類名,然後單擊下一步,
我選擇預設,Next
依舊預設,Next
注意事項,在建立web的工程的時候需要注意的是如下圖,選擇上web.xml這個按鈕,因為Servlet需要配置一下web.xml這個檔案,web.xml在web-inf這個檔案下面
上圖第二個箭頭前面有個方括號,勾選上就可以自動建立好web.xml這個檔案了。
1 package com.ningmeng; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * Servlet implementation class FirstServlet 14 */ 15 @WebServlet("/FirstServlet") 16 public class FirstServlet extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 /** 20 * @see HttpServlet#HttpServlet() 21 */ 22 public FirstServlet() { 23 super(); 24 // TODO Auto-generated constructor stub 25 } 26 27 /** 28 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 29 */ 30 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 // TODO Auto-generated method stub 32 response.setContentType("text/html"); 33 response.setCharacterEncoding("GBK"); 34 PrintWriter out=response.getWriter(); 35 out.print("<HTML>"); 36 out.println("<HEAD><TITLE>Servlet例項</TITLE></HEAD>"); 37 out.println("<BODY>"); 38 out.println("servlet例項"); 39 out.println(this.getClass()); 40 out.println("</BODY>"); 41 out.println("</HTML>"); 42 out.flush(); 43 out.close(); 44 response.getWriter().append("Served at: ").append(request.getContextPath()); 45 } 46 47 /** 48 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 49 */ 50 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 51 // TODO Auto-generated method stub 52 doGet(request, response); 53 } 54 55 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>web02</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <servlet> 13 <!-- 宣告Servlet物件 --> 14 <servlet-name>FirstServlet</servlet-name> 15 <!-- 上面一句指定Servlet物件的名稱 --> 16 <servlet-class>com.ningmeng.FirstServlet</servlet-class> 17 <!-- 上面一句指定Servlet物件的完整位置,包含包名和類名 --> 18 </servlet> 19 <servlet-mapping> 20 <!-- 對映Servlet --> 21 <servlet-name>FirstServlet</servlet-name> 22 <!--<servlet-name>與上面<Servlet>標籤的<servlet-name>元素相對應,不可以隨便起名 --> 23 <url-pattern>/FirsetServlet</url-pattern> 24 <!-- 上面一句話用於對映訪問URL --> 25 </servlet-mapping> 26 </web-app>
執行效果如下圖所示