1、目录结构
2、各文件内容
index.jsp
1
2
3
4
5
6
7
8
9
10
11
|
<%@ 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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时,出现的错误提示
1
2
3
4
5
6
|
<?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()方法,或未覆写相对应的方法出现的错误提示
1
2
3
4
5
6
|
package web.servlet; @webservlet (name = "servlet" ) public class servlet extends httpservlet { } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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项目运行时出错的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/gdwkong/archive/2017/08/29/7442697.html