你的位置:首页 > Java教程

[Java教程]创建Web项目运行时出小错误及解决方法

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.

<??><web-app ="http://  ="http://www.w3.org/2001/  xsi:schemaLocation="http://   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.

<??><web-app ="http://  ="http://www.w3.org/2001/  xsi:schemaLocation="http://   version="3.1"></web-app>

解决方法:在web.

 @WebServlet(name = "Servlet",urlPatterns = "/Servlet") 

4、配置web.

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()..."); }}