Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implement JSP to connect to MySQL through Tomcat

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Implement JSP to connect to MySQL through Tomcat

Service Overview

1. What is Tomcat?

Tomcat server is a free and open source Web application server, which is a lightweight application server. It is widely used in small and medium-sized systems and not many concurrent access users. It is the first choice for developing and debugging JSP programs.

Like Web servers such as IIS, Tomcat has the ability to process HTML pages. However, Tomcat is not as capable of handling static HTML as Apache servers. The latest version of Tomcat is 8.0.0-RC1 (alpha) Released. JSP, whose full name is Java Server Pages and java Server Page in Chinese, is a dynamic web technical standard advocated by Sun Microsystems and established by many companies. JSP technology is somewhat similar to ASP technology. It inserts Java program segments and JSP tags into traditional web page HTML files to form a JSP file with a suffix of .jsp. Support JSP language, paid ls, version of web server: weblogic of oracle; websphere of IBM

Tomcat is an application server. For example, suppose we browse the pages of sina and all the pages of sina are configured on a Tomcat server, then if you don't have this Tomcat to provide server-side services, you won't be able to open any pages of sina.

Mode: BDUBERS mode

Port: 8080

1) related concepts of java:

JDK: java development kit (kit). To put it simply, JDK is a SDK for developers, which provides the development environment and running environment of Java.

SDK is a Software Development Kit that generally refers to software development packages, which can include libraries, compilers, and so on.

JRE: Java Runtime Enviroment refers to the running environment of Java, which is aimed at users of Java programs, not developers.

JVM: java virtual machineJVM is what we often call the java virtual machine. In the JDK installation directory, you can find two folders bin and lib in the jre directory. Here you can think that bin is the class library needed for jvm to work in jvm,lib, and jvm and lib are called jre together.

2. The difference between Apache and Tomcat:

Apache is only a Web server, which can be run as an independent web server, but it only supports static web pages, such as ASP, PHP, CGI, JSP and other dynamic web pages.

Tomcat can also run as a stand-alone web server. But Tomcat is also an application (Java) server, it is just a Servlet container (generating dynamic Web content).

Because Apache interprets static pages faster and more stably than tomcat, for the above reasons, a real website uses an Apache as a Web server to provide services for static page requests, and uses Tomcat server as a Servlet/JSP plug-in to display dynamic pages of the site.

3. Benefits of Apache+Tomcat integration:

1)。 Apache is mainly used to parse static text. For example, html,tomcat also has this function, but apache can greatly improve the efficiency. For enterprise applications with a large number of concurrency, it can better display the high efficiency of apache.

2) Tomcat is used to parse jsp,servlet, etc. All customer requests will first be sent to Apache. If the request is static, it will be parsed by apache, and the result will be returned to the client. If it is a dynamic request, for example, jsp,apache will hand over the parsing work to tomcat and be parsed by tomcat (which first requires the realistic integration of the two). After tomcat parsing is completed, the result will still be returned to the client through apache. In this way, we can achieve division of labor and cooperation, achieve load balancing and improve the performance of the system! And because JSP interprets the code on the server side, integration can reduce the service overhead of Tomcat.

4. The principle of Apache+Tomcat integration.

As a subproject under Apache, there is a natural relationship between Tomcat and Apache. In practice, Apache runs as the main server. When a request from jsp or servlet is monitored, the request is forwarded to the tomcat server. After parsing by the tomcat server, the request is sent back to apache, and then sent back to the user by apache.

There are two listening ports in tomcat, one is 8080 to provide web services, and the other is 8009 to listen for requests from apache. When apache receives a jsp or servlet request, it sends the request to port 8009 of tomcat, which is processed by tomcat, and then returned to apache, and returned to the customer by apache.

One: experimental objectives

Practice: implement tomcat login with user password

Practice: implement JSP to connect to MySQL through Tomcat

Second, the experimental environment

Server: xuegod63.cn IP:192.168.1.63

Client: xuegod64.cn IP:192.168.1.64

Three: experimental steps

1. Install JDK

1) prepare relevant software packages:

Jdk-7u71-linux-x64.rpm

Apache-tomcat-7.0.27.tar.gz # tomcat installation package

Mysql-connector-java # is used to connect to a mysql database using java. MySQL database connector.

Mysql-connector-java-5.1.34.tar.gz

2) install and configure Java runtime environment-jdk. Upgraded the version of jdk

