MyEclipse2014 configure Tomcat to develop Java Web program JSP and Servlet

Source: Internet
Author: User

1. Installation preparation

1) download and install MyEclipse2014, which is the latest version.

2) download Tomcat

Official Website:

We choose 8.0:

In windows select 64-bit unzipping:

Download the package and decompress it locally.

2. download Java and configure JDK

Reference: Build a Java development environment and use eclipse to create a project

3. Configure JRE in MyEclipse

Myeclipse menu -- Window -- Preferences

Go to preference settings

Window -- Preferences -- Java -- Installed JREs -- Add

Note: In MyEclipse2014, JDK 7 is provided by default. If you need other jdk, you can configure it by yourself.

Select Standard VM:

Select the installation path C: \ Program Files \ Java \ jdk1.7.0 _ 51 from the jdk7 installation path not included in MyEclipse2014.

Select the new jdk

Window -- Preferences -- Java -- Compiler

Set the Java compiler version

4. Configure Tomcat for MyEclipse

Window -- Preferences -- MyEclipse -- Servers -- Tomcat

Select Tomcat version. Here we select Tomcat 8.x. note that you must first check Tomcat 8.x server as Enable, otherwise it will not work!

Select the decompressed Tomcat package in the tomcat home directory.

Then configure the Tomcat jdk.

However, you can Add the required JDK here:

We also need to modify the settings.

Window-> Perferences-> Java-> Editor-> Content Assist. In the bottom column on the right, find auto-Activation. There are three options, find the second "Auto activation triggers for Java:" option

A "." exists in the text box after it. This indicates that the code prompt and auto-completion will only be displayed after "." is entered. Here we want to modify it. In the text box, "change to" abcdefghijklmnopqrstuvwxyz. search ". In this way, you can write Java code in Eclipse to press" abcdefghijklmnopqrstuvwxyz. any character in @ will receive code prompts.

5. Create a Java web project

MyEclipse menu bar -- File -- Web Project

Create a web Project

Enter your Project name, such as HelloWorld.

You need to select the Java EE version and Target runtime as the configured Tomcat 8:

Next step

It is recommended that you select "automatically generate web. xml" here. To modify the web root directory name, you can modify "Content directory" as needed.

Built projects

Right-click the project to bring up the Properties dialog box and modify text encoding to UTF-8:

6. JSP output current time

Open index. jsp. You must right-click Open with "MyEclipse JSP Editor" to Open the JSP file in pure code mode. Otherwise, it will be opened in Visual mode by default.

Write code in index. jsp:

<Body> current time: <br> <% Date data = new Date (); out. write (data. toLocaleString (); %> </body>

Start Tomcat:

You can view the output information in the Console window:

Deploy the code to Tomcat:

After deployment, -- Successfully deploy is displayed.

To view the server information, click the following Servers window:

To view the running result, you can enter the URL in the following Web Browser window to view it:

You can also enter the same URL in the browser to view it:

7. automatic compilation of Myeclipse and automatic deployment of Tomcat

In the tomcat configuration file conf/server. xml

      <Host name="localhost"  appBase="webapps"            unpackWARs="true" autoDeploy="true">

If autoDeploy = "true", tomcat is automatically deployed, the so-called automatic deployment is no need to restart tomcat can automatically detect the application/WEB-INF/lib and/WEB-INF/classes directory changes, automatic loading of new applications, we can change the application without restarting tomcat.

Window -- Preferences -- General -- workspace

By default, Build automatically is selected, so that myeclipse will automatically compile and release to tomcat, And the configuration file conf/server. xmlautoDeploy = "true" of tomcat, that is, there is almost no redeploy. However, myeclipse is not stable. Sometimes, it cannot be automatically released and must be redeploy. So when you restart tomcat and still cannot get your expected results, consider redeploy.

The following shows how to modify the JSP file after checking Build automatically. Click "save" and the file will not be automatically compiled into tomcat:

8. Servlet output current time

Right-click the src directory to create a servelet file:

You need to enter the package Name and servlet Name, which are inherited from httpServlet by default. Do not change the doGet and doPost methods selected by default:

Check to automatically generate a Map file in the web. xml file:

Automatically generated code in the web and xml files:

  <servlet>    <servlet-name>Timeprint</servlet-name>    <servlet-class></servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>Timeprint</servlet-name>    <url-pattern>/servlet/Timeprint</url-pattern>  </servlet-mapping>

You can modify the url-pattern to the desired format:

In the servlet File, myeclipse has automatically generated some code for us. We only need to add the code we need.

Add code to output current time

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // set the date format out. println (df. format (new Date (); // new Date () is used to obtain the current system time.

Note: if there is a Red Cross, the corresponding package file needs to be introduced. When you move the cursor over the code, a prompt box will pop up automatically. Click inport to import the corresponding package:

The complete doGet code is as follows:

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html"); PrintWriter out = response. getWriter (); out. println ("<! Doctype html public \ "-// W3C // dtd html 4.01 Transitional // EN \"> "); out. println ("<HTML>"); out. println ("<HEAD> <TITLE> A Servlet </TITLE> </HEAD>"); out. println ("<BODY>"); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // set the date format out. println (df. format (new Date (); // new Date () is used to obtain the current system time out. println ("</BODY>"); out. println ("</HTML>"); out. flush (); out. close ();}
Automatically imported packages have been added to the page header:

import java.text.SimpleDateFormat;import java.util.Date;

Click to save the compiled file or jsp file that has been automatically saved to the corresponding folder in tomcat:

In the webapps directory of tomcat, we can see that the generated Timeprint. class file is up to date,

Enter the http: // localhost: 8080/HelloWorld/servlet/Timeprint address. You can see that the servlet has output the current time in the Web Browser. You can compare the two.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.