idea报错:Allocate exception for servlet com.itheima.web.ServletDemo1

问题描述

idea中运行tomcat时报错:

严重: Allocate exception for servlet com.itheima.web.ServletDemo1
java.lang.ClassCastException: com.itheima.web.ServletDemo1 cannot be cast to javax.servlet.Servlet

浏览器显示如下信息:
image.png

分析与解决

原因一:导入servlet依赖时导入了版本2,但配置访问路径时采用了注解的方式:@WebServlet("/demo1"),而Servlet 从3.0版本后才开始支持使用注解配置,3.0版本前只支持 XML 配置文件的配置方式。
故解决方案有两种:
方案一:将@WebServlet("/demo1")注释掉,在web.xml中配置servlet的全类名和访问路径:

<?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_4_0.xsd"
         version="4.0">
    <!--servlet 全类名-->
    <servlet>
        <servlet-name>demo1</servlet-name>
        <servlet-class>com.itheima.web.ServletDemo1</servlet-class>
    </servlet>

    <!--servlet 访问路径-->
    <servlet-mapping>
        <servlet-name>demo1</servlet-name>
        <url-pattern>/demo1</url-pattern>
    </servlet-mapping>
</web-app>

方案二:将servlet依赖替换成3.0之后的版本:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

原因二:导入servletjsp依赖时没有添加<scope>provided</scope>,添加一下即可:

 <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
</dependency>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Beginning Amazon Web Services with Node.js teaches any novice Node.js developer to configure, deploy, and maintain scalable small to large scale Node.js applications in Amazon Web Services. Hosting a Node.js application in a production environment usually means turning to PaaS hosting, but this approach brings problems. Deploying Node.js directly to AWS solves the problems you encounter in these situations, enabling you to cut out the middle man. You will begin with a basic RESTful web service in Node.js, using the popular Express.js framework, pre-built and ready to run in your local environment. You will be introduced to the most powerful tools in AWS, and learn how to configure your project to take advantage of them. You will be guided through the steps of getting the various key components to work together on AWS. Through code samples using the AWS JavaScript SDK and tutorials in the AWS console, you will gain the knowledge to incorporate secure user authentication, server auto-scaling, a load balancer, CDN, customized caching behavior, and outage monitoring. Node.js is single-threaded, and designed to run high input / high output applications, making it ideal for the cloud environment. If your main task is handling a high volume of requests over HTTP / the web, it will scale very well in proportion to the computing power you allocate to it. Amazon Web Services offers a broad set of services that help organizations move faster, lower costs, and scale applications. Trusted by the largest enterprises and start-ups alike, AWS powers a wide variety of workloads across a broad spectrum. If leveraged properly, you can build a Node.js app on AWS which can automatically power itself up to handle a massive volume of traffic, and then scale back down to a lighter configuration when user demand has dropped. Amazon Web Services offers a broad set of services that help organizations move faster, lower costs, and scale applications. Get started with Node.js and AWS using this book today. What you’ll learn ● Using AWS Opsworks to architect and configure a secure web application built for hosting in the cloud. ● Integrating AWS Cloudwatch, SES, and other AWS services into your code. ● Using AWS Route 53 to configure your domain to use a load balancer, CDN, and other performance-enhancing services into your application. ● Deploying with AWS Opsworks and using AWS services to monitor, scale, and maintain your application. Who this book is for This book is intended for a beginner/novice Node.js developer who has traditionally relied on PaaS services such as Heroku or Parse to host their web applications. This book aims to empower the reader to work directly in AWS, in order to manage their own infrastructure. Hosting a Node.js application in a production environment usually means turning to PaaS hosting such as Heroku or AppFog, but this approach brings problems. Deploying Node.js directly to AWS solves the problems you encounter in these situations, enabling you to cut out the middle man. If leveraged properly, you can build a Node.js app on AWS which can automatically power itself up to handle a massive volume of traffic, and then scale back down to a lighter configuration when user demand has dropped. Table of Contents Chapter 1: Getting Started with Amazon Web Services Chapter 2: Working with AWS OpsWorks Chapter 3: OpsWorks Part II: Databases and Scaling Chapter 4: CloudFront and DNS Management Chapter 5: Simple Storage Service and Content Delivery Chapter 6: Simple Email Service Chapter 7: Monitoring the Application Chapter 8: Securing the Application

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在逃八阿哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值