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()");
		
	}

}
相关推荐
空白诗23 分钟前
前端隐藏元素的方式有哪些?HTML 和 CSS 中隐藏元素的多种方法
前端·css·html
LilySesy28 分钟前
SAP+Internet主题HTML样式选择
前端·html·sap·abap·internet服务·its·扫码枪
AiFlutter36 分钟前
Flutter-Padding组件
前端·javascript·flutter
吾即是光1 小时前
Xss挑战(跨脚本攻击)
前端·xss
渗透测试老鸟-九青1 小时前
通过组合Self-XSS + CSRF得到存储型XSS
服务器·前端·javascript·数据库·ecmascript·xss·csrf
xcLeigh1 小时前
HTML5实现俄罗斯方块小游戏
前端·html·html5
发现你走远了2 小时前
『VUE』27. 透传属性与inheritAttrs(详细图文注释)
前端·javascript·vue.js
VertexGeek2 小时前
Rust学习(五):泛型、trait
前端·学习·rust
好开心332 小时前
javaScript交互补充(元素的三大系列)
开发语言·前端·javascript·ecmascript
小孔_H2 小时前
Vue3 虚拟列表组件库 virtual-list-vue3 的使用
前端·javascript·学习·list