Recently also made a simple Java Web project, because has not been contacted before, here to record the process of building a Web server.
Generally we do a service side or on the local computer installed the environment, usually Windows system, the main installation jdk + tomcat + MySQL, these installation tutorials are available online, and very simple, I do not say much here, What I'm talking about is setting up a Web server-side environment on a long-range Linux service.
For a liunx server, we can use terminal tools such as Xshell login to operate the remote server, the advantage of using Xshell is that we can use RZ, sz command upload upload file, we can download the Linux version of the JDK in advance, Tomcat, MySQL, use the RZ command to upload to our Linux server
[email protected]:~$ sudo rz will automatically pop up the file box to load the file, you can upload the file, very convenient
[email protected]:~$ sudo sz filename to download the corresponding file
The following is the installation of various installation packages and environment variable configuration, each installation package can be downloaded on the official website (choose the version you need)
First, the installation of the JDK
1. Unzip the uploaded JDK
sudo tar -zxvf jdk-8u144-linux-i586.tar.gz
2. After the successful decompression, you can see that the directory is more folder jdk1.8.0_144, the first new folder in/usr Java, the jdk1.8.0_144 moved to/usr/java directory
sudo mv jdk1.8.0_144 /usr/java/
3. Now configure the JDK environment variables
Use the VI command to edit the /etc/profile file Vi/etc/profile
Add four configuration information at the bottom of the file:
Export java_home=/usr/java/jdk1.8.0_144
Export JRE_HOME=${JAVA_HOME}/JRE
Export Classpath=.:%{java_home}/lib:%{jre_home}/lib
Export Path=${java_home}/bin: $PATH
Where Java_home is designated as your JDK installation directory, in addition, Linux is separated by colons, separate from windows using semicolons.
Edit OK after save exit, execute command: source/ect/profile
Execute Java, Javac command to see if the JDK is configured.
II. TOMCAT Installation
1. Unzip the uploaded Tomcat package, here I'm going to put Tomcat in the/usr/lib.
sudo tar zxvf apache-tomcat-8.5.20.tar.gz-c/usr/lib
sudo mv apache-tomcat-8.0.28 tomcat8.5
sudo chmod-r 777 tomcat8.5
2. Environment variable Configuration
In the tomcat8.5 Bin folder there is a startup.sh and shutdown.sh files, you can open with VI, and then the last sentence to add the following variables (according to their actual situation to modify):
java_home=/usr/java/jdk1.8.0_144
Jre_home=${java_home}/jre
Path= $JAVA _home/bin: $PATH
Classpath=.: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar
tomcat_home=/usr/lib/tomcat8.5
3. Start Tomcat
Execute command SH startup.sh
in the browser input; http://localhost:8080 If the effect of the Tomcat home page appears, the configuration is successful. (localhost is the local IP, if placed on the server side, you need to replace the server to provide the external public network IP)
Third, MySQL installation
I am using MySQL provided in the software source, directly with the command
sudo apt-get install Mysql-server
During the installation, you will be prompted for MySQL login password (need to log, log in after MySQL need to use this password)
Log in to MySQL and use the command
mysql-uroot-p123456 (123456 is the password you set), you can do the database operation later.
Show Databases (shows some of the databases)
Use databasename(which database is used, DatabaseName is the database name)
Show Tables(when the database is selected, which tables are displayed)
After the above three tools are installed, the Java Web Server environment is set up, if our project does not involve the database, we just need to put our web project, export a war package, directly into the Tomcat directory under the WebApps directory is OK, Tomcat will automatically unzip the war pack for us. However, if our project involves database operations, we will need some related configuration as well.
1. Requires MySQL JDBC driver jar package to be put into Tomcat's lib directory, my version is Mysql-connector-java-5.1.43-bin.jar
2. Configuring the MySQL data source under Tomcat requires some configuration of several files under Tomcat's CONF directory
Edit Server.xml, add under globalnamingresources tab
<resource driverclassname= "Com.mysql.jdbc.Driver" maxactive= "Ten" maxidle= "2" maxwait= "" Name= "Jdbc/mysql" Password= "123456" type= "Javax.sql.DataSource" url= "Jdbc:mysql://localhost:3306/grginvoice?autoreconnect=true" Username= "Root"/>
Edit Context.xml, add under Context tab
<resource name= "Jdbc/mysql" auth= "Container" type= "Javax.sql.DataSource"
Username= "Root"
Password= "123456"
Maxactive= "850"
maxidle= "80"
Removeabandoned= "true"
removeabandonedtimeout= "5"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/grginvoice?autoreconnect=true"
minevictableidletimemillis= "4000"
Timebetweenevictionrunsmillis= "/>"
Edit Web. xml and add it under the Web-app tab
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Of course, you also need to create the right database and tables in MySQL, restart Tomcat, you can use MySQL on the server
(The database and Tomcat installed on the same server, the URL inside the address with localhost, do not need to replace the server's IP address)
Here's the code for a MySQL database connection section
Connection conn = Null;datasource ds = null; Statement stmt = null; ResultSet rs = null;try {Context CTX = new InitialContext (); if (CTX = = null) System.out.println ("no content");d s = (Datasou RCE) ctx.lookup ("Java:comp/env/jdbc/mysql"); if (ds! = NULL) conn = Ds.getconnection (); if (conn! = null) System.out.println ("Connect MySQL success"); stmt = (Statement) Conn.createstatement (); String strSQL = "SELECT * from Grginvoice.ticketinfo where logicid=\" 0001\ ""; rs = (ResultSet) stmt.executequery (strSQL), if (rs! = null) System.out.println ("Query is Success"), while (Rs.next ()) { System.out.println (rs.getstring (2));} } catch (Namingexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (SQLException e) {//Todo Auto-ge Nerated catch Blocke.printstacktrace ();}
Ps:
MySQL runs in safe-updates mode, which causes the update or delete command to not be executed under a non-primary key condition:
Execute the following command:
Show variables like ' sql_safe_updates ';
SET sql_safe_updates = 0;
Build a Java Web server