Day 16
Software architecture : common software architecture B/S, C/S
C/S: Client/Server (client/Server) such as QQ, there are independent clients, APP
Advantages: high security, no local connection
Disadvantages: software updates require both client and server ends, which is more troublesome
B/S: Browser/Server:
Advantage: only need to write server program
Cons: Poor security
Web resources :
Static resources: HTML
Dynamic resources: jsp/Servlet
Dynamic resources need to be converted into static resources before they can be accessed by the browser
Access web resources: protocol name://port name/path
web server
Tomcat: supports JavaWeb
JBoss: support jee
Weblogin: support jee
Tomcat: open source free
Environment variables must be configured before starting Tomcat
JAVA_HOME: must be configured first, because Tomcat needs jdk to start
JAVA_HOME=path to jdk
Add %JAVA_HOME/lib% before the Path variable;
Modify the port number of Tomcat: enter conf (the main directory of the configuration file) and then enter server.xml to modify
Tomcat's directory structure : lib stores jar packages, conf stores configuration files, logs log files temp temporary files, and src stores files written by yourself
web application
Steps: Create a directory in the webapps directory (no Chinese and spaces are allowed in the name), this directory is the project directory, and under the project directory, create an HTML file
HTTP protocol
The A and B parties of the agreement are the client and the server, and the format of the communication between the two parties.
Corresponding Protocol: Respond?
200: The request was successful
404: The requested resource is not found, indicating that the client requested resource does not exist
500: The requested resource was found, but an error occurred inside the server
302: Redirect
304:
Response header: List-Modified Request header: if-Modified-Since
When the time of if-Modified-Since is compared with the real time of the file, the server will respond with 304 and there will be no response body, indicating that the browser caches the latest version.
Eg:<meta http-equiv=”Refresh”content=”5;url=http://www.baidu.com”>
Automatically jump to Baidu page after five seconds
Eg generates a picture case (can be used to generate a verification code)
publicstaticvoid main(String[] args) throws Exception {
/*
* Classes used to manipulate images
* Image , ImageIOP BufferedImage Icon ImageIcon
*/
// get image buffer
BufferedImage bi = new BufferedImage(70, 30, BufferedImage.TYPE_INT_RGB);
// Get the drawing environment (drawing pen)
Graphics2D g2=(Graphics2D) bi.getGraphics();
g2.setColor (Color.WHITE ); // Set the color
g2 .fillRect(0, 0, 70, 30); // fill the image from 0,0 , which is to fill the color set in the previous step
g2.setColor (Color.RED ); // Set the border color
g2 .drawRect(0, 0, 149, 69); // Set the border
g2 .setFont( new Font( "宋体" ,Font. BOLD ,18)); // Set the font
g2.setColor ( Color.BLACK ); // Set the color
g2 .drawString( "HelloWord" , 3, 10); // Write a string to the picture
ImageIO.write(bi, "JPEG", new FileOutputStream("F:/a.jpg"));//保存图片
}
Note: First get the BufferedImage picture buffer, then get the drawing environment getGraphics() , first set it in the operation, and finally
保存:ImageIO.write(bi, "JPEG", new FileOutputStream("F:/a.jpg"));
Servlet overview
Each servlet is unique and can handle different requests
***Accept request
*** Process the request
***Complete response
The way to implement Servlet (what you wrote yourself)
Most of the methods in the servlet are not called by us
How to let browser access servlet
1. Specify an access path for the servlet (bind the servlet with a path)
Configuration: Configure in web.xml
<servlet>
<servlet-name>xxx</servlet-name>
<servlet-class>AServlet.Aservlet</servlet-class>
<load-on-startup>0</load-on-startup>
// Created when the server starts, 0 is the serial number
</servlet>
<servlet-mapping>
<servlet-name>xxx</servlet-name>
<url-pattern>/servler</url-pattern>
</servlet-mapping>v
2. Browser access path
http://localhost:8080/javaDay09/servlet
HTTP Servlet
The client sends a request, and tomcat calls the declaration cycle method to service (HttpServletRequest, HttpServletResponse), and it gets the request method by itself to call doGet() or
doPost(), method
Note: the doGet() method and the d0Post() method have to be written by ourselves. If it is called without overwriting, 405 will appear.
Do not define global variables in servlet, you can define and create local variables, you can create stateless members, if you create stateful members, then the state must be read-only.
Servlet Context(***)
There can only be one ServletContext object in a project
We can get this unique object in multiple servlets and use it to pass data to multiple servlets
This object is created when tomcat starts, ServletContext is released when it is closed, and it co-exists with Tomcat (server).
Get the ServletContext :
1 Void init (ServletConfig config)
{ServletContext context = config.getServletContext(); The getServletContext method of the SrevletConfig class can be used to obtain the Servlet object;}
2 The getServletContext method of the GenericServlet class can be obtained directly using this.getServletContext;
Domain object functionality:
Definition: used to transmit messages in multiple servlets, with the functions of storing and retrieving data
四个:《pageContext》《ServletRequest》《HTTPSession》《servletContext》
All domain objects have a Map inside, which is used to store data and operate with the ServletContext object:
setAttribute(Stringname, Object value) stores data
eg: ServletContext.setAttribute("xxx","XXX"); saves an and attribute, name xxx, value XXX
If this method is called multiple times and the value of name is the same, it is equivalent to the effect of overwriting.
Object getAttribute(String name): Get the value in the ServletContext
eg:String value =(String)ServletContext.getAttribute(“xxx”);
void removeAttribute(Stringname);删除
EnumerationgetAttributeNames(): Get all and object attribute names
Get initialization parameters
Servlet can also obtain initialization parameters, but it is a local parameter, that is to say, a servlet can only obtain its own initialization parameters, not others, that is, the initialization parameters are only those of a servlet.
You can configure public initialization parameters, which require ServletContext
Step: Define Global Parameters
<context-param>
<param-name>context-name</param-name>
<param-value>context-value</param-value>
</context-param>
Cannot be written in Servlet, in web-APP, globally,
call using the ServletContext object
String value =con.getInitParameter("context-name");
Get the related resource method
1 Get the real path
String path =this.getServletContext().getRealPath("/index.jsp");
D:\Myeclipse\develop\apache-tomcat-9.0.0.M17\webapps\java10\index.jsp
Get the path name with the drive letter, get the file path under the project
Get current directory and subdirectories
Setset =this.getServletContext().getResourcePaths("/WEB-INF");