专注Java教育14年 全国咨询/投诉热线:444-1124-454
星辉LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Servlet的作用及开发步骤

Servlet的作用及开发步骤

更新时间:2022-01-04 10:17:47 来源:星辉 浏览665次

1.概念

Servlet:Servlet Apple的简称,是服务器端的程序(代码,功能实现),可交互的处理客户端发送到服务端的请求,并完成操作响应

动态网页技术

JavaWeb程序开发的基础,JavaEE规范(一套接口)的一个组成部分。

2.Servlet作用

接收客户端请求,完成操作

动态生成网页(页面数据可变)

将包含操作结果的动态网页响应给客户端

3.Servlet开发步骤

(1)搭建开发环境

将Servlet相关的jar包(lib\servlet-api.jar)配置到classpath中

(2)编写Servlet

实现javax.servlet.Servlet

重写5个主要方法

在核心的service()方法中编写输出语句,打印访问结果

MyServlet.java

import java.io.IOException; 
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyServlet implements Servlet { 
    @Override
    public void destroy() {
        // TODO Auto-generated method stub 
    } 
    @Override
    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return null;
    } 
    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    } 
    @Override
    public void init(ServletConfig arg0) throws ServletException {
        // TODO Auto-generated method stub 
    } 
    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        // TODO Auto-generated method stub
		System.out.println("My First servlet"); 
    } 
}

(3)部署Servlet

编译MyServlet后,将生成的class文件放在WEB-INF/classes文件中

(4)配置Servlet

编写WEB-INF下项目配置文件web.xml

<?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>my</servlet-name>
	<servlet-class>MyServlet</servlet-class>
 </servlet> 
 <servlet-mapping>
	<servlet-name>my</servlet-name>
	<url-pattern>/myservlet</url-pattern>
 </servlet-mapping> 
</web-app>

注意:url-pattern 配置的内容就是浏览器地址栏输入的url中项目名称后资源的内容

(5)运行测试

启动Tomcat,在浏览器地址栏中输入http://localhost:8080/myweb/myservlet访问,在Tomcat中打印时间表示成功

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>