Web编程---Servlet技术

文章目录

一、目的

  • Servlet接口及其实现类的使用

  • Servlet虚拟路径映射的配置

  • 使用Eclipse工具开发Servlet

  • Servlet的生命周期

二、原理

Servlet由Servlet容器提供,所谓的Servlet容器是指提供了Servlet 功能的服务器(本书中指Tomcat),Servlet容器将Servlet动态的加载到服务器上。

Servlet的生命周期

Servlet的生命周期是指Servlet从被Servlet容器加载、初始化,到处理客户端请求,直至销毁的整个过程。以下是Servlet生命周期的详细介绍:

三、过程

1. TestServlet02文件演示效果

(1) 通过getServletConfig()函数获取初始化参数里面内容。

(2) 通过get()展示获取到的参数内容

2. TestServlet03文件演示效果

(1) 通过getServletContext()函数获取参数内容

(3) 通过get()展示获取到的参数内容

3. TestServlet04与TestServlet05文件演示效果

(1) 文件4设置参数,文件5获取参数

(2) 通过get()展示获取到的参数内容

4. 控制台展示生命周期过程

代码段

四、代码

web.xml文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0">
  <!-- <display-name>chapter03</display-name> -->

  <!-- 3.4.1 小结配置信息 -->
	<servlet>
		<servlet-name>TestServlet02</servlet-name>
		<servlet-class>cn.itcast.servlet.TestServlet02</servlet-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet02</servlet-name>
		<url-pattern>/TestServlet02</url-pattern>
	</servlet-mapping>
  
  <!-- 3.4.2 配置信息 -->
	<context-param>
		<param-name>companyName</param-name>
		<param-value>itcast</param-value>
	</context-param>
	<context-param>
		<param-name>address</param-name>
		<param-value>beijing</param-value>
	</context-param>
	<servlet>
		<servlet-name>TestServlet03</servlet-name>
		<servlet-class>cn.itcast.servlet.TestServlet03</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet03</servlet-name>
		<url-pattern>/TestServlet03</url-pattern>
	</servlet-mapping>
	
	<servlet>
		<description></description>
		<display-name>TestServlet04</display-name>
		<servlet-name>TestServlet04</servlet-name>
		<servlet-class>cn.itcast.servlet.TestServlet04</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet04</servlet-name>
		<url-pattern>/TestServlet04</url-pattern>
	</servlet-mapping>

	<servlet>
		<description></description>
		<display-name>TestServlet05</display-name>
		<servlet-name>TestServlet05</servlet-name>
		<servlet-class>cn.itcast.servlet.TestServlet05</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet05</servlet-name>
		<url-pattern>/TestServlet05</url-pattern>
	</servlet-mapping>
	
	
	

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
   <!--  <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file> -->
    
  </welcome-file-list>
</web-app>  

TestServlet02.java文件

java 复制代码
package cn.itcast.servlet;
import java.io.*;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class TestServlet02 extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		super.doGet(req, resp);
		PrintWriter out = resp.getWriter();
		// 获得ServletConfig对象
		ServletConfig config = this.getServletConfig();
		// 获得参数名为encoding对应的参数值
		String param = config.getInitParameter("encoding");
		out.println("encoding=" + param);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
}

TestServlet03.java文件

java 复制代码
package cn.itcast.servlet;
import java.io.*;
import java.util.*;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class TestServlet03 extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = resp.getWriter();
        // 得到ServletContext对象
		 ServletContext context = this.getServletContext();
		 // 得到包含所有初始化参数名的Enumeration对象
		Enumeration<String> paramNames = context.getInitParameterNames();
		  // 遍历所有的初始化参数名,得到相应的参数值,打印到控制台
		 out.println("all the paramName and paramValue are following:");
		  // 遍历所有的初始化参数名,得到相应的参数值并打印
		 while (paramNames.hasMoreElements()) {
			String name = paramNames.nextElement();
			String value = context.getInitParameter(name);
			out.println(name + ": " + value);
            out.println("<br>");
		 }
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
}

TestServlet04.java文件

java 复制代码
package cn.itcast.servlet;
import java.io.*;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class TestServlet04 extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		super.doGet(req, resp);
		ServletContext context = this.getServletContext();
		// 通过setAttribute()方法设置属性值
		context.setAttribute("data", "this servlet save data");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
}

TestServlet05.java文件

java 复制代码
package cn.itcast.servlet;
import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class TestServlet05 extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		super.doGet(req, resp);
		ServletContext context = this.getServletContext();
		Object object = context.getAttribute("data");
		PrintWriter out = resp.getWriter();
		out.println(object.toString());
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
}

SomeServlet.java文件

java 复制代码
package com.powernode.servlets;

import java.io.IOException;

import jakarta.servlet.Servlet;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;


public class SomeServlet implements Servlet {
	private ServletConfig config;
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config = config;
		System.out.println("init");
	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return config;
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		获取servlet名称
		String servletName = config.getServletName();
		System.out.println("获取servlet名称:" + servletName);
		
//		获取servletContext对象
		ServletContext servletContext = config.getServletContext();
		System.out.println("获取servletContext对象" + servletContext);
//		获取初始化参数
		String initParameter1 = config.getInitParameter("name");
		System.out.println("name:" + initParameter1);
		
		System.out.println("service");
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("destroy()");
		
	}

}
相关推荐
哑巴语天雨24 分钟前
React+Vite项目框架
前端·react.js·前端框架
初遇你时动了情38 分钟前
react 项目打包二级目 使用BrowserRouter 解决页面刷新404 找不到路由
前端·javascript·react.js
乔峰不是张无忌3301 小时前
【HTML】动态闪烁圣诞树+雪花+音效
前端·javascript·html·圣诞树
鸿蒙自习室1 小时前
鸿蒙UI开发——组件滤镜效果
开发语言·前端·javascript
m0_748250741 小时前
高性能Web网关:OpenResty 基础讲解
前端·openresty
前端没钱2 小时前
从 Vue 迈向 React:平滑过渡与关键注意点全解析
前端·vue.js·react.js
NoneCoder2 小时前
CSS系列(29)-- Scroll Snap详解
前端·css
无言非影2 小时前
vtie项目中使用到了TailwindCSS,如何打包成一个单独的CSS文件(优化、压缩)
前端·css
我曾经是个程序员2 小时前
鸿蒙学习记录
开发语言·前端·javascript
羊小猪~~2 小时前
前端入门之VUE--ajax、vuex、router,最后的前端总结
前端·javascript·css·vue.js·vscode·ajax·html5