Tomcat部署Servlet出现404解决方案

来源:互联网 发布:数据合计js结果显示 编辑:程序博客网 时间:2024/04/29 04:49

博主在学习《Head First Servlets & JSP》时,访问Servlet出现404错误。但文件中只有一个Servlet,所以问题就出在web.xml中。(因为重新学习,所以只用了编辑器,没有使用IDE)


web.xml:

<?xml version="1.0" encoding="ISO-8851-1" ?><web-app xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4"><servlet><servlet-name>Chapter1 Servlet</servlet-name><servlet-class>Ch1Servlet</servlet-class></servlet><servlet-mapping><servlet-name>Chapter1 Servlet</servlet-name><url-pattern>/Serv1</url-pattern></servlet-mapping></web-app>

接着,在Eclipse中重新创建一个工程。生成的web.xml为:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>ch1</display-name>  <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>  <servlet><servlet-name>Chapter1 Servlet</servlet-name><servlet-class>Ch1Servlet</servlet-class></servlet><servlet-mapping><servlet-name>Chapter1 Servlet</servlet-name><url-pattern>/Serv1</url-pattern></servlet-mapping></web-app>
是不是发现有很多不同的地方,接着博主就对每一个不同的地方开始查找。最后发现,居然是编码问题!编码问题!编码问题!(重要的话要说三遍  -.- )


修改后的web.xml :

<?xml version="1.0" encoding="UTF-8" ?><web-app xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4"><servlet><servlet-name>Chapter1 Servlet</servlet-name><servlet-class>Ch1Servlet</servlet-class></servlet><servlet-mapping><servlet-name>Chapter1 Servlet</servlet-name><url-pattern>/Serv1</url-pattern></servlet-mapping></web-app>

注意,第一行的encoding的值变了。

接着去浏览器访问试试


嗯,居然过了....然过了...过了...了



得出一个经验:以后出现问题先看看编码对不对

0 0