基于創(chuàng)建Web項(xiàng)目運(yùn)行時(shí)出錯(cuò)的解決方法(必看篇)
1、目錄結(jié)構(gòu)

2、各文件內(nèi)容
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()...");
}
}
正常運(yùn)行后運(yùn)行后,Server控制臺(tái)會(huì)出現(xiàn)輸出“doPost()...”字符
3、當(dāng)未配置web.xml時(shí),出現(xiàn)的錯(cuò)誤提示
<?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中配置相關(guān)信息,或在Servlet.java注解中添加內(nèi)容:
@WebServlet(name = "Servlet",urlPatterns = "/Servlet")
4、配置web.xml文件但未覆寫doGet()和doPost()方法,或未覆寫相對(duì)應(yīng)的方法出現(xiàn)的錯(cuò)誤提示
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()...");
}
}

以上這篇基于創(chuàng)建Web項(xiàng)目運(yùn)行時(shí)出錯(cuò)的解決方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring+Mybatis動(dòng)態(tài)切換數(shù)據(jù)源的方法
這篇文章主要為大家詳細(xì)介紹了Spring+Mybatis動(dòng)態(tài)切換數(shù)據(jù)源的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Java中通過反射實(shí)現(xiàn)代理Proxy代碼實(shí)例
這篇文章主要介紹了Java中通過反射實(shí)現(xiàn)代理Proxy代碼實(shí)例,java實(shí)現(xiàn)代理可以通過java.lang.reflect.Proxy接口結(jié)合java.lang.reflect.InvocationHandler來實(shí)現(xiàn),需要的朋友可以參考下2023-08-08
詳解SpringBoot中的tomcat優(yōu)化和修改
這篇文章主要介紹了詳解SpringBoot中的tomcat優(yōu)化和修改,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Spring Boot集成sa-token的項(xiàng)目實(shí)踐
本文主要介紹了Spring Boot集成sa-token的項(xiàng)目實(shí)踐,實(shí)現(xiàn)了基本的登錄和權(quán)限控制功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
Kotlin基礎(chǔ)教程之?dāng)?shù)據(jù)類型
這篇文章主要介紹了Kotlin基礎(chǔ)教程之?dāng)?shù)據(jù)類型的相關(guān)資料,需要的朋友可以參考下2017-05-05
JAVA實(shí)現(xiàn)簡單停車場系統(tǒng)代碼
JAVA項(xiàng)目中正號(hào)需要一個(gè)停車收費(fèi)系統(tǒng),就整理出來java實(shí)現(xiàn)的一個(gè)簡單的停車收費(fèi)系統(tǒng)給大家分享一下,希望對(duì)大家有所幫助2017-04-04
Java?基于Hutool實(shí)現(xiàn)DES加解密示例詳解
這篇文章主要介紹了Java基于Hutool實(shí)現(xiàn)DES加解密,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08

