Java Web Learning Note 8

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.