9.1 Eclipse's Web Environment Construction (Transfer)

xiaoxiao2021-03-06  43

*********************************************************** *****

Author: Chen Gang, general programmer, had the privilege to Eclipse plug-ins developed over a medium-sized software. Nowhere to learn

Put into paper, temporarily take the title of "Eclipse plugin development guide", will be published by Tsinghua University Press in early 2005.

BLOG:

http://blog.9cbs.net/glchengang/

*********************************************************** *****

Chapter 9 Eclipse J2EE Development

Eclipse default installation is not supported by J2EE development. It needs to install a third-party plugin. The main introduction to this chapter is Lomboz. The main development environment is Tomcat Lomboz Struts Hibernate, which is a selected selection of current comparative. Where Tomcat acts as a web server; Lomboz is a tool developed by J2EE; Struts provides powerful MVC mode support; Hibernate replaces the bulky EJB to act as a database of persistence.

All of the above tools and software packs are not only popular, powerful, and free, is a typical match for J2EE development. This chapter will be divided into three levels to gradually expand:

L Lomboz Development under Pure J2EE Development

l Developing Struts J2EE development

l Development of J2EE after integration struts and hibernate

Due to the limited space, this chapter is focused on the installation and configuration of the development environment, ands to demonstrate the specific development operations in a typical and deep example, and finally give a data index of extended knowledge.

In this chapter, in this chapter, CVS is also used to manage all routines. After the title of each section, the version number of this section will be displayed. The specific environment in this chapter is: WindowsXP JDK1.4.2_06 Eclipse3.1M4 CVSNT2.0.58D Tomcat5.0.28 Lomboz3.1.0 Struts 1.2.4.

9.1 Construction of the Web Environment (V0010)

9.1.1 Download CVS Version Precautions

Due to the V0010 version, there are some empty directories, and these empty directories must also be there, otherwise the project will be wrong. This requires a modification of a CVS configuration, as shown in Figure 9.1, open Eclipse's preferences → Group → CVS → Cancel the "Repair Directory" entry.

Figure 9.1 Modifying CVS Configuration

9.1.2 Download and Installation of Tomcat

This section first built Tomcat environment, Tomcat's download and installation, and Eclipse, Lomboz did not have a direct relationship, it is completely independent.

1, download Tomcat

(1) Use IE to open Tomcat download page: http://jakarta.apache.org/tomcat/index.html, select the link "binaries" on the left side of the page, go to the page shown in Figure 9.2:

Figure 9.2 Tomcat project selection

(3) Click the "Tomcat" item identified in the above figure, the page shown in Figure 9.3 appears

Figure 9.3 Specific download item

(4) Download the "5.0.28.exe" item shown in Figure 9.3, the downloaded file name is: jakarta-tomcat-5.0.28.exe

l Note:

l (1) Do not download Tomcat5.5. *, because it requires support for JDK5.0; do not download 4.1. * Edition, it's too weak. Because the installation and configuration between different versions will be different, in order to synchronize this tutorial, you must download version 5.0.28. l (2) If you cannot download multiple thread download tools such as flashget, you can download the "Save As ..." item for the original IE right-click menu.

2, install Tomcat

The process of installing Tomcat is relatively simple, double-click the downloaded download file: jakarta-tomcat-5.0.28.exe starts installation.

(1) Select the installation component. Accept the default check, as shown in Figure 9.4 below.

Figure 9.4 Selecting Components

(2) Select the Tomcat installation directory. Also accept the default value, install it to the C: / Program Files / Apache Software Foundation / Tomcat 5.0 directory, as shown in Figure 9.5 below:

Figure 9.5 installation directory of Tomcat

(3) Select the HTTP listening port (PORT) as shown in Figure 9.6 below. The default port is 8080. If the 8080 port has been occupied by other software on your computer (eg IIS, JBOSS, etc.), you can select an idle port. Finally, set to Tomcat's super administrator admin Set a password (this book is 123456).

Figure 9.6 Setting port and password

(4) Set the JVM used in Tomcat, the default value of this book is "C: / Program Files / Java / J2RE1.4.2_06", as shown in Figure 9.7 below. Many data pointed out that setting ClassPath, Java_Home, PATH, etc. when installing JDK, but this book has never set these environment variables from the first chapter, and can run smoothly, perhaps the new version of JDK1.4.2.2.2 _06 is very good to solve these problems. As you can see from this step, Tomcat is already positioned at the time of installation, and it is not necessary to set it back.

