Java learning punch in: days 90 and 91
javaWeb —JSP
Servelet
HTML+CSS and JavaScript were briefly introduced before, but only the interface problem can be solved. At that time, although it was mentioned that JSON can realize front-end and back-end interaction, JavaScript and java interaction, how the submitted form is sent to our background database needs other technical means
Servlet
Before learning about servlet s, learn about Java EE and Java
JAVA SE is the standard version of java, including the basic class library. This set of basic class library includes basic syntax, object-oriented, exception, IO, collection, reflection and other modules
JAVA EE is a relatively large specification. The JAVA EE specification includes 13 sub specifications (each sub specification also includes other sub specifications)
The C/S architecture and B/S architecture have been mentioned twice, so I won't repeat them here; It is mainly oriented to B/S architecture, that is, web applications.
S refers to the software server. Tomcat is a software. The default port is 8080
For example, enter a web address here
http://119.75.218.70:8080/egov/login.html
Here, the static resource login is obtained from 8080 on the host, that is, from the egov folder on the tomcat server html;
The path behind this is tomcat to get different java programs from the server to process business.
The above static resources do not need java code operation. After user access, they are directly displayed on the browser web page, that is, drawn on the web page. However, the URL here may correspond to different paths, and different sub paths may complete different operations;, The path can correspond to a static HTML web page or a java program - that is, it stores dynamic resource files. Here is an example
https://www.nowcoder.com / this is the total path, including DNS. The host name is www.nowcoder com https://www.nowcoder.com/login here is the login part, and the following login is the resource of this part https://www.nowcoder.com/mockexam/MockExam obtains the mockexam resources under the server mockexam path Paths and files are bound one by one
The following is the resource code; Servers are divided into software servers and hardware servers. Machines equipped with software servers are called hardware servers. That is, the machine equipped with Tomcat software server is the hardware server
Tomcat manages a folder on the server a, which is called webapps (Web Applications). The web container can manage multiple web application s; The resources include static resources and non static resources
browser -------http--->Web The server -----Servelet---> Server java program ---jdbc--> database Browser webServer DBSever
Servlet is a set of standard interfaces, which are connected by jdbc and servlet in JAVA EE and HTTP protocol of W3C. Convenient decoupling
Serelet -- serv represents the server side, let represents the applet server side applet
Analog Servlet
Before learning JDBC, I explained JDBC with an emulator; JDBC is a set of specifications defined by SUN company, that is, a set of interfaces, such as the Connection interface for obtaining connections; Statement interface, and then various database manufacturers have implemented these interfaces, such as driver interface, MySQL manufacturer has implemented mysql cj. jdbc. Driver; In the development process, the bytecode of MySQL implementation class should be imported; The resources here are in the connector when downloading mysql
/** * SUN Java EE specification developed by the company: Servlet specification * Servelet Interface is the core interface in the servlet specification */ public interface Servelet{ /* The small java program on the server side must implement the service method */ void service(); } /* javaweb programmer When writing small java programs, you can't write them at will. You must implement the servlet interface */ public class DeleteServlet implements Servlet{ public void service(){ System.out.println("Connect to database, deleting data, please wait"); } } /* Tomcat WebServer Web The server web container */ public class Tomcat{ public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.println("Server connection succeeded"); System.out.println("Please open the browser and enter the request path in the browser address bar"); //Waiting for user access String requestPath = s.next(); System.out.println("Your access path is" + requestPath); //The java applet that gets the path is bound together, which is similar to the configuration file FileReader file = new FileReader("Web.html"); Properties pro = new Properties(); pro.load(file); pro.getProperties(requestPath); //LoginServlet //Create objects through the reflection mechanism Class c = Class.forName(serveltClassName); Servlet servlet = (Servlet)c.newInstance();//Are all implemented servlets //Calling method for servlet interface servelet.service(); } } Then, the input path, the path and resources are bound one by one. The establishment of this binding relationship depends on programmer Written configuration file,.xml,Similar to what I wrote before properties; Web.xml /login = LoginServlet /delete = DeleteServlet /save = SaveServlet Servelt When we write a program, we can't write it casually. We must implement it Servelet Interface, and corresponding the program and path in the configuration file; Here, the program call depends on Tomcat
Tomcat installation configuration
Installing software is a hurdle that must be crossed by learning servlet; Like Mysql, bloggers encountered all possible problems in installation at that time; It's hard 😢; This time is no exception, so I'm here to share the problem-solving process
- First, install Tomcat and directly enter Tomcat into Baidu to download it on the official website; It is the same as mysql; There are two versions, one is Zip version and the other is exe version; For personal preference, I choose exe. Here, you can customize the path when selecting the path; finish is complete
- Then configure environment variables; Set two variables as the installation path of tomcat; Then configure the bin path of tomcat in the path
Problem: localhost:8080 failed to open
- Maybe you didn't open Tomcat exe; You can load it only after you open it
- If access is denied, the service may be occupied; The solution is
PS C:\Windows\system32> netstat -ano | findstr 8080 TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 7340 PS C:\Windows\system32> taskkill -pid 7340 -f success: Terminated PID Process for 7340.
The previous shutdown thread is often used, so remember that it is netstat -ano | findstr 8080
After that, close the service taskkill pid id -f; Here, we can change the name of the command to shut down the service and change shutdown to stop; It can be executed directly in the DOS window, otherwise it will conflict with other commands
Introduction to Tomcat directory
- The bin directory is the commonly used commands of Tomcat, including starting startup, closing stop, etc
- conf: configuration files, including properties, are very simple, but xml configuration files are more complex; It is used for data exchange and can be used as a configuration file
1catalina.org.apache.juli.AsyncFileHandler.level = FINE 1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina. 1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90 1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8
-
lib library, which contains many bytecodes, is the core program of Tomcat implementation; The bottom layer of Tomcat relies on java
-
logs log information, including the log information of our operations
-
temp temporary file
-
webapps includes developed applications, which can put many applications
-
work JSP storage directory
Simulation development program
When developing a program, you develop it first and then deploy it; Development can be developed at any location, but finally, resources should be allocated; The so-called configuration is to put the developed project into our server applet; In this way, users can directly access the project under port 8080 of the host
Here, let's simulate the static resource file and write a static HTML page for users to access
No more writing here. Just the HTML directory written before. The so-called deployment is very simple here: directly copy and paste the development project folder into the webapps directory
In this way, even if the deployment is successful, the complex processes we simulated above are encapsulated and directly completed by Tomcat, so it is very simple.
Here, when we enter the path in the browser, we don't need to write webapps, but directly write the next level directory of apps; So I want to visit index html; Then the address typed by the browser is
localhost:8080/imcfeng.com/index.html
In this way, access is realized. Tomcat is a server software. It is difficult to understand the underlying layer, but after encapsulation, you can directly establish a connection and bind the path with the static resources in the server. Users can directly access relevant resources through the path
You can see that the static page resource file we wrote is successfully opened through Tomcat
Interpretation of essence
Tomcat is a server-side applet. Its main purpose is to build the connection between users and servers. If the self-developed project is not deployed, you can only access it yourself. You can click index HTML can open the interface, which is the S-side of the server; However, for remote users, the server-side resources are invisible, and they can operate only the browser, that is, the B-side; Enter the URL to access the resources of the server; Tomcat is a transit station, and users send requests; After that, Tomcat forwards the obtained resources and returns them to the user. The user'S browser draws the relevant pages
Dynamic resource development WEB-INF
SUN company has defined the standard servlet. html, css, js and other files can be placed in the created directory. In the directory of the project we created, the file name must be WEB-INF; This is a specification, all uppercase, and connected with - in the middle;
WEB-INF fixed three folders
- classes: the bytecode where the item is stored
- lib: the library stores the jar package, that is, classPath. The path here is different from that of Tomcat. The path here is a local path and the path above is a global path
- web.xml: bind path and resource; Because this is a dynamic resource, Tomcat cannot find it without binding
So there is no technical content here. Write it directly and fixedly
The step of development is to write java program files and put the files into the classes of the project; Then web Write configuration information in XML
web.xml must be a legal file, that is, the referenced Tomcat xml file, and the web app tag should be written; A webapp can only correspond to one xml file. It will be resolved during server startup. If the parsing fails, the app will also fail to start; This is fixed by the specification
Here you can demonstrate the writing of xml
<?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>servlet Name, optional</servlet-name> <servlet-class>java File name, if there is a package, add it</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet Name, optional</servlet-name> <url-pattren>Request path</url-pattren> </servlet-mapping> </web-app>
In this way, the dynamic resources of the server can be accessed in the user's browser. The path here must start with "/", which represents the name of the resource
The next step is to develop dynamic resources and display the results in the browser, that is, a complete project, so that the project can be applied 🎄