本文整理汇总了Java中javax.servlet.ServletContext.getServletContextName方法的典型用法代码示例。如果您正苦于以下问题:Java ServletContext.getServletContextName方法的具体用法?Java ServletContext.getServletContextName怎么用?Java ServletContext.getServletContextName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletContext
的用法示例。
在下文中一共展示了ServletContext.getServletContextName方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doGet
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
//get the response writer
PrintWriter out = response.getWriter();
//initialise the servlet context
ServletContext context = request.getServletContext();
//get the context name
String lname = context.getServletContextName();
//initialise the parameters
String a = context.getInitParameter("a");
String b = context.getInitParameter("b");
String c = context.getInitParameter("c");
String d = context.getInitParameter("d");
Enumeration<String> e1 = context.getInitParameterNames();
String param_Names = "";
//appending attr names to a string variable
while(e1.hasMoreElements()) {
param_Names = param_Names + e1.nextElement()+"<br>";
}
//set the attribute
context.setAttribute("x", "XXX");
context.setAttribute("y", "YYY");
context.setAttribute("z", "ZZZ");
String x = (String)context.getAttribute("x");
String y = (String)context.getAttribute("y");
String z = (String)context.getAttribute("z");
//get the attribute names
Enumeration<String> e2 = context.getAttributeNames();
String attr_Names = "";
//appending attr names to a string variable
while(e2.hasMoreElements()) {
attr_Names = attr_Names + e2.nextElement()+"<br>";
}//while close
//html code to display
out.println("<html>");
out.println("<body>");
out.println("<h3>Servlet Context Details<h3>");
out.println("<table border='1'>");
out.println("<tr><td colspan='2'>Parameters Details</td></tr>");
out.println("<tr><td>a</td><td>"+a+"</td></tr>");
out.println("<tr><td>b</td><td>"+b+"</td></tr>");
out.println("<tr><td>c</td><td>"+c+"</td></tr>");
out.println("<tr><td>d</td><td>"+d+"</td></tr>");
out.println("<tr><td>Parameter Names</td><td>"+param_Names+"</td></tr>");
out.println("<tr><td colspan='2'>Attributes Details</td></tr>");
out.println("<tr><td>x</td><td>"+x+"</td></tr>");
out.println("<tr><td>y</td><td>"+y+"</td></tr>");
out.println("<tr><td>z</td><td>"+z+"</td></tr>");
out.println("<tr><td>Attributes Names</td><td>"+attr_Names+"</td></tr>");
out.println("<tr><td>Logical Name</td><td>"+lname+"</td></tr>");
out.println("<tr><td>Foreign Context</td><td>"+context.getContext("/app8")+"</td></tr>");
out.println("</table></body></html>");
}