After setting the JVM, click the "Install" button to start installing.

Figure 9.7 Location of the JVM

(5) After the installation is complete, in the Windows "Control Panel" → Administration Tools "→ Services window, you can see a service that Tomcat is registered as Windows, as shown in Figure 9.8 below. Please make sure it is a "manual" mode, this is important when developing, because we will start Tomcat through Eclipse.

Figure 9.8 Windows "Services" window

3, start Tomcat

Although when it is developed, it is to start Tomcat through Eclipse, but now in order to test whether Tomcat is installed successfully, start Tomcat first.

(1) You can run Tomcat configuration interface (as shown in Figure 9.10 below) through Windows "" "" item ", as shown in Figure 9.10 below), this interface contains some parameter settings of Tomcat, Settings don't need to change it. You can start Tomcat directly "click" button.

Figure 9.10 Tomcat configuration interface

(2) Enter "http://127.0.0.1:8080" or "http://127.0.0.1:8080" in the IE browser, where 8080 is the port number set when the installation is installed. If the start is successful, the page shown in Figure 9.11 will appear; Converse, if this page does not appear, the startup is not successful, then you need to check whether the previous installation step is consistent with this book. Figure 9.11 Verify that Tomcat is installed and started

Note: There are two links on the left of the above-picture page: Tomcat Administration, Tomcat Manager, which is used to manage Tomcat, and the login password is the username admin and password 123456 set when installing Tomcat. Among them, Tomcat Adiministration items can set up database connection pools, manage users, and permissions, and other Tomcat server-related settings; Tomcat Manager items are mainly used to publish web management, which can be easily published in Tomcat.

For a solution to Tomcat Chinese issues, see Section 9.4.6.

9.1.3 Download and Installation of Lomboz

When downloading Lomboz, you must choose the Eclipse version to select the corresponding Lomboz version download, otherwise the corresponding version is different, it is very likely that Lomboz cannot be used normally. This chapter is due to the use of CVS, so use the Eclipse3.1M4 version, Lomboz selection the corresponding version 3.1.0.

1, download Lomboz

The download address of Lomboz is: http://forge.objectWeb.org/project/showfiles.php? Group_id = 97, download page as shown in Figure 9.12, select for Eclipse3.1.x Lomboz to download, but also need Simultaneously download the EMF package (as shown in the figure).

The downloaded file name is:

l Org.ObjectWeb.Lomboz_3.1.0.n20050106.zip

L emf-sdo-runtime --i200412160800.zip

Figure 9.12 Lomboz download page

2, install Lomboz

(1) Because Lomboz, EMF is an Eclipse's plugin, just like the installation method of other Eclipse plugins, this book uses a Links-style plug-in installation method. The specific steps are no longer repeated, see the installation steps of the 1.2 section.

The following figure 9.13 is a directory structure after the installation is:

Figure 9.13 Lomboz, EMF installation directory structure

The links directory in Figure 9.13 have two new text files created:

l File Lomboz.Link, the content is only one sentence: pat = Lomboz_3.1.0.N20050106

l File EMF.LINK, only one sentence: pat = EMF-SDO-Runtime-I200412160800

(2) Verify that Lomboz is successfully installed

Start Eclipse. If the installation is successful, select "File" → "New" → "Project" will appear below the Lomboz project shown in Figure 9.14.

Figure 9.14 Verify that LOMBOZ is successfully installed

(3) If the above image is not appeared, please check and try the following:

l Delete the subdirectory Configuration in the Eclipse directory, and then start Eclipse to try it.

l Check if the version of Lomboz is consistent with Eclipse.

l Whether the PATH item in the links file is set correctly. l Lomboz's directory structure is correct: ../ Lomboz_3.1.0.N20050106 / Eclipse / plugins, note lomboz_3.1.0.N20050106 and plugins have an elCIPSE directory.

9.1.4 Lomboz environment settings

After installing Lomboz, you need to do some settings for Tomcat to develop web. The specific steps are as follows:

(1) Open Eclipse's preferences, set the position of the JDK Tools.jar package, this book is "c: /jdk/lib/tools.jar", as shown in Figure 9.15 below:

Figure 9.15 Setting the location of the Tools.jar package of JDK

(2) As shown in Figure 9.16, note that in the drop-down box of the Server Types item, you should select the item corresponding to the current Tomcat version; both Application Server Directory and ClassPath Variable are the installation directory to Tomcat: C: / Program Files / Apache Software Foundation / Tomcat 5.0.

