MYECLIPSE2014 Configuring the Tomcat Development Javaweb program JSP and Servlet

Source: Internet
Author: User



1. Installation Preparation



1). Download the installation MyEclipse2014, which is already the latest version.









2). Download Tomcat



Official website: http://tomcat.apache.org/



We choose 8.0: http://tomcat.apache.org/download-80.cgi



Select 64-bit decompression version under Windows: Http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.0.3/bin/apache-tomcat-8.0.3-windows-x64.zip






Download it and then unzip it locally.






2. Download Java, configure JDK



Reference: Building a Java development environment and creating a project using Eclipse






3. Configure the JRE in the MyEclipse



MyEclipse Menu--window--preferences



Go to preference settings









Window--preferences--java--installed Jres--add



Note that the default comes with Jdk7 in MyEclipse2014. If you need another JDK, you can configure it yourself.



You need to select a standard VM:









Select the installation path of the JDK7 that we have configured in front of the non-MyEclipse2014 C:\Program files\java\jdk1.7.0_51









Tick the new JDK









Window--preferences--java--compiler






Set the version of the Java compiler









4. Configuring Tomcat for MyEclipse



Window--preferences--myeclipse--servers--tomcat



Choose the Tomcat version, we choose Tomcat 8.x here, note that the first to check Tomcat 8.x server to enable, otherwise it does not work!



The Tomcat unpacking package that was extracted before the Tomcat home directory selection









Reconfigure the Tomcat JDK for our previously configured JDK









However, you can also add the required JDK here:









There is also a need to be aware of where we need to revise.



Content Assist, Editor-in-perferences, Window------the bottom-right column, find Auto-activation, below three options to find a second "auto Activ ation triggers for Java: "Options
You'll see a "." In the text box that follows. Exist. This means: only enter "." Then there will be code hints and auto-completion, and the place we want to modify is here. Put the "." In the text box Replace it with "abcdefghijklmnopqrstuvwxyz. Search" so that you can write Java code in Eclipse and press any character in "[email protected]" for code hints.









5. Create a new Java Web project



MyEclipse Menu Bar--file--web Project



Create a new Web project









Project name fills in its own project names, such as HelloWorld.



You need to select the Java EE version and the target runtime for the previously configured TOMCAT8:









Next









It is best to check the automatic generation of Web. XML, if you need to modify the website root name, you can modify content directory as required









The project was built









On the project, right-click the Popup Properties dialog box and modify the text encoding to UTF-8:









6.JSP Output Current Time



Open the index.jsp, note that you need to open the JSP file in a code-only way by right-clicking the open with "MyEclipse JSP Editor", otherwise it will be opened by default in visual Visual mode.









Write the code in INDEX.JSP:





[Java]View Plaincopy< param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. <body>
    2. Current Time:<br>
    3. <%
    4. Date data=new Date ();
    5. Out.write (Data.tolocalestring ());
    6. %>
    7. </body>



To start Tomcat:












You can view the output information in the console window:









To deploy the code into tomcat:









The deployment will show--successfully deploy









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









To view the results of a run, you can enter a URL in the following Web browser window to view:









You can also enter a URL in the browser to view:









Automatic compilation of 7.Myeclipse and automatic deployment of Tomcat



Configuration file Conf/server.xml in Tomcat


[HTML]View Plaincopy< param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. <Host name="localhost" appbase="WebApps"
    2. unpackwars="true" autodeploy="true">



If autodeploy= "true" then Tomcat will automatically deploy, so-called automatic deployment is no need to restart Tomcat to automatically detect changes in the application's/web-inf/lib and/web-inf/classes directories, automatically loading new applications , we can change the application without having to restart Tomcat.






Window--preferences--general--workspace



By default, the build automatically is checked so that MyEclipse is automatically compiled for Tomcat, and Tomcat's configuration file conf/server.xmlautodeploy= "True", which means With redeploy the situation was hardly. But MyEclipse is not very stable, sometimes, can not be released automatically, must be redeploy. So when you restart Tomcat and don't get your expected results, consider redeploy.







The following is the removal of the check build automatically after modifying the JSP file, point "save" will not be automatically compiled into Tomcat:









8.Servlet Output Current Time



Right-click on the SRC directory to create a new Servelet file:









You need to fill in the package name and the servlet name name, the default is inherited from HttpServlet, the default check Doget and Dopost and other methods do not change:









Tick automatically generate a map file in the Web. xml file:









Automatically generated code in the Web,xml file:





[HTML]View Plaincopy


<servlet>
<servlet-name>timeprint</servlet-name>
<servlet-class>com.mc.demo.timeprint</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>timeprint</servlet-name>
<url-pattern>/servlet/timeprint</url-pattern>
</servlet-mapping>



You can modify url-pattern to the desired format:












In the servlet file, MyEclipse has automatically generated some code for us, we just need to add the code we want.






Add code to output the current time


[Java]View Plaincopy
    1. SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Set Date format
    2. Out.println (Df.format (new Date ())); New Date () to get the current system time


Note that if there is a red fork indicating the need to introduce the corresponding package file, when the mouse cursor over the code will automatically pop up the prompt box, click InPort to import the corresponding package:









The complete doget code is as follows:








[Java]View Plaincopy

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 ("
Out.println ("<BODY>");
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Set Date format
Out.println (Df.format (new Date ())); New Date () to get the current system time
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}

The page header has automatically added the Automatically imported package:





[Java]View Plaincopy
    1. Import Java.text.SimpleDateFormat;
    2. Import Java.util.Date;








Click Save compiled files or JSP files are automatically saved to the corresponding folder in Tomcat:



In Tomcat's WebApps directory you can see that the generated Timeprint.class file is up to date,



Enter Address Http://localhost:8080/HelloWorld/servlet/Timeprint you can see in the Web browser that the servlet has already output the current time and can compare the two.









Article for: http://blog.csdn.net/21aspnet/article/details/21867241



Thank you for the help that the author has given me in my studies.



MYECLIPSE2014 Configuring the Tomcat Development Javaweb program JSP and Servlet


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: info-contact@alibabacloud.com 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.