ServletConfig 类的三大作用
1、可以获取 Servlet 程序的别名 servlet-name 的值
2、获取初始化参数 init-param
3、获取 ServletContext 对象
package top.qaqaq.P130;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @author RichieZhang
* @create 2022-12-04 下午 6:28
*/
public class HelloServlet4 implements Servlet {
public HelloServlet4() {
System.out.println("1 构造器方法");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2 init初始化方法");
// 1、可以获取 Servlet 程序的别名 servlet-name 的值
System.out.println("HelloServlet4程序的别名是:" + servletConfig.getServletName());
// 2、获取初始化参数 init-param
System.out.println("初始化参数username的值是:" + servletConfig.getInitParameter("username"));
System.out.println("初始化参数url的值是:" + servletConfig.getInitParameter("url"));
// 3、获取 ServletContext 对象
System.out.println(servletConfig.getServletContext());
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* service方法是专门用来处理请求和响应的
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("3 service === Hello Servlet 被访问了");
// 类型转换(因为它有getMethod()方法)
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// 获取请求方式
String method = httpServletRequest.getMethod();
// System.out.println(method);
if ("GET".equals(method)){
doGet();
} else {
doPost();
}
}
/**
* 做get请求的操作
*/
public void doGet(){
System.out.println("get请求");
System.out.println("get请求");
}
/**
* 做post请求的操作
*/
public void doPost(){
System.out.println("post请求");
System.out.println("post请求");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("4 destory销毁方法");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<!-- servlet标签给Tomcat配置Servlet程序 -->
<servlet>
<!--servlet-name标签 Servlet程序起一个别名(一般是类名)-->
<servlet-name>HelloServlet</servlet-name>
<!--servlet-class是Servlet程序的全类名)-->
<servlet-class>top.qaqaq.P122.HelloServlet</servlet-class>
</servlet>
<!-- servlet-mapping标签给servlet程序配置访问地址 -->
<servlet-mapping>
<!-- servlet-name标签的作用是告诉服务器,我当前配置的地址给哪个servlet程序使用 -->
<servlet-name>HelloServlet</servlet-name>
<!-- url-pattern标签配置访问地址
/ 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径
/hello 表示地址为:http://ip:port/工程路径/hello
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HelloServlet2</servlet-name>
<servlet-class>top.qaqaq.P127.HelloServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet2</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HelloServlet3</servlet-name>
<servlet-class>top.qaqaq.P128.HelloServlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet3</servlet-name>
<url-pattern>/hello3</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HelloServlet4</servlet-name>
<servlet-class>top.qaqaq.P130.HelloServlet4</servlet-class>
<!-- init-param是初始化参数 -->
<init-param>
<!-- 是参数名 -->
<param-name>username</param-name>
<!-- 是参数值 -->
<param-value>root</param-value>
</init-param>
<!-- init-param是初始化参数 -->
<init-param>
<!-- 是参数名 -->
<param-name>url</param-name>
<!-- 是参数值 -->
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet4</servlet-name>
<url-pattern>/hello4</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HelloServlet5</servlet-name>
<servlet-class>top.qaqaq.P131.HelloServlet5</servlet-class>
<!-- init-param是初始化参数 -->
<init-param>
<!-- 是参数名 -->
<param-name>username</param-name>
<!-- 是参数值 -->
<param-value>root2</param-value>
</init-param>
<!-- init-param是初始化参数 -->
<init-param>
<!-- 是参数名 -->
<param-name>url</param-name>
<!-- 是参数值 -->
<param-value>jdbc:mysql://localhost:3306/test2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet5</servlet-name>
<url-pattern>/hello5</url-pattern>
</servlet-mapping>
</web-app>