JSP学习笔记(二十三):我的第一个servlet页面
servlet需要继承javax.servlet.http.HttpServlet类,添加一个servlet类 T1.java
package com.abc.test;
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 T1 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("Hello World!");
}
}
修改web.xml文件,添加内容如下,内容添加到<web-app></web-app>之间
<servlet>
<servlet-name>t1</servlet-name>
<servlet-class>com.abc.test.T1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>t1</servlet-name>
<url-pattern>/servlet/t1</url-pattern>
</servlet-mapping>
<servlet-name>t1</servlet-name>
<servlet-class>com.abc.test.T1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>t1</servlet-name>
<url-pattern>/servlet/t1</url-pattern>
</servlet-mapping>
在浏览器中输入 /servlet/t1 就可以访问该页面,修改web.xml文件后一般需要重启服务器。