Create a Web project run-time error and workaround

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.