4. 尚硅谷_佟刚_JavaWEB_第一个 Servlet 程序.wmv
I. 第一个Servlet程序
X) Servlet程序本身
11
public class HelloServlet implements Servlet {
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
System.out.println("getServletConfig");
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
System.out.println("getServletInfo");
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("service");
}
public HelloServlet(){
System.out.println("HelloServlet's constructor");
}
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
System.out.println("getServletConfig");
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
System.out.println("getServletInfo");
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("service");
}
public HelloServlet(){
System.out.println("HelloServlet's constructor");
}
}
X) web.xml 中配置Serlvet. (Ctrl+Shift+f 会格式化代码)
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置和映射 Servlet -->
<servlet>
<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置和映射 Servlet -->
<servlet>
<!-- Servlet 注册的名字-->
<servlet-name>helloServlet</servlet-name>
<!-- Servlet 的全类名-->
<servlet-class>com.atguigu.javaweb.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-class>com.atguigu.javaweb.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<!-- 需要和某一个 servlet 节点的serlvet-name 子节点的文本节点一致 -->
<servlet-name>helloServlet</servlet-name>
<servlet-name>helloServlet</servlet-name>
<!-- 映射具体的访问路径: / 代表当前WEB 应用的根目录 -->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
X) 运行结果