Flask接口签名sign原理与实例代码浅析
195
2023-08-06
JavaEE中关于ServletConfig的小结
在Servlet的配置文件中,可以使用一个或多个
示例代码如下:
复制代码 代码如下:
package com.yyz.servletconfig;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo1 extends HttpServlet {
ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取指定的初始化参数
String value = config.getInitParameter("xxx");
response.getOutputStream().write(value.getBytes());
//获取所有的初始化参数
Enumeration e = cofig.getInitParameterNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
value = config.getInitParameter(name);
response.getOutputStream().write((name+"="+value+"
").getBytes());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
相应的web.xml如下:
复制代码 代码如下:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> &http://nbsp;
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
&http://nbsp;
测试结果如下:
在上面的代码中,ServletConfigDemo1对象中有一个ServletConfig对象,其实这是不必要的。因为ServletConfigDemo1继承了HttpServlet,HttpServlet又继承了GenericServlet 。GenericServlet 已经在内部维护了一个ServletConfig对象。相关实现如下:
复制代码 代码如下:
public abstract class GenericServlet
implements Servlet, ServletConfig, java.io.Serializable
{
… …
private transient ServletConfig config;
public ServletConfig getServletConfig() {
return config;
}
}
因而我们可以通过我们写的Servlet对象的getServletConfig()方法直接拿到ServletConfig对象,示例代码如下:
复制代码 代码如下:
package com.yyz.servletconfig;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = this.getServletConfig().getInitParameter("name");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
&nbsAyEVhbCTp; throws ServletException, IOException {
doGet(request, response);
}
}
web.xml文件:
复制代码 代码如下:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~