Servlet - note01

本文介绍了Servlet的概念及其核心机制——请求-响应模型,并详细说明了在Tomcat容器中部署Servlet的具体步骤,包括继承HttpServlet类、重写doGet/doPost方法及在web.xml中配置Servlet。

1、Servlet是服务器上运行的小程序;
它的核心是:请求-响应;

2、Tomcat容器等级
分为四个等级:Servlet容器管理Context容器;一个Context容器对应一个Web工程;
这里写图片描述

3、建Servlet步骤
1.继承HttpServlet
2.重写doGet()或doPost()
3.在web.xml中注册Servlet
例如:`

<servlet>
<servlet-name>servlet_name</servlet-name>
<servlet-calss>package_name.servlet_class_name(这是处理类名称)</servlet-calss>
</servlet>
<servlet-mapping>
<servlet-name>servlet_name(对应上面的servlet_name)</servlet-name>
<url-pattern>/package_name/servlet_class_name(这是请求的do,首先对应页面中的请求)</url-pattern>
</servlet-mapping>

4、编写Servlet的doPost方法时,需要抛出异常为:ServletExcpetion和IOException异常

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.3" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_3.xsd"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 配置spring资源 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <!-- 乱码问题 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置springmvc DispatcherServlet接收到的http请求通过DispatcherServlet进行分发。--> <servlet> <servlet-name>note</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--Sources标注的文件夹下需要新建一个spring文件夹--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>note</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>检查代码修改正确
06-07
### 验证 `web.xml` 配置文件中的 Spring MVC、字符编码过滤器和欢迎文件列表等配置是否正确 #### 1. Spring MVC 配置验证 在 `web.xml` 中配置 Spring MVC 的核心是通过 `DispatcherServlet` 来实现的。以下是一个标准的配置示例,确保配置符合规范: ```xml <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.zxh</url-pattern> </servlet-mapping> ``` - 确保 `<servlet-name>` 和 `<servlet-mapping>` 中的名称一致[^3]。 - 如果未指定 `contextConfigLocation` 参数,则默认会加载 `/WEB-INF/springMvc-servlet.xml` 文件。 #### 2. 字符编码过滤器配置验证 字符编码过滤器通常用于解决请求参数和响应内容的编码问题。以下是一个标准的配置示例: ```xml <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` - 确保 `encoding` 参数设置为 `utf-8`,以支持多语言环境[^2]。 - `forceEncoding` 参数应设置为 `true`,以强制覆盖客户端可能设置的不正确编码[^2]。 #### 3. 欢迎文件列表配置验证 欢迎文件列表用于定义当用户访问一个目录时,服务器返回的默认文件。以下是一个标准的配置示例: ```xml <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> ``` - 确保列出的文件名与实际项目中存在的一致。 - 如果项目中有多个入口文件(如 `index.html` 和 `index.jsp`),可以按优先级顺序列出。 #### 4. 其他常见问题检查 - **语法错误**:确保所有标签都正确闭合,例如 `<filter>` 必须有对应的 `</filter>`[^3]。 - **路径问题**:如果指定了 `contextConfigLocation`,确保路径下的配置文件存在且内容正确[^3]。 - **加载顺序**:`<listener>` 标签通常需要放在 `<servlet>` 标签之前,以确保上下文初始化顺序正确[^3]。 ```xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` #### 示例完整配置 以下是一个完整的 `web.xml` 配置示例,结合了 Spring MVC、字符编码过滤器和欢迎文件列表的配置: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- Spring 上下文加载 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 字符编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC 配置 --> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>*.zxh</url-pattern> </servlet-mapping> <!-- 欢迎文件列表 --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值