A) What does the HttpServletRequest class do? The HttpServletRequest class does what it does. Every time a request comes in. The Tomcat server then wraps the requested protocol content into an HttpServletRequest object. We get the Request information primarily from the Request class. It represents all the requested information.
GetRequestURL () The absolute path to the request (also called the uniform resource locator). GetRemoteHost () Obtains the IP address of the client Localhost gets 0:0:0:0:0:0:0:0:1 127.0.0.1 gets 127.0.0.1 getHeader() gets the request header getParameter() gets the request parameters GetParameterValues () Obtains request parameters (multiple values) getMethod() Method of obtaining request parameters GET or POST Domain object setAttribute(key, value); Save the data getAttribute(key); To get the data
GetRequestDispatcher () gets the request forwarding object getCookies() gets the Cookie object getSession() gets the Session object
Reqeust common API:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// i.getrequesturi () obtain request request resource address system.out.println (request.getrequesturi ()); // ii.getrequestUrl () The absolute path to the request system.out.println (request.getrequesturl ()); // iii.getremotehost () obtains the IP address of the client system.out.println (request.getremotehost ()); System.out.println(request.getheader (" user-agent "))); System.out.println(request.getHeader("Host")); // vii.getMethod() GET or POST system.out.println (request.getMethod()); }Copy the code
C) How to get the request parameters (important) page form:
The < body > < form action = "http://localhost:8080/day07/parameterServlet" method = "get" > username: <input type="text" name="username" /><br/> Password: <input type="password" name="password" <input type="checkbox" name="hobby" value="cpp">C++ <input type="checkbox" name="hobby" value="C">C <input type="checkbox" name="hobby" value="VB">Visual Basic <input type="checkbox" name="hobby" value="js">JavaScript <br/> <input type="submit" /> </form> </body>Copy the code
The code for the Servlet program:
public class ParameterServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username"); IOException {String username = request.getParameter("username"); String password = request.getParameter("password"); / / get interests String [] hobbies = request. GetParameterValues (" hobby "); System.out.println(" username: "+ username); System.out.println(" password: "+ password); // arrays. asList converts Arrays to their corresponding List if (hobbies! = null) {system.out.println (" hobbies: "+ array.aslist (hobbies)); }}}Copy the code
D) Chinese garbled code for GET request
1. Encode the received request parameters with ISO-8859-1. Decode them with UTF-8
String username = request.getParameter("username"); Byte [] bytes = username. GetBytes ("iso-8859-1"); Username = new String(bytes, "utF-8 ");Copy the code
Option 2: Go to the Tomcat server configuration file server. XML, go to the Connector label, add URIEncoding= “UTF-8”
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
Copy the code
E) The first solution to Chinese garble in POST request (not recommended) :
Byte [] bytes = username. GetBytes ("iso-8859-1"); Username = new String(bytes, "utF-8 ");Copy the code
The second option (recommended) :
/ / solution to post Chinese garbled ultimate / / must be in effective request before invoking the getParameter. SetCharacterEncoding (" utf-8 ");Copy the code
F) Forwarding requests (used with JSP)
G) Function of Base tag
H) Relative and absolute paths in the Web
Relative path:
./ Current directory
… / Upper directory
Resource name is equivalent to./ Resource name./ can be omitted
Absolute path:
http://ip:port/ project name/resource name
In actual project development, simple use of relative paths is not allowed!!
In real project development, you can only use absolute paths or base+ relative
I) Different meanings of/slashes in the Web
The slash/is an absolute path. / on the HTML page, the browser parses it to: http://ip:prot/ / on the server code. Said: http://ip:port/ Project name/Configure url-pattern /hello ====>>>> http://ip:port/ Project name /hello in web. XML Request to forward the request. GetRequestDispatcher (“/a/b/c. HTML “) said the http://ip:port/ project name/a/b/c. the TML servletContext. GetRealPath (“/”); = = = = = > > > > > > said to http://ip:prot/ project name/server code in a special case: response. SendRedirect (“/”); =====>>>> indicates the port number http://ip:prot/
6. HttpServletResponse class
A) The HttpServletResponse class
HttpServletResponse class, representing the response. The HTTP protocol for all responses can be set using the HttpServletResponse class.
Each time a Request comes in, a Request object is created, and a Response object is created. Tomcat server is responsible for creation.
B) Description of the two output streams.
The response has two streams, a character stream and a byte stream.
Response.getoutputstream () byte stream file to download
Response.getwriter () returns string information (most frequently used)
If you use a byte stream, you can’t use a character stream,
If you use a stream of characters, you cannot use a byte stream.
The following exception occurs when two streams are used together:
C) How to send data back to the client
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, PrintWriter writer = response.getwriter (); IOException {response.getwriter (); Writer. write("this is the content of response!! "); ); }Copy the code
D) The first solution of garbled response (not recommended) :
/ / set the character set of the response to utf-8, set up the servers support Chinese output response. SetCharacterEncoding (" utf-8 "); / / ISO - 8859-1 is the default character set, it does not support Chinese System. Out. The println (response) getCharacterEncoding ()); // Set the response header, content-type, // text/ HTML; Indicates that HTML content is returned. Responsetheader (" content-type ", "text/ HTML; charset= utF-8; charset=UTF-8");Copy the code
The second option is (recommended) :
SetContentType ("text/ HTML; ") response.setContentType("text/ HTML; charset=UTF-8");Copy the code
E) How to set the response header and response status code
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {system.out.println (" Response1"); IOException {system.out.println (" Response1"); // Set response status code 302 Response.setStatus (302); / / set response headers response. SetHeader (" Location ", "http://localhost:8080/day07/response2"); }Copy the code
F) Request redirection
/ / is also request redirection (recommended) response. SendRedirect (" http://localhost:8080/day07/response2 ");Copy the code
Request redirection features:
Comparison of request forwarding and redirection