直接上图,不废话!!!
第一:首先在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 FirstServlet14 */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 stub25 }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 stub32 response.setContentType("text/html");33 response.setCharacterEncoding("GBK");34 PrintWriter out=response.getWriter();35 out.print("");36 out.println("Servlet实例 ");37 out.println("");38 out.println("servlet实例");39 out.println(this.getClass());40 out.println("");41 out.println("");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 stub52 doGet(request, response);53 }54 55 }
1 23 web02 45 12index.html 6index.htm 7index.jsp 8default.html 9default.htm 10default.jsp 1113 14 19FirstServlet 15 16com.ningmeng.FirstServlet 17 1820 21 26FirstServlet 22 23/FirsetServlet 24 25
运行效果如下图所示