Javaweb Servlet核心接口学习记录(1)—ServletConfig接口
servlet接口是servlet主要抽象的API,所有servlet都需要直接实现这一接口或者继承实现了该接口的类。servlet API中有两个类实现了Servlet接口,GenericServlet和HttpServlet。大多数情况下,开发人员只需要在这两个类的基础上扩展来实现他们自己的Servlet。在servlet体系中,除了上述两个类外,还有一些辅助servlet获取相关资源的重要接口:
-
ServletConfig接口:用于获取servlet初始化参数和servletContext对象;
-
ServletContext接口:代表当前servlet运行环境,servlet可以通过servletContext对象来访问Servlet容器的各种资源;
-
HttpServletRequest接口:用于封装Http请求信息;
-
HttpServletResponse接口:用于封装Http响应信息;
本小节单独介绍ServletConfig接口
- javax.servlet.ServletConfig接口的定义为:
public abstract interface javax.servlet.ServletConfig;
容器在初始化一个servlet时,将为该servlet创建一个唯一的的servletConfig对象,并将这个对象通过init(ServletConfig config)方法来传递并保存在此Servlet对象中。
2.ServletConfig的主要方法:
| 方法 | 方法描述 |
| — | — |
| getInitParameter(String param) | 根据给定的初始化参数名称,返回匹配的初始化参数值 |
| getInitParameterNames(String param) | 返回一个Enumeration对象,里面包含了所有的初始化参数 |
| getServletContext() | 返回一个ServletContext()对象
|
| getServletName() | 返回当前servlet的名字,即@WebServet的name属性值,
如果没有配置这个属性值,则返回servlet类的全限定名 |
3.ServletConfig接口获取Servlet初始化参数
由容器从Servlet的配置属性(如initParams
或<init-param>
所指定的参数)中读取。
示例: Servlet初始化参数在web.xml文件中的配置:
HelloServlet
com.qst.chapter03.servlet.HelloServlet
url
jdbc:oracle:thin:@localhost:1521:orcl
user
qst
password
qst123
Servlet初始化参数的获取:
public class HelloServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
String url = config.getInitParameter(“url”);
String user = config.getInitParameter(“user”);
String password = config.getInitParameter(“password”);
try {
Connection conn = DriverManager.getConnection(url,user,password);
}catch(SQLException e){
e.printStackTrace();
}
…
}
}
4.演示
创建ServletConfigDemo
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
- Servlet implementation class ServletConfigDemo
*/
public class ServletConfigDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
- @see HttpServlet#HttpServlet()
*/
public ServletConfigDemo() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void init(ServletConfig config) throws ServletException {
String name = config.getInitParameter(“user”);
String pass = config.getInitParameter(“password”);
String servletName = config.getServletName();
System.out.println(“name值为:”+name);
System.out.println(“pass值为:”+pass);
System.out.println(“servletName值为:”+servletName);
Enumeration eums = config.getInitParameterNames();
while(eums.hasMoreElements()) {
String eum = eums.nextElement();
System.out.println(eum+“对应值为:”+config.getInitParameter(eum));
}
}
/**
- @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
- @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
使用web.xml的方式进行配置:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns=“http://xmlns.jcp.org/xml/ns/javaee”
xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd”
id=“WebApp_ID” version=“4.0”>
ClassDemo03
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
myservlet
com.ServletConfigDemo
user
qst
password
123
myservlet
/myservlet
运行ServletConfigDemo
控制台
最后
小编这些年深知大多数初中级工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Java全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你需要这些资料,⬅专栏获取
最后
小编这些年深知大多数初中级工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Java全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-itF9IBC1-1719510589202)]
[外链图片转存中…(img-Qr3a48ka-1719510589204)]
[外链图片转存中…(img-IWFNmrMX-1719510589206)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你需要这些资料,⬅专栏获取