徒手创建Servlet程序实例-获取当前日期Tomcat部署

2022-08-05 11:09:58

徒手创建Servlet程序实例-获取当前日期——Tomcat部署

简介

对于javaEE规范有点生疏了,最近正好有时间。准备整理下,里面几个常用的web规范。就先以servlet开始吧!

Servlet介绍

Servlet名字由来

Servlet是有 server 和 applet 组成的。 直译过来就是服务程序, 把Server的er去掉和applet 的app去电 组成 servlet 。

百度解释

Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容。

个人理解

可以使用jsp 动态网页技术进行结合,实现动态生成网页内容。具体什么是动态网页,可以理解为,不同时间段和不同的访问者看到的内容不一致。不是同一个内容,就是动态网页。

这里的配置可以和mvc里的Servlet做类比,这里 的配置和mvc里的完全一致。在做ssm开发的时候,可以使用servlet 配置一个总的Servlet程序来转发,前端请求的具体资源。咱们这次这是把前端请求映射到我们自己写Servlet上了,mvc是映射到 spring的 DispatcherServlet 中心控制器上。

获取当前日期实例

目录结构

新建目录效果请添加图片描述

请添加图片描述

code

import java.util.Date;
import javax.servlet.*;






public class  FirstServlet extends GenericServlet {
    public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException {
         res.getWriter().println(new Date());
    }

}

java文件编译成class文件

javac -classpath "指定包路径"   *.java   -使用指定的jar 进行编辑java文件为class

编辑 java文件->class文件实操

javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 8.5\lib\servlet-api.jar" FirstServlet.java

请添加图片描述

注意事项

javac -classpath 要指定Servlet的资源包,以为默认情况下,Servlet资源包是不会加载的。当然这和类加载机制有关系,解决此问题的方式有如下三种方式,本次采用的方式就是第三种方式(推荐使用,零侵入)

类加载机制

  1. Bootsrap 类加载器,负责加载Java的核心类库,JAVA_Home/jre/下面的类
  2. Extension 类加载器: 负责加载JAVA/jre/lib/ext 下面的类
  3. System类加载器: 负责将命令中的classpah 或者CLASSPATH环境变量指定的类库加载进内存中

编辑成功,java文件同目录下多出一个同名文件.class 文件

请添加图片描述

servlet包路径位置

编译遇到的问题-拒绝访问

请添加图片描述

解决方案

因为 ServletTest/WEB-INF /classes 没有写入权限所以写入class文件失败

在此文件下 空白处 鼠标右击选择属性

  1. 右击选择属性

请添加图片描述

2.找到安全选项卡,点击编辑

请添加图片描述

3.找本机对应用户,红线圈住位置的复选框都打上对钩

请添加图片描述

Tomcat-项目部署

启动tomcat,因为刚才的项目是直接在tomcat的打包目录新建的,所以直接启动tomcat即可

webapps下一个文件就是一个项目-文件夹名字就是项目名字

请添加图片描述

启动成功!

请添加图片描述

Servlet配置介绍

<?xml version="1.0" encoding="UTF-8"?>
<!--
 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://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"
  metadata-complete="true">

  <servlet>
    <servlet-name>FirstServlet</servlet-name>
    <servlet-class>FirstServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>FirstServlet</servlet-name>
      <url-pattern>/test/</url-pattern>
  </servlet-mapping>
  
</web-app>

配对使用,上那个是定位到咱们自己的那个java类上的映射

下面的mapping 映射用于匹配前端URL地址请求,适合和里的设置一直

效果展示

完美部署成功!

测试地址:localhost:8080/servletTest/test/

请添加图片描述

感谢阅读~都已经看到这了,不妨给个一键三连

  • 作者:康世行
  • 原文链接:https://kangshihang.blog.csdn.net/article/details/124514412
    更新时间:2022-08-05 11:09:58