溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于創(chuàng)建Web項(xiàng)目運(yùn)行時(shí)出錯(cuò)的解決方法(必看篇)

發(fā)布時(shí)間:2020-09-18 01:45:41 來(lái)源:腳本之家 閱讀:153 作者:dwkong 欄目:編程語(yǔ)言

1、目錄結(jié)構(gòu)

基于創(chuàng)建Web項(xiàng)目運(yùn)行時(shí)出錯(cuò)的解決方法(必看篇)

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>

基于創(chuàng)建Web項(xiàng)目運(yùn)行時(shí)出錯(cuò)的解決方法(必看篇)

解決方法:在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ò)的解決方法(必看篇)

以上這篇基于創(chuàng)建Web項(xiàng)目運(yùn)行時(shí)出錯(cuò)的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI