作者:叁念
标签为servlet配置一些初始化参数。
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Demodisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>MyServletservlet-name>
<servlet-class>com.cn.MyServletservlet-class>
<init-param>
<param-name>charsetparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>nameparam-name>
<param-value>sannianparam-value>
init-param>
<init-param>
<param-name>passwordparam-name>
<param-value>123param-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>MyServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
package com.cn;
import java.io.IOException;
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;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取ServletConfig对象
ServletConfig config = this.getServletConfig();
// 取出初始化信息
System.out.println(config.getServletName());
System.out.println(config.getInitParameter("name"));
Enumeration enumeration = config.getInitParameterNames();
while (enumeration.hasMoreElements()) {
String nextElement = enumeration.nextElement();
String parameter = config.getInitParameter(nextElement);
System.out.println(parameter);
}
}
}
访问:http://localhost:8080/项目名/
效果如下:
编写web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Demodisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<context-param>
<param-name>cpparam-name>
<param-value>this is valueparam-value>
context-param>
web-app>
书写Servlet
package com.cn;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
String parameter = context.getInitParameter("cp");
System.out.println(parameter);
}
}
效果如下
// String path = context.getRealPath(相对于web应用的相对路径)
String realPath = this.getServletConfig().getServletContext().getRealPath("/");
System.out.println(realPath);
//打印如下 D:\codingsoft\tomcat\wtpwebapps\Demo\
创建ServletContextDemo设置属性值
package com.cn;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ServletContextDemo")
public class ServletContextDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.getServletContext().setAttribute("binggo", "666");
}
}
创建ServletContextDemo1获取属性值
package com.cn;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ServletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object attribute = this.getServletContext().getAttribute("binggo");
System.out.println(attribute.toString());
}
}
访问方式:顺序,先存后取,否则会空指针异常
http://localhost:8080/Demo/ServletContextDemo
http://localhost:8080/Demo/ServletContextDemo1
效果:打印 666
注意:ServletContext域的作用范围:整个web应用
package com.cn;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ServletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/MyServlet");
dispatcher.forward(request, response);
}
}
源码:
链接: https://pan.baidu.com/s/1Y-9f_OytfC5Q0G8RtA8LnQ 密码: gc7y