[root@xuegod63~] # rpm-ivh jdk-7u71-linux-x64.rpm

[root@xuegod63~] # rpm-pql / root/jdk-7u71-linux-x64.rpm# can know that the installation directory of jdk is in / usr/java by looking at the information of jdk.

Linux export command parameters

Function description: sets or displays environment variables.

Syntax: export [- fnp] [variable name] = [variable setting value]

Example:

[root@xuegod63 ~] # A = "aaa"

[root@xuegod63 ~] # echo $A

Aaa

[root@xuegod63 ~] # export B = "bbb"

[root@xuegod63 ~] # echo $B

Bbb

3) modify the environment variable:

[root@xuegod63~] # vim / etc/profile# at the end of the file, add the following:

Export JAVA_HOME=/usr/java/jdk1.7.0_71

Export JAVA_BIN=/usr/java/jdk1.7.0_71/bin

Export PATH=$ {JAVA_HOME} / bin:$PATH

Export CLASSPATH=.:$ {JAVA_HOME} / lib/dt.jar:$ {JAVA_HOME} / lib/tools.jar

Note:

ExportJAVA_HOME=/usr/java/jdk1.7.0_71#java home directory

ExportJAVA_BIN=/usr/java/jdk1.7.0_71/bin#java executable directory

ExportPATH=$ {JAVA_HOME} / bin:$PATH# repair system environment variables

ExportCLASSPATH=.:$ {JAVA_HOME} / lib/dt.jar:$ {JAVA_HOME} / lib/tools.jar# classpath

[root@tomcat~] # source / etc/profile# to make the configuration file effective

4) verify that the java runtime environment is installed successfully:

[root@xuegod63~] # java-version

Javaversion "1.7.0Y71"

Java (TM) SERuntimeEnvironment (build1.7.0_71-b14)

JavaHotSpot (TM) 64-BitServerVM (build24.71-b01,mixedmode)

# if the corresponding version of the installation appears, the java runtime environment has been installed successfully.

2. Install tomcat to improve server performance:

Note: the tomcat-native.tar.gz # package is already available in tomcat's bin directory and no longer needs to be downloaded.

Specific installation and configuration

1) install and configure tomcat

There are two ways to install Tomcat. One is binary files that can be unzipped directly. The second is compilation and installation. The first method is used, because the first binary file is downloaded by default on the official website.

[root@xuegod63 ~] # tar zxvf apache-tomcat-7.0.27.tar.gz

[root@xuegod63 ~] # ls apache-tomcat-7.0.27/bin/ # execution file is in this place

[root@xuegod63 ~] # mv apache-tomcat-7.0.27 / usr/local/tomcat/

[root@xuegod63 ~] # cd / usr/local/tomcat/

[root@xuegod63 tomcat] # vim webapps/ROOT/index.jsp # default home page, website root directory

Note: / usr/local/tomcat/webapps/ROOT/ website root directory

2) create a Sysv management script for tomcat startup

[root@xuegod63 tomcat] # vim / etc/init.d/tomcat # writes the following

#! / bin/bash

# Tomcat init script for linux.

# chkconfig: 2345 96 14

# description: The Apache Tomcat server/JSP container

JAVA_OPTS='-server-Xms64m-Xmx128m'

JAVA_HOME=/usr/java/jdk1.7.0_71

CATALINA_HOME=/usr/local/tomcat # specify the tomcat home directory

$CATALINA_HOME/bin/catalina.sh $* # specify the script that runs or closes tomcat

[root@xuegod63tomcat] # ls / usr/local/tomcat/bin/catalina.sh

/ usr/local/tomcat/bin/catalina.sh

Description:

JAVA_OPTS='-Xms64m-Xmx128m'# is a variable used to set JVM-related running parameters.

-Xms: initial Heap (heap) size, minimum memory used. This value should be set to a higher value when cpu performance is high.

-maximum Xmx:javaheap. The above two values are the minimum and maximum memory allocated for JVM, depending on the size of the hardware physical memory. It is recommended to set it to half of the physical memory.

3) set boot:

[root@xuegod63] # chkconfig-- add tomcat

[root@xuegod63] # chkconfig-- list tomcat

Tomcat0:off1:off2:on3:on4:on5:on6:off

[root@xuegod63 ~] # chmod + x / etc/init.d/tomcat

[root@xuegod63 ~] # service tomcat start# starts the tomcat service

