当前位置: 技术问答>java相关
JBuider中写Servlet如何提取初始数据?
来源: 互联网 发布时间:2015-05-23
本文导语: 我在web.xml中设置初始数据的,我的web.xml文件如下: initservlet servletsam.InitServlet param1 value1 param2 value2 initservlet /initser...
我在web.xml中设置初始数据的,我的web.xml文件如下:
initservlet
servletsam.InitServlet
param1
value1
param2
value2
initservlet
/initservlet
servlet的代码如下:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class InitServlet extends HttpServlet {
ServletConfig config;
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config=config;
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("Test InitServlet");
out.println("");
out.println("print all inital parameters");
Enumeration initParams=config.getInitParameterNames();
while(initParams.hasMoreElements())
{
String initParam=(String)initParams.nextElement();
out.println(initParam);
System.out.println(config.getInitParameter(initParam));
out.println(": "+config.getInitParameter(initParam));
// out.println("");
}
out.println("");
}
/**Clean up resources*/
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doGet(request,response);
}
}
为什么不能显示初始数据?只能显示print all inital parameters?
求教
initservlet
servletsam.InitServlet
param1
value1
param2
value2
initservlet
/initservlet
servlet的代码如下:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class InitServlet extends HttpServlet {
ServletConfig config;
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config=config;
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("Test InitServlet");
out.println("");
out.println("print all inital parameters");
Enumeration initParams=config.getInitParameterNames();
while(initParams.hasMoreElements())
{
String initParam=(String)initParams.nextElement();
out.println(initParam);
System.out.println(config.getInitParameter(initParam));
out.println(": "+config.getInitParameter(initParam));
// out.println("");
}
out.println("");
}
/**Clean up resources*/
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doGet(request,response);
}
}
为什么不能显示初始数据?只能显示print all inital parameters?
求教
|
// 取参数
String param1 = getServletContext().getInitParameter( "param1" );
String param1 = getServletContext().getInitParameter( "param1" );
|
ServletContext context = getConfig().getServletContext();
Enumeration names = context.getInitParamerNames();
String initParam;
while(names.hasMoreElements())
{
initParam=(String)context.getInitParameter((String)names.nextElement());
}
Enumeration names = context.getInitParamerNames();
String initParam;
while(names.hasMoreElements())
{
initParam=(String)context.getInitParameter((String)names.nextElement());
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。