Figure 9.16 Settings in Tomcat in Lomboz

(3) Tomcat5.0.28 version cannot be started in Lomboz, you must do some small modifications. To the Lomboz plugin, "../lomboz_3.1.0.n20050106/eclipse/plugins/com.Objectlearn.jdt.j2ee_3.0.1/servers" directory, you can see the configuration files of a variety of web servers, they all appear on the image 9.16 The Server Types drop-down box, in addition to the Tomcat50x.server file, you don't need it, remove them or back up to other places. Finally, use Notepad to open Tomcat50x.server and replace all "$ {serverrootdirectory} / bin; $ {serverrootdirectory} / commit / endorsed" entry into "$ {serverrootdirectory} / common / endorse", there are two, about 35,39 lines in the file.

9.1.5 JSP HelloWorld

This section will write a JSP HelloWorld to verify that the above Tomcat and Lomboz environment are successful.

1. Set the build path of Java

Open the Eclipse preferences, as shown in Figure 9.17 below, select "Java" → "Build Path" → Select the Folder item. After this step setting, the new Java project (including J2EE project) will default to bin as the output directory.

l Attractive: This step is important, because when you create a J2EE project with Lomboz, you cannot choose "Project Layout" as you creating a normal Java project. At this point, the output directory of J2EE project will be in the root directory, after javabean The Java file will also mix in the class file, very inconvenient. More critical is that the Output path of the JavaBean will be repositioned later. If this is not passed, the output path of the entire project will automatically locate the bin directory when it is positioned. Adjustment, easy to cause confusion. In short, this step must not be omitted.

Figure 9.17 Setting the build path for the Java project

2. Create a J2EE project

(1) Restart Eclipse. Select "File" → "New" → "Project", select the "Lomboz J2EE Project" item shown in Figure 9.18, and then click Next. Figure 9.18 Select "Lomboz J2EE Project" project

(2) Enter the project name MYWEB, and then click Next.

(3) Do not do any changes in the next "Definition Java Build Settings" page, click Next.

(4) The last page is the setting of J2EE, as shown in Figure 9.19, 9.20 below. A total of three steps: Create a Web Modules called Hello; in the Targeted Servers tab, select the "Apache Tomcat v5.0.x" item and click Add to join; click the "Finish" button, Start to generate a J2EE project.

Figure 9.19 Creating a Web Modules

Figure 9.20 Setting Targeted Servers

(5) After completing the above operation, the project structure shown in Figure 9.21 will appear in the Package Resource Manager view.

Figure 9.21 MYWEB project project structure

3, start Tomcat in Lomboz

Right-click "Hello Module", pop up the right-click menu shown in Figure 9.22, select Run Server to start Tomcat (make sure Tomcat or stop status before starting). There are other common menu items in this menu:

l Stop Server - Stop Tomcat

l Debug Server - Start Tomcat with the debug mode. In Web development, it is more common than Run Server.

l Check All JSP Syntax - Check if all JSP files in the project are compliant with specifications

l Undeploy Module - Remove the web module that has been released on Tomcat

l Deploy Module - Publish the web module to Tomcat

l Show in Browser - Preview the effect of this web module in IE.

Figure 9.22 Right-click menu of the Hello module

If Tomcat is started, the string shown in Figure 9.23 is displayed in the console.

Figure 9.23 Output display of the console

4, release the Hello module

Right-click the Hello module to open the right-click menu as shown in Figure 9.22, select the Deploy Module item, and publish the Hello module to Tomcat.

Output from the console of Figure 9.24, it can be seen that Lomboz uses Ant to publish a web page, each line shows the Package release process of the Hello module, and some keyword explanation is given:

l mkdir - create a directory

l Copy - copy file

l Jar - Package with JDK's JAR (here is packaged to Hello.war)

l Delete - Delete file

Figure 9.24 Console output when publishing a Hello module

The Right-click menu of the Hello module is again called, select the show in browser item. Lomboz will open the IE browser to get the effect shown in Figure 9.25, or directly open the IE browser, enter the address "http://127.0.0.1:8080/hello/" to view the effect. This page is displayed in the index.jsp file.

Figure 9.25 Use IE to view web page effect 5, modify index.jsp

Modify Index.jsp to display a HelloWorld string as shown in Figure 9.26 below.

Figure 9.26 Modify Index.jsp

After saving, you must re-release the Hello module with the "Deploy Module" menu item before you can see the modified effect in IE.

