标签:
1、文件>新建>动态WEB项目 "javaeedemo">在Java Resource的src下新建包“servletdemo”,包下新建一个类“MyServet”,另外在WebContent>WEB_INF下新建一个XML文件,这个是servlet的配置文件,稍后会用到
2、MyServet类必须继承HttpServlet,示例代码如下
package servletdemo; 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 MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO 自动生成的方法存根 //super.doGet(req, resp); //HttpServletRequest 请求 //HttpServletResponse 响应 PrintWriter pw=resp.getWriter(); pw.print("第一个servlet程序!"); pw.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO 自动生成的方法存根 //super.doPost(req, resp); doGet(req, resp); //通常POST请求会在这里调用上面的doGet()方法 } }
3、配置XML文件
标签:
原文地址:http://www.cnblogs.com/SamFlynn/p/4474889.html