Context parameters (Context-param)
Since Init-param is configured in the <servlet> tag, it can only be read by this servlet, so it is not a global parameter and cannot be read by other servlets.
Context-param is called the context parameter and can be read by all Sevlet. Context Tags Usage <context-param> configuration
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <Context-param> <Param-name>Upload Folder</Param-name> <Param-value>Attachment</Param-value> </Context-param> <Context-param> <Param-name>Allowed file type</Param-name> <Param-value>. gif,. jpg,. bmp</Param-value> </Context-param> <servlet> <Servlet-name>Contextparamservlet</Servlet-name> <Servlet-class>Com.helloxr.servlet.ContextParamServlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Contextparamservlet</Servlet-name> <Url-pattern>/servlet/contextparamservlet</Url-pattern> </servlet-mapping>
PackageCom.helloxr.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classContextparamservletextendsHttpServlet {Private Static Final LongSerialversionuid = 65464645464L; Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("<HEAD><TITLE> Read context Parameters </TITLE></HEAD>"); Out.println ("<BODY>"); Out.println ("<link rel= ' stylesheet ' type= ' text/css ' href= '. /css/style.css ' > "); Out.println ("<div align=center><br/>"); Out.println ("<fieldset style= ' width:90% ' ><legend> all contextual parameters </legend><br/>"); ServletContext ServletContext=getservletconfig (). Getservletcontext (); String Uploadfolder= Servletcontext.getinitparameter ("Upload folder"); String Allowedfiletype= Servletcontext.getinitparameter ("Allowed file type"); Out.println ("<div class= ' line ' >"); Out.println ("<div align= ' left ' class= ' leftdiv ' > upload folder </div>"); Out.println ("<div align= ' left ' class= ' Rightdiv ' >" + uploadfolder + "</div>"); Out.println ("</div>"); Out.println ("<div class= ' line ' >"); Out.println ("<div align= ' left ' class= ' leftdiv ' > actual disk path </div>"); Out.println ("<div align= ' left ' class= ' Rightdiv ' >" + servletcontext.getrealpath (uploadfolder) + "</div>"); Out.println ("</div>"); Out.println ("<div class= ' line ' >"); Out.println ("<div align= ' left ' class= ' leftdiv ' > allowed file types to be uploaded </div>"); Out.println ("<div align= ' left ' class= ' Rightdiv ' >" + allowedfiletype + "</div>"); Out.println ("</div>"); Out.println ("</fieldset></div>"); Out.println ("</BODY>"); Out.println ("</HTML>"); Out.flush (); Out.close (); }}
Browsing effect:
Java Web Learning Note 8