6, some related issues

(1) If the modified effect is not seen, it is possible to be the reason for the page cache of IE. You can try the following solution: turn off the IE, then open, go to "Tool" → "INTERNATE Options, click below] 9.27 The Delete File button to remove the IE's web cache.

Figure 9.27 Delete IE page cache

(2) It is also because of the caching reason, after stopping the Tomcat service, even if the refresh web page is still displayed. Put the IE to restart, the page will not be accessible.

(3) If the Tomcat is started in Eclipse, turn off the Eclipse, Tomcat service will also stop. But it is recommended to use the "STOP Server" menu item to stop Tomcat service normally.

9.1.6 How does it not have to be released, a web modification is displayed on IE.

After the previous settings, although it can develop web, each modification must re-release the Hello module, in order to display the modified effect on IE, this is undoubtedly unacceptable when developing, which is at least the development of the development time. Doubled. This section will give a method of displaying a modified effect on IE without publishing.

First, the solution is based on the following knowledge:

l When I publish the Hello module, Lomboz is a WebApps directory that poses the Hello module into a WAR compression package, and then copied to Tomcat's WebApps directory, and the webpage displayed on the IE is from the WAR compression package in this directory, so it cannot be directly displayed The post-JSP file is also understandable.

l Tomcat To publish a web page, not to be a WAR package, or you can issue an uncompressed file directory. In the actual project, it is more convenient to publish the scattered files directly, because this update JSP file is relatively convenient.

l There is a file called Server.xml in the conf subdirectory in the Tomcat installation directory, which can be used to define a new web application.

From the above knowledge, you can draw the following solution: By modifying the server.xml file, define a new web application, locate this web application to the MyWeb project in the Workspace directory of Eclipse. After this setting, the file IE display is the JSP file being written in Eclipse, that is, no need to package this step in the WAR.

The specific steps are as follows:

(1) In order to avoid interference, first remove the original Hello module.

Open Tomcat homepage: http: //127.0.0.1: 8080 /. Select the link "Tomcat Manager", enter the username password (Admin, 123456), resulting in the page shown in Figure 9.28. Click "undeploy" on the right side of the Hello module to release the Hello module from the Tomcat.

Figure 9.28 Undo Tomcat on the Hello module

(2) Modify server.xml to define a new web application

Server.xml The specific path for this file is as follows: C: / Program Files / Apache Software Foundation / Tomcat 5.0 / Conf / Server.xml. This server.xml file is inserted in the item before the of this server is inserted into the setting of , the specific code of is as follows:

Reloadable = "True"

DocBase = "c: /eclipse3.1m4/eclipse/Workspace/myweb/hello"

Workdir = "C: /Eclipse3.1m4/eclipse/Workspace/myweb/bin" />

Code description:

l Note Before adding the above code to the , several properties of can be written, or write it into a row.

l PATH - refers to the name of the web module Hello, which its access address is: http: //127.0.0.1: 8080 / Hello /

l DOCBASE - Defines the location of the JSP file. This point points to the path to the Hello module in Eclipse

l Workdir - Before IE display, JSP must be compiled into a servlet, this property is where the Hello module outputs the location of the servlet. As shown in Figure 9.29 below, because the default output path of the MYWEB project is a MyWeb / Bin directory, the Workdir here is also positioned to this myWeb / bin directory.

Figure 9.29 Default Output Folder of MyWeb Project

(4) Right-click "Hello" module → select Lomboz J2EE → Select Debug Server (or Run Server). Then, enter "http://127.0.0.1:8080/hello/" in the IE browser to view the effect. Finally, it will modify the index.jsp file, directly refresh IE, if you can see the modified effect, indicate all the above settings success.

The Navigator view shown in Figure 9.30 (Note: Not the "Package Explorer" view), Index.jsp generates several new files and directories after IE display (may need to refresh the MyWeb project first):

Figure 9.30 MYWEB project structure

9.1.7 Configuring the database connection pool of Tomcat

In Web development, there is a very large impact on the Web performance, Tomcat has its own connection pool, this section configures Tomcat connection pool.

1, copy the JDBC connection package

Copy the JDBC Connection Package Mysql-Connector-Java-3.0.16-Ga-bin.jar used in Chapter 8 to the C: / Program Files / Apache Software Foundation / Tomcat 5.0 / Common / Lib Directory, Common / Lib is Tomcat The directory of the global reference library, Tomcat automatically loads all JAR packages in this directory at startup.

