Dynamic web module version版本:2.5
Servlet的继承关系:
实现方法:
方式一:
- 1、新建普通java类
- 2、继承HttpServlet类
- 3、重写service()方法
- 4、配置web.xml配置项(WebContent/WEB-INF目录下的web.xml)
public class Servlet01 extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
System.out.println("Servlet01...");
}
/**
* 该方法在web.xml配置项中有初始化参数是需要重写.
* 可以接收web.xml配置项
* <init-param>中的初始化参数
*/
@Override
public void init(ServletConfig config) throws ServletException {
// 得到初始化参数
String encode = config.getInitParameter("encode");
System.out.println("编码:" + encode);
}
}
web.xml配置项写法
<?xml version="1.0" encoding="UTF-8"?> <!-- ?xml表示声明当前是xml文件;version表示当前xml的版本;encoding表示文件编码; 这句话一定要写在第一行 -->
<!-- web-app表示是当前xml文件的根节点,一个xml文件有且只有一个根节点 -->
<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">
<!-- 当前项目的名称 -->
<display-name>servlet01</display-name>
<!-- 当访问该项目时,没有给定资源路径,会自动在welcome-file-list标签中查找配置好的路径,找到则显示,未找到报404 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--
注意点:
1、修改了xml配置文件中的数据,必须重启服务器才会生效
2、servlet标签中的servlet-name不能有相同的
3、servlet-mapping中的url-pattern的值不能有相同的
否则报错:The servlets named [servlet01] and [servlet02] are both mapped to the url-pattern [/ser01] which is not permitted
4、说明: url-pattern 可以配多个(一个 servlet 可以通过多个 url-pattern 访问)
通配符 “*”只能放在最前面或最后面,不能放中间且不能单独存在(以/分割的,不能和单词组成整体)。越精确越优先。
-->
<!-- 配置servlet -->
<servlet>
<servlet-name>servlet01</servlet-name> <!-- 自定义名称,给服务器看的,与servlet-mapping标签中的servlet-name的值一致 -->
<servlet-class>com.shsxt.servlet.Servlet01</servlet-class> <!-- 当前配置的servlet类的路径;按住"ctrl"键 ,能点到类中表示路径正确-->
<!-- 初始化参数 -->
<init-param>
<param-name>encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 自启动:服务器启动时自动实例化servlet;值越小,优先级越高 -->
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 配置servlet的映射 -->
<servlet-mapping>
<servlet-name>servlet01</servlet-name> <!-- 自定义名称,给服务器看的,与servlet标签中的servlet-name的值一致 -->
<url-pattern>/ser01</url-pattern> <!-- 对外访问路径,给浏览器访问的;路径前要加"/" -->
<!-- 通配符"*"的使用规则:只能放在最前面或者最后面,不能放中间,不能单独使用,不能和单词组合使用;值越准确,优先级越高 -->
<!-- 设置路径只要以html结尾,都可以访问到该servlet -->
<url-pattern>*.html</url-pattern>
<!-- 路径包含指定目录都可访问 -->
<url-pattern>/test/*</url-pattern>
<!-- 无论什么路径都可访问 -->
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
web.xml配置可能出现的错误:
1.The markup in the document following the root element must be well-formed.
解决方法:
web.xml中的标签要写在<web-app></web-app>
中间。
2.严重: Allocate exception for servlet servlet01
java.lang.ClassNotFoundException: com.shsxt.servlet.Servlet01
修改servlet-class对应的项目路径及名称
3.xml文件eclipse没有提示:
文件打开格式不对, 右键–>open with -->XML Editor 打开;
左下角换成Source就是xml格式打开,有提示功能.
方式二:
- 1、新建普通java类
- 2、继承GenericServlet类
- 3、实现service()方法
- 4、配置web.xml配置项(WebContent/WEB-INF目录下的web.xml>>web.xml文件如同方式一)
public class Servlet02 extends GenericServlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("Servlet02...");
}
}
方式三:
- 1、新建普通java类
- 2、实现Servlet接口
- 3、实现接口中方法
- 4、配置web.xml配置项(WebContent/WEB-INF目录下的web.xml>>web.xml文件如同方式一)
public class Servlet03 implements Servlet {
/**
* 系统方法,由系统调用,初始化方法,当servlet被实例化时调用,只会执行一次
*/
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("Servlet03 出生了...");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* 系统方法,由系统调用,服务方法,当servlet被访问时调用,可以被执行多次
*/
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("Servlet03...");
}
@Override
public String getServletInfo() {
return null;
}
/**
* 系统方法,由系统调用,销毁方法,当servlet被销毁时调用,只执行一次(服务器正常关闭才会执行)
*/
@Override
public void destroy() {
System.out.println("Servlet03 go die了...");
}
}
方式四:
- 1、新建普通java类
- 2、继承HttpServlet类
- 3、重写doGet()和doPost()方法
- 4、配置web.xml配置项(WebContent/WEB-INF目录下的web.xml)
public class Servlet04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet04... GET...");
doPost(req, resp);//调用doPost方法,这样不用重复写代码
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet04... POST...");
// 代码全写在post中
}
}