基于创建Web项目运行时出错的解决方法(必看篇)
2017-08-30 11:14
549 查看
1、目录结构
2、各文件内容
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="/Servlet" method="post"> <input type="submit" value="提交"> </form> </body> </html>
web.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"> <servlet> <servlet-name>Servlet</servlet-name> <servlet-class>web.servlet.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/Servlet</url-pattern> </servlet-mapping> </web-app>
Servlet.java
package web.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "Servlet") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doPost()..."); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet()..."); } }
正常运行后运行后,Server控制台会出现输出“doPost()...”字符
3、当未配置web.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"> </web-app>
解决方法:在web.xml中配置相关信息,或在Servlet.java注解中添加内容:
@WebServlet(name = "Servlet",urlPatterns = "/Servlet")
4、配置web.xml文件但未覆写doGet()和doPost()方法,或未覆写相对应的方法出现的错误提示
package web.servlet; @WebServlet(name = "Servlet") public class Servlet extends HttpServlet { }
package web.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "Servlet") public class Servlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet()..."); } }
以上这篇基于创建Web项目运行时出错的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- VS2003的调试症状:“试图运行项目时出错:无法在web服务器上启动调试。”的解决方法
- 用idea创建web项目,servlet response 等出错的原因(jsp中内置对象方法无法被解析的解决办法)
- 创建Web项目运行时出小错误及解决方法
- [转] 试图运行项目时出错:无法在web服务器上启动项目得解决办法(转载)
- VS2010创建SILVERLIGHT项目时提示“你需要安装最新的SILVERLIGHT DEVELOPER运行时”解决方法
- 用maven在eclipse中创建web项目时不能创建三个文件夹的解决方法
- 试图运行项目时出错:无法在web服务器上启动项目得解决办法
- 试图运行项目时出错:无法在web服务器上启动项目得解决办法
- VS 2005 调试过程中不定期出现 ”试图运行项目时出错:无法启动调试。拒绝调试”的对话框的解决方法
- 关于jdk版本问题导致的eclipse导入web项目出错解决方法
- VS2010创建SILVERLIGHT项目时提示“你需要安装时新的SILVERLIGHT DEVELOPER运行时”的解决方法?
- Eclipse中直接复制粘贴Web项目并运行后,项目名字还是原来名字无法运行的问题解决方法
- 试图运行项目时出错:无法在web服务器上启动项目得解决办法
- 关于在VS2013下创建OpenCV项目出错的解决方法
- Eclipse中tomcat不识别web项目,导致无法部署运行的解决方法
- 试图运行项目时出错:无法在web服务器上启动项目得解决办法
- 试图运行项目时出错:无法在web服务器上启动项目得解决办法
- 用VS.NET打开或创建web项目出错的解决办法
- VS2008创建Silverlight项目时出错解决方法
- “试图运行项目时出错:无法在 web 服务器上启动调试。” 的解决办法