[root@xuegod63tomcat] # netstat-antup | grep 8080

Tcp00:::8080:::*LISTEN4220/java

4) browser login Tomcat test:

Visit the website provided by tomcat through the browser, which we already know that tomcat can provide Web services separately.

Http://192.168.1.63:8080/

Click "ManagerApp", and when you enter the management page, a message appears:

5) add account password

[root@xuegod63 ~] # vim / usr/local/tomcat/conf/tomcat-users.xml # change at the end of the file:

Change:

Are:

# add user role: manager-gui. Added user: mk password: 123456

6) restart the tomcat service and test login

[root@xuegod63 ~] # service tomcat stop

[root@xuegod63 ~] # service tomcat start

Log in again, and log in with the set user name and password. The interface after login is shown in the figure:

7) install tomcat-native

Tomcat can use apr technology to provide better scalability and performance. Used to improve the performance of tomcat.

Tomcat native provides an optimization technology on the specific running platform, which is based on ARP (Apache Portable (portable) Runtime) technology tomcat can make use of the apr interface of apache, use some local operations of the operating system, so as to improve performance. APR improves the processing power of static pages

Details: http://tomcat.apache.org/native-doc/

Dependent package: apr-devel,openssl-devel

[root@xuegod64 tomcat] # yum install-y apr-devel openssl

[root@xuegod63 tomcat] # cd / usr/local/tomcat/bin

[root@xuegod63 bin] # ls tomcat-native.tar.gz # there are tomcat-native.tar.gz files in this directory

Tomcat-native.tar.gz

[root@xuegod63 bin] # source / etc/profile

[root@xuegod63 bin] # tar-zxvf tomcat-native.tar.gz

[root@xuegod63 bin] # cd tomcat-native-1.1.23-src/jni/native/

[root@tomcat native] # / configure-- with-apr=/usr/-- with-ssl

[root@tomcat native] # make & & make install

8) install the connector for MySQL

[root@xuegod63 ~] # tar-zxvf mysql-connector-java-5.1.34.tar.gz

[root@xuegod63 ~] # cd mysql-connector-java-5.1.34

[root@xuegod63 mysql-connector-java-5.1.34] # ls

Build.xml COPYING mysql-connector-java-5.1.34-bin.jar README.txt

CHANGES docs README src

[root@xuegod63 mysql-connector-java-5.1.34] cp mysql-connector-java-5.1.20-bin.jar / usr/local/tomcat/lib/ # just copy this jar package over

[root@xuegod63 tomcat] # service tomcat stop

[root@xuegod63 tomcat] # service tomcat start

9) install mysql:

[root@xuegod64 ~] # yum install mysql-server-y

[root@xuegod64 ~] # service mysqld restart

[root@xuegod64 ~] # mysqladmin-uroot password 123456 # set password

10) set the default encoding of mysql database to utf8

[root@xuegod64 ~] # vim / etc/my.cnf # add red flag content to the configuration file

[client]

Default-character-set=utf8

[mysqld]

Datadir=/var/lib/mysql

Socket=/var/lib/mysql/mysql.sock

User=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

Symbolic-links=0

Default-character-set=utf8

[mysqld_safe]

Log-error=/var/log/mysqld.log

Pid-file=/var/run/mysqld/mysqld.pid

[root@xuegod64 ~] # service mysqld restart

11) create a test database

Note: after confirming that the above is UFT8, let's create a test database. Pay attention to your terminal should also be UTF8 coding ah, otherwise Chinese will appear garbled!

[root@xuegod64] # mysql-uroot-p123456

Mysql > create database tomcat

Mysql > use tomcat

Mysql > create table tt (id int,name varchar)

Mysql > insert into tt values (1)

Mysql > insert into tt values (2meme Xuegod')

Ysql > grant all on tomcat.* to tomcat@localhost identified by 'tomcat'

Mysql > exit

[root@xuegod64 ~] # mysql-utomcat-ptomcat # Test tomcat account.

12) Test home page:

[root@xuegod63 ROOT] # pwd

/ usr/local/tomcat/webapps/ROOT

# Note: when copying, delete and add% sign. If you copy this paragraph directly, there will be an extra% at the beginning of the last two lines, so be careful to delete it. [root@xuegod63 ROOT] # vim test.jsp # writes the following:

Step:

Context:

test

Httpd://192.168.1.63:8080/test.jsp

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report