Article directory
1.1 C/S Architecture
Client/ServerClient/Server
As a stand-alone program, the client has a better graphics effect but is difficult to maintain.
1.2 B/S Architecture
Browser / Server Browser / Server
Enter the URL directly into the browser to visit
After the server is upgraded, the browser can directly access and obtain
However, the browser cannot carry too large resources, so the page effect and other experience are not as good as those of the client.
2.1 server
A web server is a container for running and publishing web applications. Only by placing the developed web project in this container can all users in the network access it through a browser.
2.2 Common servers
- Tomcat (one of the mainstream web servers, suitable for beginners)
- jetty
- resin (very fast)
3.1 Tomcat installation
Official website: Apache Tomcat® - Apache Tomcat 10 Software Downloads
After the download is complete, decompress it directly. Note that the file path is best not to have a Chinese name.
There are server management files under the bin folder
The webapps project deployment file initially holds some example files
3.2 Using Tomcat
Double click the bat file
The command line port appears and remains open
If the interface closes directly after running the command line here, you need to check whether the JAVA_HOME configuration is correct
Checking method: Enter java on the command line to see if it is an executable command
Garbled characters appear here because the encoding method of Tomcat needs to be modified
Enter the browser to visit Apache Tomcat/9.0.31
Observe the following interface, Tomcat starts successfully
Double-click the bat file to terminate Tomcat operation
At this time, accessing Apache Tomcat/9.0.31 will not be able to observe the content
3.3 Tomcat configuration
The port number is the unique identification of our access program, the default is 8080
Can be modified in server.xml under the conf folder
Use Notepad to open and modify the port value after restarting Tomcat
You can successfully modify the default port number.
Similarly, the configuration of other attributes can also be completed by modifying the text content.
3.4 Tomcat project deployment
Create a project file under the webapps folder
The WEB-INF folder and the html file you will access are required under the project file
Under the WEB-INF folder
web.xml can be copied from root and others are empty folders
- Visit: http://localhost:8080/firstweb/first.html
- protocol: http
- Domain name or hostname: locallhost
- Port number: 8080
- Project path: firstweb/first.html
So we can see the first.html page we wrote in toncat
If the project path is selected incorrectly, a "resource does not exist" error will appear.
4.1 Servlet Technology
- The server-side program (code, function implementation) can interactively process the request sent from the client to the server and complete the operation response.
- A dynamic web technology
- Basics of Java Web Program Development
4.2 Servlet configuration
Find the servlet-api.jar package under the lib file path
save its path
Append to the environment variable Classpath of the system
4.3 Configuration test
Create the MyServlet.java file
//简单Servlet示例
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
public class MyServlet implements Servlet{
public void init(ServletConfig config) throws ServletException{
}
public void service(ServletRequest request,ServletResponse response) throws ServletException,IOException{
System.out.println("My First Servlet");
}
public void destroy(){
}
public ServletConfig getServletConfig(){
return null;
}
public String getServletInfo(){
return null;
}
}
First of all, make sure that the java javac version is consistent
My versions are both 1.8.0_312
If you find that the two are inconsistent, it is because different versions of JDK are installed in the computer.
Delete different JDKs or adjust the path to put the bin path of a certain version of JDK in the front
Compile the above JAVA file on the command line
Compiled files with .class suffix appear in the same folder
It indicates that the Servlet environment configuration is successful.
4.4 Servlet Deployment
Place the class file in the previously created classes folder
Configure the web.xml 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"
metadata-complete="true">
<!-- 1.添加servlet节点 -->
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<!-- 2.添加servlet-mapping节点 -->
<!-- 2.对Servlet做映射 name保持一致 -->
<!-- 2.url-pattern是访问Servlet的路径 -->
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>
Although there is currently no content, no 404 or 500 errors are reported when accessing
500 means that there is an error in the Servlet code, including version incompatibility, etc.
Indicates that the Servlet was deployed successfully
The System.out.println statement prints output on the console
If you modify the Java file, you need to recompile and restart the Tomcat server
5.1 IDEA Department
We found that after rewriting the code, it is necessary to recompile, move files, restart services, etc., and the time cost is relatively high.
We deploy the Servlet environment in the IDEA development tool to facilitate our subsequent development.
IDEA creates a new project
Select the Enterprise project Name can be modified independently
Store the following files in the Web-INF folder
Add lib as dependency as our project's library
Configure Tomcat
deployment project
Click Execute IDEA in the lower menu to run the Tomcat server autonomously
Auto-open browser interface