建立Web項目運行時出小錯誤及解決方案

來源:互聯網
上載者:User

標籤:http   目錄結構   schema   結構   檔案   html   項目   lang   nbsp   

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項目運行時出小錯誤及解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.