Servlet-基础开发步骤(使用servlet接口)

本实例主要演示servlet的开发步骤。

工具:tomcat  Jcreator 浏览器


1 在tomcat文件夹的webapps文件夹内新建WebFirst文件夹,接着在WebFirst下新建WEB-INF文件夹,Web-INF文件夹下新建classes文件夹以及lib文件夹,同时新建、web.xml文件


2 使用Jcreator新建java文件,并且导入servlet-api包

(1)导入jar包





(2)编写源代码,保存到之前新建的classes文件夹

编写基本java文件如下:

package com.tsinghua;

import javax.servlet.*;
import java.io.*;

class HelloWord{

}


实现接口(如图):



实现全部源代码:

//first Servlet(使用接口)

package com.sw;

import javax.servlet.*;
import java.io.*;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class HelloWord implements Servlet{
	/**
	 * Method init
	 *
	 *
	 * @param parm1
	 *
	 @throws ServletException
	 *
	 */
	 //用于初始化servlet(类似于类的构造函数)
	 //该函数只会被调用一次(当用户第一次访问时调用)
	public void init(ServletConfig parm1) throws ServletException {
		// TODO: 在这添加你的代码
		System.out.println("init it");
	}

	/**
	 * Method getServletConfig
	 *
	 *
	 * @return
	 *
	 */
	 //得到serclet配置文件
	public ServletConfig getServletConfig() {
		// TODO: 在这添加你的代码
		return null;
	}

	/**
	 * Method service
	 *
	 *
	 * @param parm1
	 * @param parm2
	 *
	 @throws ServletException
	 @throws IOException
	 *
	 */
	 //用于处理业务逻辑
	 //书写业务逻辑代码
	 //每次访问时都会被调用
	 //Request用于获得客户端的信息
	 //Response用于向客户端返回信息
	public void service(ServletRequest parm1, ServletResponse parm2) throws ServletException, IOException {
		// TODO: 在这添加你的代码
		//从parm2中得到printwriter
		System.out.println("service it");
		
		PrintWriter pw=parm2.getWriter();
		pw.println("First servlet!");
	}

	/**
	 * Method getServletInfo
	 *
	 *
	 * @return
	 *
	 */
	public String getServletInfo() {
		// TODO: 在这添加你的代码
		return null;
	}

	/**
	 * Method destroy
	 *
	 *
	 */
	 //销毁servlet实例(释放内存)
	 //1 重装serclet(webapps)时候 2 关闭tomcat时 3 关机时
	public void destroy() {
		// TODO: 在这添加你的代码
		System.out.println("destory!");
	}
		
	
}

3 编写web.xml文件如下(之前新建的web.xml文件)

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
  <servlet>
  <!--servlet名字-->
  <servlet-name>HelloWord</servlet-name>
  <!--sevlet(包名+类名)-->
  <servlet-class>com.sw.HelloWord</servlet-class>
  </servlet>
  
  <!--??-->
  <servlet-mapping>
  <!--??-->
  <servlet-name>HelloWord</servlet-name>
  <!--浏览器访问的url(任意)-->
  <url-pattern>/first</url-pattern>
  </servlet-mapping>

</web-app>

4 打开浏览器

输入:http://localhost:8080/WebFirst/first即可进行访问,查看到数据


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在创建Maven项目时,如果没有添加servlet-api依赖,可能会出现无法编译和运行Web应用程序的问题。这是因为servlet-api是一组Java类和接口,用于开发Servlet程序。如果没有这些类和接口,Java编译器将无法识别HttpServletRequest和HttpServletResponse等Servlet相关的类和方法。 要解决这个问题,可以手动添加servlet-api依赖。在pom.xml文件中,添加以下依赖: ``` <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> ``` 这将让Maven自动下载并包含所需的servlet-api JAR文件。如果使用了其他版本的servlet-api,则需要相应地更改版本号。 另外,可以选择使用Web项目模板来创建Maven项目。这将自动包括servlet-api依赖项,从而避免了手动添加依赖项的步骤。在Eclipse和IntelliJ IDEA等现代IDE中,都提供了Web项目模板来简化Web应用程序的创建和管理。 总之,为了顺利编译和运行Web应用程序,需要在Maven项目中添加servlet-api依赖项。可以手动添加依赖项,也可以使用Web项目模板来自动添加依赖项。 ### 回答2: 在使用IDEA创建Maven项目时,可能会出现没有servlet-api的情况。这是因为servlet-api不是Maven中默认依赖的部分。如果需要在项目中使用servlet相关的API,需要手动添加servlet-api的依赖。 解决此问题的步骤如下: 第1步:在pom.xml文件中添加servlet-api依赖。 在pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> ``` 第2步:更新项目 在Maven项目中,应该从Maven Projects面板中更新项目以让Maven解析新依赖项。 第3步:重新构建项目 在更新项目之后,应该重新构建项目以确保servlet-api包已包含在项目中。重新构建可以通过执行以下操作完成: - 选择“Build”菜单; - 选择“Rebuild Project”选项。 第4步:使用servlet API 在添加依赖和重新构建项目后,可以在项目中使用servlet API了。 总结:在使用IDEA创建Maven项目时,如果需要使用servlet API,需要手动添加servlet-api的依赖。添加依赖项、更新项目、重新构建项目后,即可使用servlet API了。 ### 回答3: 当我们使用IDEA创建一个Maven项目时,会发现servlet-api并没有被包含在项目中。这是因为servlet-api是JavaEE的标准,而Maven项目入门模板是基于JavaSE的,没有包含这一扩展。 那么,如何解决这个问题呢?下面是具体的解决方案: 1. 手动添加servlet-api依赖 可以在项目的pom.xml文件中手动添加servlet-api依赖,如下所示: ``` <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>{servlet-api版本号}</version> </dependency> ``` 其中,servlet-api版本号可以根据自己的需要进行设置。 2. 通过Maven导入servlet-api 在Maven的仓库中,servlet-api是可以找到的。因此,我们可以通过在项目中添加如下依赖,直接从Maven仓库中导入servlet-api: ``` <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>{servlet-api版本号}</version> <scope>provided</scope> </dependency> ``` 其中,scope为provided,表示该依赖只在编译和测试时使用,在运行时会由应用服务器提供。 3. 使用webapp Maven archetype创建项目 webapp Maven archetype是一种特殊的Maven模板,它已经包含了servlet-api和其他Web相关依赖。因此,使用这个模板创建的项目就不会有缺少servlet-api的问题了。 以上是关于解决IDEA创建Maven项目没有servlet-api的三种方法。不同的方法适合不同的场景,在使用中要根据实际情况进行选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值