ServletConfig object
Effect
This object allows you to read the web. Initialization parameters configured in xml.
Why put parameter information on the web? XML file?
A: Make your program more flexible [change the profile web.xml without changing the program code]
Get the web. Parameter information for XML file configuration
web.xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.example.demo1.MyServlet</servlet-class> <init-param> <param-name>name</param-name> <param-value>kaico</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> </web-app>
java code
package com.example.demo1; import javax.servlet.*; import java.io.IOException; import java.util.Enumeration; public class MyServlet implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("servlet Initialized"); } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { //Getting ServletConfig objects in Servlet ServletConfig servletConfig = this.getServletConfig(); String name = servletConfig.getInitParameter("name"); //Get a value based on the name of the configuration System.out.println("servlet Visited" + name); servletResponse.getWriter().write("hello"); } @Override public String getServletInfo() { return null; } @Override public void destroy() { System.out.println("servlet Destroyed"); } }
ServletContext object
introduce
When Tomcat (or another web server) starts, a ServletContext object is created. It represents the current web site. ServletContext objects can be called domain objects (domain objects can be simply understood as a container [similar to a Map collection])
Effect
- Since ServletContext represents the current web site, all Servlets share a ServletContext object, so ServletContext enables communication between Servlets.
- ServletConfig gets the parameter information for configuring a single Servlet, and ServletContext gets the parameter information for configuring the entire web site
- Use ServletContext to read resource files from a web site
- Implement Servlet Forwarding [Not many forwards with ServletContext, mainly request forwarding]
Communication between Servlet s
The ServletContext setAttribute(String name,Object obj) method is used to communicate between Servlets. The first parameter is the keyword, and the second parameter is the object you want to store.
Code case:
Code for servlet1
//Get the ServletContext object ServletContext servletContext = this.getServletContext(); String value = "kaico"; //MyName as the keyword and value as the value stored in the domain object [type in Map collection] servletContext.setAttribute("MyName", value);
Code for servlet2
//Get the ServletContext object ServletContext servletContext = this.getServletContext(); //Get the value stored in the domain object by keyword String value = (String) servletContext.getAttribute("MyName"); System.out.println(value);
This allows servlet2 to get the information stored by servlet1, thereby enabling communication between multiple Servlets.
Get information about the web site configuration
If I want all Servlets to have access to the information to connect to the database, it's impossible on the web. Configure each Servlet in the XML file, which is too much code! And it can be very verbose and redundant.
Web. The XML file supports configuration parameter information for the entire site [which is available to all Servlet s]
<context-param> <param-name>name</param-name> <param-value>kaico</param-value> </context-param>
This configuration allows all servlets to pass through the servletContext.getInitParameter("name"); Method gets value.
Read Resource File
Previously, when reading a file, if the program and the file are in the same package name, they can be obtained directly from the file name!, The reason is simple. Previously we wrote programs that were run through JVM, but now we run through Tomcat according to the web's directory specifications. Servlet compiled class files are stored in the WEB-INFclasses folder
Get files under the java package through the servletContext object
Place pictures in Java code package location
//Get the ServletContext object ServletContext servletContext = this.getServletContext(); //Call the ServletContext method to get the stream reading the file InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/com.kaico/web/1.png");
ServletContext Gets the files in the web directory
Files are placed in a web directory and can be obtained directly from the file name
//Get the ServletContext object ServletContext servletContext = this.getServletContext(); //Call the ServletContext method to get the stream reading the file InputStream inputStream = servletContext.getResourceAsStream("com/example/demo1/docker Icon.png");
Reading Resource Files via Class Loader
Files are placed in the src directory [also known as a class directory]
//Get Class Loader ClassLoader classLoader = Servlet111.class.getClassLoader(); //Get Read File Stream from Class Loader InputStream inputStream = classLoader.getResourceAsStream("com/example/demo1/docker Icon.png");
Note: If the file is too large, it cannot be read by the class loader and will cause memory overflow
request object and response object
Tomcat receives http requests from clients and creates a request object representing the request and a response object representing the response for each request. Get the data submitted by the browser and look for the request object. The response object represents an http response, then output data to the browser and look for the response object.
HttpServletRequest
The HttpServletRequest object represents the client's request. When the client accesses the server through the HTTP protocol, all the information in the HTTP request header is encapsulated in this object, and the developer can obtain the client's information through this object's method. (To get browser information, look for the HttpServletRequest object)
common method
Get Client [Browser] Information
- The getRequestURL method returns the full URL at which the client makes the request.
- The getRequestURI method returns the part of the resource name in the request line.
- The getQueryString method returns the parameter part of the request row.
- The getPathInfo method returns additional path information from the request URL. Additional path information is the content in the request URL that follows the path of the Servlet and precedes the query parameters, starting with'/'.
- The getRemoteAddr method returns the IP address of the requesting client
- The getRemoteHost method returns the full host name of the requesting client
- The getRemotePort method returns the network port number used by the client
- The getLocalAddr method returns the IP address of the WEB server.
- The getLocalName method returns the host name of the WEB server
Get Client Request Header
- getHeader method
- getHeaders method
- getHeaderNames method
Get client request parameters (data submitted by client)
- getParameter method
- getParameterValues (String name) method
- getParameterNames method
- getParameterMap method
HttpServletRequest Application
Anti-theft chain
I now have the latest resource for the Queen of Thieves, which I would like to see on my web page. Now people on other websites see that I have the resources of the king of thieves and want to paste my resources on his own website. So my exclusive resources were robbed by a CTRL+C and CTRL+V? And theft-proof chains can't be protected by their CRTL+C and CRTL+V
Describe a scene: The first page has a hyperlink pointing to the latest resources of the Queen. When I click in, I can see the latest resources of the Queen. Others can copy and paste my address and put it on their web pages. So I can't cut it off [Did you come and see my ad!]. To see my resources, you have to go through my home page point.
To achieve this effect, you need to get the Referer header to determine if the Referer came from my first page. If it's not from my home page, jump back to my home page.
//Where do I get web pages from String referer = request.getHeader("Referer"); //If it's not accessed directly from my home page or from the address bar, if ( referer == null || !referer.contains("localhost:8080/zhongfucheng/index.jsp") ) { //Go back to the first page response.sendRedirect("/zhongfucheng/index.jsp"); return; } //To execute the following statement, the instructions are clicked in from my home page. That's OK, show as usual response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("Road Flies XXXXxxxxxxxxxxxxxxxx");
Form Submission Data [Submit Data via post]
Get the form in the servlet to the submitted data, code as follows
//Format request character encoding request.setCharacterEncoding("UTF-8"); //Get the value from the name property of html String username = request.getParameter("username"); String password = request.getParameter("password"); String gender = request.getParameter("gender"); //Check box and drop-down box have multiple values and get multiple values String[] hobbies = request.getParameterValues("hobbies"); String[] address = request.getParameterValues("address"); //Get the value of the text field String description = request.getParameter("textarea"); //Get the value of the hidden field String hiddenValue = request.getParameter("aaa"); ....Various System.out.println().......
Submit data as a hyperlink
Common get methods for submitting data are: using hyperlinks, sendRedirect()
sendRedirect("servlet Address?Parameter Name="+parameter values &"Parameter Name="+parameter values);
servlet receives data
//Receive a value with username as the parameter name String username = request.getParameter("username"); System.out.println(username);
Solve the problem of Chinese random code
The reason for the scrambling of post and get requests is different here, so the solution is different.
Reason for post request scrambling
The Tomcat server default encoding is ISO 8859-1, while the browser uses UTF-8 encoding. The browser's Chinese data is submitted to the server, Tomcat encodes the Chinese language in ISO 8859-1 encoding, and when I read the data in Servlet, I get the random code. (Solution: The request's encoding is set to UTF-8, so scrambling is solved.)
//Format request character encoding request.setCharacterEncoding("UTF-8");
Reason for get request scrambling
How the post method passes parameters. When we click the submit button, the data is encapsulated in Form Data, and the entity body is carried over in the http request (the transmitted data is called the entity body). Since the request object encapsulates the http request, the request object can parse the sent data, so simply setting the encoding to UTF-8 will solve the problem of chaos. get is different in that its data is carried over from the message line and is not encapsulated in the request object, so it is invalid to set encoding using request.
Solve get-style scrambling
Since you know that Tomcat's default encoding is ISO 8859-1, it must have been ISO 8859-1 when it was brought to the browser by the message body.
Code case:
//The data is now a string encoded by ISO 8859-1, which is garbled String name = request.getParameter("username"); //Scrambled get original data by reverse checking ISO 8859-1 byte[] bytes = name.getBytes("ISO8859-1"); //From the original data, set the correct code table and build the string String value = new String(bytes, "UTF-8");
The get method can also alter the configuration of the Tomcat server to resolve clutter, but it is not recommended and is not flexible. The default encoding for Tomcat is ISO 8859-1. If you change the encoding for UTF-8 under the configuration of the Tomcat server, this will solve the problem of the server causing garbage when parsing the data. Add URIEncoding="utf-8" to the Connector on port 8080, and set the encoding for Tomcat to UTF-8 when it accesses the port, thus resolving the chaos. This modification fixes UTF-8 encoding.
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>
There is another way to change the server encoding. Set Tomcat's encoding when accessing the port to be the page's encoding, which changes with the page's encoding.
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true" />
Summary:
- post directly changes the encoding of the request object
- get mode requires manual conversion of encoding
- The get method also modifies the Tomcat server encoding, which is not recommended because it relies too much on the server!
- Submit data using post
Implement forwarding
Redirect can be redirected using response's sendRedirect(), which functions as page Jump and request's getRequestDispatcher.forward(request,response) implements forwarding as well as page jumping.
Forward code:
//Get the requestDispatcher object and jump to index.jsp RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.jsp"); //Call forward() of the requestDispatcher object to forward, pass in request and response methods requestDispatcher.forward(request, response);
Requesting this servlet will be forwarded to index.jsp up.
Forward Submitted Data to Server
When ServletContext was used, it was said that Servlets can communicate through ServletContext, which can also be called domain objects. Request can also be called a domain object, except that the domain of the ServletContext is the entire web application, and the domain of the request represents only one http request. Request is used below to communicate between Servlets.
Servlet111 code
//Store zhongfucheng value with username as key request.setAttribute("username", "kaico"); //Get the requestDispatcher object RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Servlet222"); //Call forward() of the requestDispatcher object to forward, pass in request and response methods requestDispatcher.forward(request, response);
Servlet222 Code
//Gets the value stored in the request object String userName = (String) request.getAttribute("username"); //Output this value in the browser response.getWriter().write("i am :"+userName);
Result: Servlet222 successfully retrieved the data stored in Servlet111 by the request object.
Using ServletContext and request to communicate between Servlets, which one do we use?
The general principle is that requests are used whenever possible. Because ServletContext represents the entire web application, ** Using ServletContext consumes a lot of resources, and the request object ends with the request and the resources are recycled. ** Communication between Servlets using the request domain is very frequent in development.
Time Series Diagram Forwarded
Details of Forwarding
If a portion of the content written in the Servlet program has actually been transferred to the client before calling the forward method, the forward method throws an IllegalStateException exception. That is, don't write data to the browser before forwarding it
If you write to the Servlet Engine's buffer before calling the forward method, the forward method can be executed as long as the content written to the buffer has not been actually output to the client. The content originally written to the output buffer will be emptied, but the response header field information written to the HttpServletResponse object remains valid.
The difference between forwarding and redirecting
The actual location is different, the address bar is different
- Forwarding occurs on the server
- Forwarding is done by the server. Careful friends will find that the browser's address bar does not change when forwarding. When I visit Servlet111, even if I jump to the page of Servlet222, the browser's address is still Servlet111. That is, the browser does not know the action of the jump and forwarding is transparent to the browser. From the forwarding sequence diagram above, we can also see that forwarding is only an http request, in which the request and response objects are the same. This also explains why request can be used as a domain object for communication between Servlets.
- Redirection occurs in the browser
- Redirection is jumped by the browser. When redirection jumps, the browser's address changes. It has been described that the principle of redirection is achieved by a combination of the response's status code and the Location header. This is a page Jump by the browser that redirects two http requests. The request domain object is invalid because it is not the same request object
Different usage
Write directly from the resource name to the server and write the application name to the browser.
- request.getRequestDispatcher("/Resource Name URI").forward(request,response); Forwarding'/'represents the root directory of this application [zhongfucheng]
- response.send("/web application/resource name URI"); When redirecting,'/'represents the webapps directory
The range of URL s to and from is different
- Forwarding is a resource that a server jump can only go to the current web application
- Redirection is a browser jump to any resource
Different types of data are passed
- Forwarded request objects can pass various types of data, including objects
- Redirection can only pass strings
Different jump times
- When forwarding: jump immediately when the jump statement is executed
- Redirection: a jump is not performed until the entire page has been executed
Which one is used for forwarding and redirecting?
Forwarding takes the parameters of the request before forwarding. Redirection is a new request.
Typical scenarios:
Forwarding: Access the Servlet to process business logic, and then forward to jsp to display the processing results, with the URL unchanged in the browser
Redirect: Submit the form, redirect to another jsp after successful processing, prevent the form from being submitted repeatedly, the URL in the browser has changed
RequestDispatcher description
The RequestDispatcher object calls forward() to implement forwarding, which is already used. RequestDispatcher has another method, include(), which implements inclusion. What is the use?
When you write a Web page, you don't need to change the header or tail of a Web page. If we use Servlet s to output headers and endings in multiple places, we need to rewrite the code. The include() method of RequestDispatcher can be used to achieve the effect of including headers and tails.
Include headers and tails using Servlet111
request.getRequestDispatcher("/Head").include(request, response); response.getWriter().write("--------------------------------------------"); request.getRequestDispatcher("/Foot").include(request, response);
HttpServletResponse object
The http response consists of a status line, entity content, message header, and an empty line. The HttpServletResponse object encapsulates the information for the http response.
Application of HttpServletResponse
Call the getOutputStream() method to output data to the browser
The getOutputStream() method is called to output data to the browser. The getOutputStream() method can use either print() or write(). What's the difference?
print(): An exception will appear in the output Chinese. Reason: We have learned in io that outputStream outputs binary data, the print() method receives a string, the print() method changes "China" to binary data, Tomcat converts it using IOS 8859-1 encoding, and "China" does not support ISO 8859-1 encoding at all. So an exception occurred.
write(): can normally output Chinese to the browser. Reason: The code looks for gb2312 encoding by default when converting to byte[] array, while Chinese supports gb2312 encoding, so it can display normally.
HttpServletResponse object sets data encoding format for browser return
Set the header to inform the browser that the data code I send back is utf-8
//Set the header to inform the browser that the data code I send back is utf-8 response.setHeader("Content-Type", "text/html;charset=UTF-8"); response.getOutputStream().write("Hello, I am China".getBytes("UTF-8"));
In addition to using the HttpServletResponse object to set the header, I can use html tags to simulate an http header.
//Get the servletOutputStream object ServletOutputStream servletOutputStream = response.getOutputStream(); //Simulate http headers with meta tags to tell browsers the encoding and format of the data to be returned servletOutputStream.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes()); servletOutputStream.write("I am China".getBytes("UTF-8"));
Call the getWriter() method to output data to the browser
For the getWriter() method, which is a subclass of Writer, only character data can be output to the browser, not binary data. Directly outputting Chinese will cause scrambling. Because the default encoding of Tomcat is ISO 8859-1, when we output Chinese data, Tomcat will encode the data to us according to the ISO 8859-1 code table. Chinese does not support this code table, so there is chaos. You also need to have the browser display the data using the UTF-8 code table. As mentioned above, the server is encoding problems when handling Chinese encoding and browser display data. This paper introduces a method that not only sets up the browser to display data with UTF-8, but also sets the transcoded Chinese code table to UTF-8 internally. response.setContentType("text/html;charset=UTF-8");
//Set up the browser to display data in UTF-8 encoding, response.setContentType("text/html;charset=UTF-8"); //Get the printWriter object PrintWriter printWriter = response.getWriter(); printWriter.write("After reading the blog!");
Implement file download
To be able to download to others, the server should have this resource. If the browser sends all requests for a Servlet, I'll write a Servlet, and when someone visits my Servlet, they can download my picture!
Steps to achieve:
1. Read files in java
//Get the path to the resource String path = this.getServletContext().getRealPath("/download/1.png"); //Read Resources FileInputStream fileInputStream = new FileInputStream(path); //Get the file name and save the path as \ on your computer. String fileName = path.substring(path.lastIndexOf("\\") + 1);
2. Tell the browser that I want to download this file (set the header)
//Set the header and tell the browser that I want to download 1.png this picture response.setHeader("Content-Disposition", "attachment; filename="+fileName);
3. Send back the read content to the browser
//Write the read resource to the browser int len = 0; byte[] bytes = new byte[1024]; ServletOutputStream servletOutputStream = response.getOutputStream(); while ((len = fileInputStream.read(bytes)) > 0) { servletOutputStream.write(bytes, 0, len); } //close resource servletOutputStream.close(); fileInputStream.close();
4. When you request this servlet, the browser prompts you to download it.
5. Setting up Chinese random code in file names
To resolve file name scrambling, we need URL encoding
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
Implement automatic refresh
To refresh pages, update resources and enable browsers to refresh automatically at the prescribed time, it must be time to modify the header again.
//Automatically refresh pages every 3 seconds response.setHeader("Refresh", "3");
After we log in to the website, we often see [successfully logged in, automatically jump after 3 seconds...], in fact, this is done with Refresh.
response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("3 Jump page in seconds....."); //Jump to index after three seconds. JSP page, web application mapping path I set to /, url does not write application name response.setHeader("Refresh", "3;url='/index.jsp'");
The results are as follows:
Set Cache
The browser itself has a caching mechanism
When I first visited index. When jsp, the browser sent two requests to the server [one for web pages and one for pictures], when I visited the index for the second time. When jsp, the browser cached the pictures! Pictures are not reloaded, they are taken out of the cache.
But some data can't be cached, so here's the ability to disable caching
//The browser has three headers to set up the cache for compatibility! Set up all three headers response.setDateHeader("Expires", -1); response.setHeader("Cache-Control","no-cache"); response.setHeader("Pragma", "no-cache"); //To see the effect here PrintWriter printWriter = response.getWriter(); printWriter.print("How do you do" + new Date().toString());
Of course, if some data on the page is not updated for a long time, you can set it as a cache, which can improve the performance of the server
Implement data compression
The amount of information on a Web page is very large, and if you don't compress the data and send it back to your browser, it will consume a lot of traffic.
Compression principle: We know that getOutputStream() and getWriter() both export data directly to the browser. Now all I have to do is not export the data directly to the browser, let me compress it first, then export it to the browser. java provides us with a GZIP compression class.
//Create a GZIPOutputStream object and give it ByteArrayOutputStream ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); //GZIP compresses data and GZIP writes data stored on byteArrayOutputStream gzipOutputStream.write(ss.getBytes()); //gzipOutputStream has a buffer, clears the buffer and closes the stream conveniently gzipOutputStream.close(); //Remove compressed data byte[] bytes = byteArrayOutputStream.toByteArray(); //Write compressed data to a browser response.getOutputStream().write(bytes);
The direct output of compressed data to the browser is messy because: since you compress the data and you write it to the browser, the browser does not know that this is compressed data, it opens the data in a normal way. Of course, it's a mess!
To have a high number of browsers, the data is compressed
//Tell the browser that this is gzip compressed data response.setHeader("Content-Encoding","gzip"); //Write compressed data to the browser response.getOutputStream().write(bytes);
Generate random number pictures
It is very common to generate random pictures. Verification codes are often written when we log on, and those are a picture that is written to the browser through the HttpServletResponse.
Steps to achieve:
1. To generate a picture, java provides the BufferedImage class for us to use
//Generate a picture in memory with a width of 80 and a height of 20, of type RGB BufferedImage bufferedImage = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB); //Get this picture Graphics graphics = bufferedImage.getGraphics(); //Set color and font to picture graphics.setColor(Color.BLUE); graphics.setFont(new Font(null, Font.BOLD, 20)); //Fill the whole picture with white graphics.setColor(Color.white); graphics.fillRect(0, 0, 80, 20); //To write data to the picture, write 12345 first, the horizontal coordinate is 0, the vertical coordinate is 20 [height] graphics.drawString("12345", 0, 20);
2. Write pictures to your browser
Write pictures to your browser, and java provides a stream of pictures [ImageIO] for us to use
//To write a picture to your browser, tell the browser that the type of return is a picture response.setHeader("ContentType", "jpeg"); //java provides a stream of pictures for us to use, this is a tool class //Pass in the picture, type is jpg, write to browser ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
If you want to generate Chinese, just look for a Chinese mapping table.
redirecting
A hyperlink that tells the browser to jump to another page is called a redirect jump. It is important to inform the browser to jump.
Redirection using the following HttpServletResponse object
//Redirect to index.jsp page response.sendRedirect("/zhongfucheng/index.jsp");
You can see two status codes on your browser, one is 302. One is 200. The 302 status code represents a temporary redirection in the http protocol. For example: I go to the Disciplinary Committee and say: Give me a leave form and I'm going home. The Disciplinary Commissioner told me: I don't have an absence form here. Go to the counselor. Looking back at my visit to Sevlet222: I look for Servlet222, which tells the browser: I don't have the resources you want, you have the resources in index.jsp page, you can find it yourself.
It is easy to see that redirection is achieved by using 302 status codes and jump addresses. So we can redirect the jump by setting the http header
//Setting status code is 302 response.setStatus(302); //HttpServletResponse encapsulates common status codes into static constants, so we can use SC_MOVED_TEMPORARILY stands for 302 response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); //The address to jump is index.jsp page response.setHeader("Location", "/zhongfucheng/index.jsp");
The sendRedirect() method actually encapsulates setStatus() and setHeader(), and the principles are setStatus() and setHeader().
getWriter and getOutputStream details
- The getWriter() and getOutputStream() methods cannot be called at the same time. Exception will occur if called at the same time
- The data that the Servlet program writes to the ServletOutputStream or PrintWriter object is retrieved from the response by the Servlet engine, which treats the data as the body of the response message, then combines it with the response status line and each response header and outputs it to the client.
- When the serice() method of a Servlet ends (that is, when doPost() or doGet() ends), the Servlet engine checks whether the output stream object returned by the getWriter or getOutputStream method has called the close method, and if not, the Servlet engine calls the close method to close the output stream object.