分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

创建Web项目运行时出小错误及解决方法

发布时间:2023-09-06 01:07责任编辑:苏小强关键词: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.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项目运行时出小错误及解决方法

原文地址:http://www.cnblogs.com/gdwkong/p/7442697.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved