Javaweb Learning Path Summary, ServletContext, Servletresponse, ServletRequest (3)

Source: Internet
Author: User

1. Path Summary    1. Java Project 1 File File = new file ("");            File.getabsolutepath ();                * Using the Java command, the output path is the drive letter where the current Java command stays * F:\workspaces\20141123\demo\bin * using the MyEclipse or Eclipse runtime          * F:\workspaces\20141123\demo 2 File File = new file ("/");            File.getabsolutepath ();            * Get current drive letter * f:3 URL url = Hello.class.getClassLoader (). GetResource (""); * Get path, use ClassLoader * f:/workspaces/20141123/demo/bin/* Current class is loaded, root directory 4 URL url = Hello            . Class.getclassloader (). GetResource ("/"); * Cannot use * NULL 2, Web project 1, get file through ServletContext, Webroot file under root ServletContext sc = this.g            Etservletcontext ();            * Get the actual path * Sc.getrealpath ("/1.html");             * Get URL * url url = sc.getresource ("/1.html");                * Get Stream "* *" * inputstream is = Sc.getresourceasstream ("/1.html"); IfTo obtain webroot/page/abc/2.html, InputStream is = Sc.getresourceasstream ("/page/abc/2.html");                    2, the relative path of the web * premise: relative to the current page 1.html * Use Category: 1, ABC: The name of the directory or servlet with the current page sibling  *<ahref= "c/c.html">C.html</a><BR>2,/abc: relative to the Web site,%tomcat%/webapps/*<ahref= "/day06_web/b/c/c.html">C.html</a><BR>3./ABC: Current directory, same as first case *<ahref= "./c/c.html">C.html</a><BR>4 、.. /ABC: Last Catalog *<ahref=".. /b/c/c.html ">C.html</a><BR>Summary: Operation process Current page: http://localhost:8080/day06_web/b/b.html target page: http://localhost:8080/day 06_web/b/c/c.html/--WebApps--http://localhost:80802, ServletContext* A description of the context of the current Web project (a description of all the contents of the current Web project), created by Tomcat at startup, and destroyed when Tomcat shuts down.        Plainly, that is to say, you can manage files under Webroot, such as reading files under Webroot * servlet-> init (servletconfig)--config.getservletcontext (); * The project where the current servlet is located * Tomcat creates a separate zone for each Web project that is used to manage the entire project. This area becomes ServletContext * Manage current Project "* * * * *" ServletContext sc = This.getservletcontext ();//source code is actually Config.getservletcontex T () * Obtain the actual path, require/start * Sc.getrealpath ("/1.html"),//Get the absolute path of 1.html under Webroot root * operation Data * A DD Set Get Remove delete * Setattribute/getattribute/removeattribute * ServletContext object shared for all Servlets Data * Configure content for the current Web project "* *" * Configuration file Location: web. XML * Configuration content, such as configuration database connection string<Context-param>                    <Param-name>Username</Param-name>                    <Param-value>Root</Param-value>                </Context-param>* How to read//get servletconetxt Servletconetxt sc = This.getservletcontext (); Get all the configuration information names<Context-param><Param-name>Username</Param-name>Enumeration<String>names = Sc.getinitparameternames ();                while (Names.hasmoreelements ()) {String value = sc.getinitparameter (name);            SYSTEM.OUT.PRINTLN ("Context:" +name+ ":" +value); * Context Root * Current ServletContext the root of the Web project * Tomcat-to webapps/webname/* myeclipse -->webname/webroot/3, Servletresponse    * The server responds to the browser, and all data that needs to be sent to the browser is stored on this object.        * Send data, use the flow operation, the required data, stored in the specified stream, the data will be displayed in the browser httpservletresponse response;        * Character Stream * Response.getwriter ();        * Byte stream * Response.getoutputstream (); * When using Getoutputstream, you cannot use Getwriter * Getoutputstream () have already been called for this response * when using G    Etwriter, you cannot use Getoutputstream * getwriter () have already been called for this response * Summary: Two streams can only use one * Byte stream: Getoutputstream, generally used in the program with copy function, etc. * Send Chinese * cannot send * out.print (data);                Unable to send Chinese data * exception information: Java.io.CharConversionException not an ISO 8859-1 character: Medium * can be sent    * Out.write (Data.getbytes ("UTF-8"));    * Character stream: Getwriter, usually send data content in the program * Send Chinese * out.println ("Chinese");        * garbled "* * * *" * Response.setcontenttype ("Text/html;charset=utf-8");  * Notify Tomcat and browser to send data encoding * NOTE: * When setting the encoding, it must be placed before the output statement is required, it is recommended to place the first line in Doget or Dopost            4, ServletRequest      * Browser requests to the server (where the data is stored when the browser sends the data to the server) * Request method: Get and POST * Get: Data sent, appended to the requested URL * POST: Data sent in the HTTP request body * Browser Send data * Form form, * Method property: Specified Request method * Action Property: Program path to receive data * Server obtains data sent by browser * Get single number            According to * Request.getparameter ("username");            When getting input type= "Radio", get the selected value * Get a set of data * Request.getparametervalues ("Love");            When getting input type= "checkbox", get the selected value * Handle Chinese garbled * request.setcharacterencoding ("UTF-8"); * Note: * This method is only valid for post requests, get needs to be processed separately * need to be placed before the data is obtained, it is recommended to place the first line the method of handling doget is given to Dopost.            Request.setcharacterencoding ("UTF-8") in Dopost;            String userName = Request.getparameter ("UserName"); If you do not want to get it in doget, you can use the following code: The first way: string userName = new String (Request.getparameter ("UserName"). GetByte            S ("Iso-8859-1"), "UTF-8"); Second way://Get parameter string query = RequEst.getquerystring ();            Decode String q = urldecode.decode (query, "UTF-8"); Results: Q:uername= Chinese

Javaweb Learning Path Summary, ServletContext, Servletresponse, ServletRequest (3)

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.