1. Directory structure
2. Contents of each document
index.jsp
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
Xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "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
PackageWeb.servlet;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.io.IOException; @WebServlet (name= "Servlet") Public classServletextendsHttpServlet {protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {System.out.println ("DoPost () ..."); } protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {System.out.println ("Doget () ..."); }}
After running normally, the server console will appear with the output "doPost () ..." character
3. Error prompt when Web. XML is not configured
<? XML version= "1.0" encoding= "UTF-8" ?> < 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/ Java ee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd " version=" 3.1 " ></web-app>
Workaround: Configure the relevant information in Web. xml or add content to the Servlet.java annotation:
@WebServlet (name = "Servlet", Urlpatterns = "/servlet")
4. Configure the Web. xml file but do not overwrite the doget () and Dopost () methods, or do not overwrite the corresponding method appears error prompt
package = "Servlet")publicclassextends httpservlet { }
PackageWeb.servlet;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.io.IOException; @WebServlet (name= "Servlet") Public classServletextendsHttpServlet {protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {System.out.println ("Doget () ..."); }}
Create a Web project run-time error and workaround