當前位置: 首頁>>代碼示例>>Java>>正文


Java ServletContext.getServletContextName方法代碼示例

本文整理匯總了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>");

		}
 
開發者ID:pratikdimble,項目名稱:Servlet_Context_Interface,代碼行數:61,代碼來源:ContextServlet.java


注:本文中的javax.servlet.ServletContext.getServletContextName方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。