This article, "Learning from servlet (i)", describes how to use MyEclipse to create a Web project and how to configure a Tomcat server in MyEclipse.
In MyEclipse, you create a new Web Project and you see:
We need to be aware of the following points:
Project name: The project name, which represents the directory name of the Web application, is generated in the Tomcat "WebApps" directory when the Web App is published on the server, so there is a "myservlet" directory.
"Source Folder": Represents the directory in which the source code is stored in this Web application, which is specifically used to place some of the ". Java" files that are written, such as programs written for the servlet.
"Web root Folder": Web App Root name, is actually just a map of the Web application directory, that is, the name "WebRoot" directory, but I created a "Myservlet" Web project name of a map name only, There is no such directory on the hard disk.
"Context root URL": The virtual directory name that is mapped to "Web root folder" for browser URL access. Previously said "WebRoot" This directory is not there, because it is only a mapping of "myservlet", so "Context root URL" is actually a Web application name of a mapped virtual directory name (that is, the external access path).
"Java EE specification level": The version number of the selected Java EE, which is selected to import the corresponding version of the Java EE jar package, such as the jar package containing the servlet. (Choosing 1.4 will not conflict with more advanced apps, I'll choose 5.0 first).
When you click OK, a window pops up:
Because I chose the j2ee5.0 version, MyEclipse asked me if I changed the compiler to 5.0, and said that my current workspace compiler defaults to 6.0 (6.0 is the JDK chosen by the default compiler for MYECLIPSE10, I actually imported and selected 7.0 JDK, but it doesn't say 7.0) because We do not need to change the compiler to 5.0, just select No "no".
Such a Web project was created in MyEclipse, as shown in:
The JRE System Library is the jar package required to place the J2SE, which is the standard package that is normally used
The Java EE 5 Library is the jar package needed to place the EE, so that we don't need to go to Tomcat's "Lib" Directory to import it using the servlet.
The "WebRoot" directory was previously said to be the map of the directory where the Web application resides, as well as the organization of the Web application (see "Tomcat Detailed usage Learning (iii)"), with the necessary "Web-inf" directory, And this directory also has the "Lib" directory we said necessary and "web. xml" file, you may ask why there is no "classes" directory, don't worry, when we write the servlet program and publish the Web app to the Tomcat server, MyEclipse will automatically help us generate the "classes" directory and put the compiled Java file generated bytecode files in that directory, so we just need to write the servlet program in the "src" directory.
Note that although we created the Web project in MyEclipse, the project is only in the MyEclipse workspace, and only when we publish the Web project in the server will the server automatically apply the Web to Tomcat's "WebApps" Generated in the directory.
So how do you configure and open the Tomcat server in MyEclipse and publish your Web app to the server?
In MyEclipse, click "Windows"---> "Preferences ...", select "MyEclipse"---> "Servers" and select "Tomcat" to see the following:
Because my computer is loaded with TOMCAT7, so choose "Tomcat 7.x" so that we can configure "Configure Tomcat 7.x", click as shown below:
Just choose Tomcat's home directory, and remember to choose "Enable"!!!
Finally, we start the server in MyEclipse:
You can see the console window prompting the server to start:
At this point we also need to publish the Web app, click on the icon that just started the server to the left:
Select the Web App project name you want to publish and click the "Add" button:
Select the type of server you just turned on in server, and you can see that MyEclipse automatically places our web app in the Web app directory that the server you just opened can run:
After clicking "Finish", you can see if the Web App was published successfully:
After the successful release, we can go to the browser to verify, because when the "Myservlet" was created, MyEclipse also helped me to create a index.jsp file, and in the Web. xml file set the Index.jsp home page, there are two ways to access:
A possible problem for starting Tomcat in MyEclipse:
Bad version number in. class file
This problem occurs because Java programs written in Web applications are compiled with a higher version of the JDK, and Tomcat uses a lower version of the JDK, so Java programs in the Web app cannot be run.
First we have to realize that Tomcat is also a Java program that requires JDK support at runtime, and the Tomcat JDK used in MyEclipse is set by MyEclipse, so if we want Tomcat to run the Web app, it should be configured as:
It is important to choose a suitable JDK for the Tomcat server.
Learning from the servlet (ii)