What is b/s architecture
The client uses the browser, the server uses a Web browser, and the client communicates with the servers using the HTTP protocol.
2) Advantages
① clients do not need to be installed separately (because of the use of a browser); The C/s architecture is cumbersome to install each client separately, and once the client version changes, it needs to be installed again.
② development is relatively simple; the C/s architecture requires that we write the corresponding communication processing modules and custom protocols separately on the client and server side, while the B/s architecture uses the standard HTTP protocol (i.e. no need for custom protocols), and the browser and Web server already contain the corresponding communication module.
What is Tomcat
Tomcat itself is a servlet container, a program that can provide a servlet runtime environment, but Tomcat also provides all the functionality that the Web server has, so we also call Tomcat a Web server. The default port for Tomcat is 8080.
Common Errors and Solutions
1) 404: Is a status code (is a three-digit number, sent by the server to the browser, tell the browser whether the request is handled correctly), 404 means: The server according to the request resource path, cannot find the corresponding resource.
Resolution: ① Check that your request address is correct based on Http://ip:port/appname/url-pattern.
② carefully check web. XML, pay particular attention to whether Servlet-name is consistent.
2) 500: Server processing error, generally because the program run error.
Workaround: ① Check the code of the program, for example: whether to inherit.
② Check Web. XML, the class name should be filled in correctly.
3) 405: The server cannot find the corresponding service method.
Workaround: Check the Service method's signature (method name, parameter type, return type, exception type).
What is the HTTP protocol
HTTP (Hypertext Transport Protocol) is a Hypertext Transfer protocol. is an application-layer protocol that defines the process and data format of communication between a browser (or other client) and a Web server.
The process of communication
Step1: The browser establishes a connection (Socket) to the Web server.
Step2: The browser will package the request data (Request packet) and send it to the Web server.
The Step3:web server packs the processing results (response packets) and then sends them to the browser.
The Step4:web server closes the connection.
Get request vs. POST request
1) in which case, the browser uses the Get method to send the request:
① Enter an address directly in the browser address. ② Click on the link address.
③ Form Default Submission method: <form method= "Get (default)/post" >.
2) in which case, the browser will send a request using the Post method:
① set the Method property value for the form to "post".
3) Features of GET request:
The ①get request adds the request parameter to the request resource path, because the requested row holds a limited amount of data (that is, the maximum number of bytes in the address bar), so a GET request can only submit a small number of data.
②get requests will display the request parameters in the browser address bar, which is unsafe (for example, the router will record the entire address).
4) features of POST request:
The ①post request adds the request parameters to the entity content, so a large amount of data can be submitted.
The ②post request does not display the request parameters in the browser address bar, which is relatively secure. However, the POST request does not encrypt the request parameters. Encrypted with the HTTPS protocol.
How to read request parameters
1) Method One: String Request.getparameter (string paraname);
① if the paraname (that is, the parameter name) does not match the actual parameter name, you get null (no error).
② when submitting data using a form, if the user does not fill in any values, an empty string "" is obtained.
2) Method Two: string[] request.getparametervalues (String paraname);
① Use this method when there are multiple parameters and the names are the same. For example:? city=bj&city=cs&city=wh
U Note: The Getparametervalues method can also be used in cases where there is only one parameter.
Accessing the database (MYSQL)
1) Use MySQL database
① login mysql:mysql-uroot;//Log in to MySQL, using root user rights
② View all current databases: show databases;
③ Create a new database: Create databases Db_chang default character set utf8;//creating Db_chang database, default is UTF8 encoding set (cannot write minus sign-)
④ using a database: use Db_chang;
⑤ See which tables are in the current database: show tables; drop TABLE tablname;//Delete list
Exception: IllegalStateException
The following error can be ignored, because the Tomcat hot deployment caused by the re-deployment of the manual can be
How Servlets output Chinese
Need to call: Response.setcontenttype ("Text/html;charset=utf-8"), where Charset=utf-8 says:
1) to specify the encoding format, as long as the support of Chinese, such as can also be set to CHARSET=GBK.
2) function Two: ① generates a CONTENT-TYPE message header that tells the browser which data type and encoding format to return.
When the ② server is output, it is encoded using the specified encoding format.
If the form has Chinese parameter values, you also need to pay attention to the coding problem
Because, when the form is submitted, the browser will encode the data in the form (encoded when the form is opened), and the server will use Iso-8859-1 to decode it by default, so it will cause garbled problems.
1) Solution One:
Step1: First ensure that the page on which the form is located is opened in the specified encoding format. That
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/> is already a specification (analog Content-type message header, Tells the browser that the data type and encoding format are being parsed.
Step2: Call request.setcharacterencoding ("Utf-8"), meaning to tell the server to decode using the specified encoding format.
U Note: This method can only be used for "POST" requests! Note the order in which the code is placed,
Before the Request.getparameter () method.
2) Solution Two:
Step1: The first step in the same way.
Step2: Use new String (Str.getbytes ("iso-8859-1"), "Utf-8");
How to redirect
Use Response.sendredirect (String URL);
Two features
1) Redirect The address is arbitrary (the premise to exist otherwise reported 404).
2) After redirection, the address of the browser address bar becomes the redirect address.
Servlet Learning Notes