Tomcat部署Servlet出现404解决方案
2017-03-17 20:59
519 查看
博主在学习《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/j2ee http://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/j2ee http://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的值变了。
接着去浏览器访问试试
嗯,居然过了....然过了...过了...了
得出一个经验:以后出现问题先看看编码对不对
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/j2ee http://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/j2ee http://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的值变了。
接着去浏览器访问试试
嗯,居然过了....然过了...过了...了
得出一个经验:以后出现问题先看看编码对不对
相关文章推荐
- Tomcat部署Servlet出现404解决方案
- 从Eclipse启动tomcat服务器,访问出现 404 错误的解决方案
- Tomcat部署web项目,虚拟目录,上下文(Context),WEB-INF,web.xml,servlet,404
- tomcat下部署两个工程时,只有一个可以访问,另一个出现404错误,该如何解决
- tomcat出现404的问题解决方案
- 在eclipse上用tomcat部署项目404解决方案
- Tomcat 部署项目后,访问出现404
- 同个Tomcat下部署两个项目,一个可以另一个不可以,出现404
- tomcat 下部署的应用出现ava.lang.OutOfMemoryError: unable to create new native thread ,解决方案。
- 部署tomcat中出现的java程序 java编译错误 程序包javax.servlet不存在javax.servlet.*
- Spring 部署Tomcat 404 错误解决方案
- angular2项目构建后部署到Tomcat刷新页面出现404的问题
- 项目部署到tomcat上启动后出现404错误
- 关于使用Myeclipse 部署 web项目到tomcat上出现PermGen space的解决方案
- 使用myeclipse插入数据到mysql 出现中文乱码|utf8乱码解决方案[适合tomcat部署的jsp应用]
- eclipse下tomcat7.0自测出现“404”异常和部署项目报404的解决办法
- tomcat中部署项目后,访问失败,报错404的原因以及解决方案
- 详解angular部署到iis出现404解决方案
- eeplat 在tomcat 6.0下面部署的时候出现Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpSessi
- 在部署Struts时,出现如下错误信息:HTTP Status 404 - Servlet action is not available