JSP内置对象-Config
1 JSP config内置对象
在JSP中,config是ServletConfig类型的内置对象。该对象可用于获取特定JSP页面的初始化参数。Web容器为每个JSP页面创建config对象。
通常,它用于从web.xml文件中获取初始化参数。
2 config内置对象的示例
2.1 编写index.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<html>
<head>
<meta charset="utf-8">
<title>一点教程网-JSP config内置对象</title>
</head>
<body>
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="提交"><br/>
</form>
</body>
</html>
2.2 编写welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>一点教程网-JSP config内置对象</title>
</head>
<body>
<%
out.print("欢迎你 "+request.getParameter("uname"));
String driver=config.getInitParameter("dname");
out.print(" 驱动名称是="+driver);
%>
</body>
</html>
2.3 配置web.xml
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>yiidian</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
<!--Web的初始化参数-->
<init-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>yiidian</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
2.4 运行测试
热门文章
优秀文章