Servlet related knowledge 3
Servletconfig interface:
When the container calls the init () method, this object is created in advance and passed to the servlet object as a parameter.
This interface defines how to get servlet configuration parameters
String getinitparameter (string name );
You can configure servlet initialization parameter values in Web. xml:
<Servlet>
<Servlet-Name> helloword </servlet-Name>
<Servlet-class>/servlet/helloword </servlet-class>
<Init-param>
<Param-Name> admin </param-Name>
<Param-value> wang_sir </param-value>
</Init-param>
</Servlet>
1. Form Processing
(1) Get the data in the form
String getparameter (string paraname); // note that the parameter name does not exist and null is returned.
String [] getparametervalues (string paramname); // if multiple parameters have the same name, use the following method
Map getparametermap (); // return key-value pairs. Each key-value pair corresponds to a parameter name and value. The parameter value is of the string [] type.
(2) problem handling in Chinese form
The browser encodes the data (non-ASCII characters) in the form. The encoding used is the encoding used to open the page where the form is located.
Step 1: Use <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
Specifies the encoding when the page is opened.
Step 2 in Servlet
A, request. setcharacterencoding ("UTF-8"); // specifies the encoding format used when the request object is encoded
B. the following method is used to output data to the browser:
----- Response. setcharacterencoding ("UTF-8 ");
| ---- Response. setcontenttype ("text/html ");
|-Response. setcontenttype ("text/html; character = UTF-8 ");
Purpose 1: Set the encoding to UTF-8 when entering data into the stream;
Purpose 2: Send a message header to the browser, telling the browser that the data currently returned is opened in UTF-8 encoding.
Step 3 if you want to save the data to the database
A. when creating a database, use create database dbname default Character Set utf8;
B. The JDBC driver specifies the database encoding, which tells the driver what encoding is used for saving data in the current database.
JDBC: mysql: // localhost: 3306/dbname? Useunicode = true & characterencoding = utf8
2. jsp
(1) What is it?
Java Server Page: a server-side dynamic page generation technology developed by Sun. Its main components are HTML elements and a small amount of Java code.
It is too cumbersome to directly use servlet to generate dynamic pages. It is more convenient to use JSP to generate dynamic pages.
JSP files are suffixed with. jsp and can be run without compilation or packaging.
(2) How to Run JSP
When accessing a. jsp file, the JSP Engine (understood as a specific module in the servlet container) will
Convert to A. Java file (The. Java file is a servlet), create its instance, and call its service () method.
(3) Composition of JSP files
A. Handwriting of HTML elements (including HTML tags, content, CSS, and JavaScript)
B. Java code
1) Java code snippets
<% Arbitrary Java code %>
2) JSP expressions
<% = %>
3) JSP implicit object
Request Response out session
4) JSP commands
Tell the JSP Engine to perform some additional processing when converting a. jsp file to a. Java file.
<% @ Command name attribute = Value %>
For example:
(4) how to convert a. jsp file to. Java (that is, how to convert it to servlet ).
A. <%> the Java code ----> is directly put into the service () method.
B. In the service () method, use out. Write ();
// The write (null) method outputs "";
// The println (null) method outputs null.
// Generally, if the result is output to the client, you need to write null as "" output
3. Forwarding
1) What is forwarding?
Within the same application, one component submits unfinished tasks to another component for completion.
Generally, a servlet completes the business logic processing and submits the data presentation task to a JSP.
2) Programming
Step 1 determine the object to request
Request. setattribute (string name, object value );
Get Helper Objects
Request. getattribute (string name, object value );
Requestdispatcher RD = request. getrequestdispatcher ("another component ");
Step 2 RD. Forward (request, response );
3) Features
A. One thing is not finished;
B. The forwarding destination must be within the same application;
C. The address in the address bar of the browser does not change during forwarding;
D. The components involved in forwarding can share the same request and response object.
Forwarding considerations:
Do not have any output before forwarding. Otherwise, an error will occur.
Forwarding clears the cache.