Some online articles can also copy the database connection package into the web-inf / lib (this "MyWeb / Hello / WEB-INF / LIB directory), this directory is automatically loaded when the Hello module is published. Package catalog. However, if the connection package will be placed in this catalog, the connection package can be used normally without the database connection pool mode; if you use the Tomcat connection pool, it will be errors, and the connection package cannot be used. 2, enter the Tomcat configuration page

Enter the address with IE: http: //127.0.0.1: 8080 / admin /, open the login page configured by the Tomcat server, then enter the username admin, password 123456, enter the TOMCAT configuration page, as shown in Figure 9.31 below:

Figure 9.31 Connection pool settings

Click the tree knots "Data Source" on the left → Select the "Create New Data Source" item of the drop-down box in the upper right corner, then enter the corresponding connection pool configuration information in the table:

l JNDI Name: JDBC / MYSQL - Set the JNDI name of the connection pool and use this name at the Java program.

l Data Source URL: JDBC: MySQL: // LocalHost / SMS - Database Connection String, SMS is a database.

l JDBC Driver Class: com.mysql.jdbc.driver - JDBC connection class.

l User name: root - Database login username.

l Password: ****** - Database login password. This example is 123456.

l Max. Active Connections: 4 - Maximum number of connections. When actual application, it should be large based on the Web usage; when the development, 4 connections are sufficient.

l Max. iDLE Connections: 2 - Maximum idle connection.

l Max. Wait for Connection: 5000 - Maximum Waiting Connection Limit.

l Validation Query: Verified query statements can be not fill.

After completing the above information, click the "Save" button in the lower right corner to save the modification, then click the "Commit Changes" button in the upper right corner to submit the modification.

3, modify Tomcat's server.xml file

Server.xml file specific path: c: / program files / apache software foundation / tomcat 5.0 / conf / server.xml, add a child in the original item:

Reloadable = "True"

DocBase = "c: /eclipse3.1m4/eclipse/Workspace/myweb/hello"

Workdir = "c: /eclipse3.1m4/eclipse/Workspace/myweb/bin">

Global = "jdbc / mysql"

Type = "javax.sql.datasource" />

4, test database connection pool

Name the following test program to Test.jsp, create in the root of the Hello module, and then access it through the IE address: http://127.0.0.1: 8080 / hello / test.jsp. This test program gets a database connection object Connection from the database connection pool, then query the iUser table of the database, and print out with the data column column (Note: iuse is the user table created in Chapter 8). Test.jsp runs as shown in Figure 9.32:

Figure 9.32 Test.jsp results

Test.jsp source code is as follows:

<% @ page contenttype = "text / html; charset = GBK"%>

<% @ Page Import = "java.sql. *"%>

<% @ Page Import = "javax.sql. *"%>

<% @ Page Import = "javax.naming. *"%>

<%

Connection con = NULL;

STATEMENT SM = NULL;

ResultSet RS = NULL;

Try {

InitialContext CTX = New InitialContext ();

DataSource DS = (Datasource) CTX.lookup ("java: comp / env / jdbc / mysql");

CON = ds.getConnection ();

SM = con.createstatement ();

RS = SM.ExecuteQuery ("SELECT * from Iuser");

While (rs.next ())

Out.println (rs.getstring ("name") ",");

} catch (exception e) {

E.PrintStackTrace ();

} finally {

IF (rs! = null) {

Try {

Rs.close ();

} catch (sqlexception e) {}

RS = NULL;

}

IF (SM! = null) {

Try {

Sm.close ();

} catch (sqlexception e) {}

SM = NULL;

}

IF (con! = null) {

Try {

C. close ();

} catch (sqlexception e) {}

CON = NULL;

}

}

%>

Program description:

L <% @ page contenttype = "text / html; charset = GBK"%> This line is a character set for setting up a web page and a key sentence to solve Chinese garbled problems. If it is a pure HTML page, this should be added to the item: .

L <% @ page import = "java.sql. *"%> This sentence is similar to Import Java.sql. * in Java. l CTX.lookup ("java: comp / env / jdbc / mysql"); this COMP / ENV is fixed, JDBC / MySQL is JNDI Name set in front of the pool.

l At the end of the program, you must turn off the database connection (actually returning the connection to the connection pool, not really turning off), otherwise, the JSP page will not be displayed until the connection is exhausted, and the JSP page cannot be displayed.

转载请注明原文地址:https://www.9cbs.com/read-60387.html

New Post(0)