欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

基于创建Web项目运行时出错的解决方法(必看篇)

时间:2022-07-18 09:33:29|栏目:JAVA代码|点击:

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

上一篇:Java包装类的缓存机制原理实例详解

栏    目:JAVA代码

下一篇:Java详细讲解分析双指针法的使用

本文标题:基于创建Web项目运行时出错的解决方法(必看篇)

本文地址:http://www.codeinn.net/misctech/208120.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有