context.getInitParameter("") 和 config.getInitParameter("") 总是返回 null
context.getInitParameter("") and config.getInitParameter("") always returning null
我是 servlet 的初学者,JSP 我已经尽力获取值,但我得到的是 null,欢迎任何帮助:
这是使用 ServletConfig 和 ServletContext 从 web.xml
获取参数值的基本代码
Servlet_Ex_4.java(- servlet):
@WebServlet("/Servlet_Ex_4")
public class Servlet_Ex_4 extends HttpServlet {
ServletContext context;
ServletConfig config;
String appUser ;
String Database ;
@Override
public void init() {
ServletContext context =getServletContext();
appUser = context.getInitParameter("appUser");
ServletConfig config =getServletConfig();
Database = config.getInitParameter("Database");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//get ServletContext object.
//get context parameter from ServletContext object.
out.print("<h1>Application User: " + appUser + "</h1>");
out.print("<h1>Database: " + Database + "</h1>");
out.close();
}
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"?>
<element>
<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">
<display-name>Servlet_Exercise_4</display-name>
<servlet>
<servlet-name>Servlet_4</servlet-name>
<servlet-class>Servlet_Ex_4</servlet-class>
<context-param>
<param-name>Database</param-name>
<param-value>Oracle</param-value>
</context-param>
</servlet>
<context-param>
<param-name>appUser</param-name>
<param-value>jai</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Servlet_4</servlet-name>
<url-pattern>/Servlet_Ex_4</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>
</element>
标签 必须从 web.xml 中删除,因为在这种情况下它不是 XML 定义的一部分
不得在 内部使用,必须将其替换为 才能从 ServletConfig 对象访问参数。这就是 OP 获取空值的原因。
将对参数“appUser”起作用,可以使用 ServletContext 对象访问它。
并且将注释与 XML 混合用于相同类型的配置可能会导致不良影响。因此,@WebServlet 应该从源代码中删除,因为大部分配置是使用 XML.
完成的
所以最终 web.xml 应该像下面这样(从 Servlet_Ex_4 class 中删除了 @WebServlet 注释)
<?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">
<display-name>ServletParams</display-name>
<servlet>
<servlet-name>Servlet_4</servlet-name>
<servlet-class>Servlet_Ex_4</servlet-class>
<init-param>
<param-name>Database</param-name>
<param-value>Oracle</param-value>
</init-param>
</servlet>
<context-param>
<param-name>appUser</param-name>
<param-value>jai</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Servlet_4</servlet-name>
<url-pattern>/Servlet_Ex_4</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
我是 servlet 的初学者,JSP 我已经尽力获取值,但我得到的是 null,欢迎任何帮助:
这是使用 ServletConfig 和 ServletContext 从 web.xml
获取参数值的基本代码Servlet_Ex_4.java(- servlet):
@WebServlet("/Servlet_Ex_4")
public class Servlet_Ex_4 extends HttpServlet {
ServletContext context;
ServletConfig config;
String appUser ;
String Database ;
@Override
public void init() {
ServletContext context =getServletContext();
appUser = context.getInitParameter("appUser");
ServletConfig config =getServletConfig();
Database = config.getInitParameter("Database");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//get ServletContext object.
//get context parameter from ServletContext object.
out.print("<h1>Application User: " + appUser + "</h1>");
out.print("<h1>Database: " + Database + "</h1>");
out.close();
}
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"?>
<element>
<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">
<display-name>Servlet_Exercise_4</display-name>
<servlet>
<servlet-name>Servlet_4</servlet-name>
<servlet-class>Servlet_Ex_4</servlet-class>
<context-param>
<param-name>Database</param-name>
<param-value>Oracle</param-value>
</context-param>
</servlet>
<context-param>
<param-name>appUser</param-name>
<param-value>jai</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Servlet_4</servlet-name>
<url-pattern>/Servlet_Ex_4</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>
</element>
标签
必须从 web.xml 中删除,因为在这种情况下它不是 XML 定义的一部分 不得在 内部使用,必须将其替换为 才能从 ServletConfig 对象访问参数。这就是 OP 获取空值的原因。 将对参数“appUser”起作用,可以使用 ServletContext 对象访问它。 并且将注释与 XML 混合用于相同类型的配置可能会导致不良影响。因此,@WebServlet 应该从源代码中删除,因为大部分配置是使用 XML.
完成的
所以最终 web.xml 应该像下面这样(从 Servlet_Ex_4 class 中删除了 @WebServlet 注释)
<?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">
<display-name>ServletParams</display-name>
<servlet>
<servlet-name>Servlet_4</servlet-name>
<servlet-class>Servlet_Ex_4</servlet-class>
<init-param>
<param-name>Database</param-name>
<param-value>Oracle</param-value>
</init-param>
</servlet>
<context-param>
<param-name>appUser</param-name>
<param-value>jai</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Servlet_4</servlet-name>
<url-pattern>/Servlet_Ex_4</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>