The second part _ Build the Java Web Development environment and configure the Tomcat server &jsp detailed

Source: Internet
Author: User
Tags apache tomcat

MyEclipse Integrated Tomcat

    • first configures Tomcat, that is, the configuration environment variable java_home, path, has been completed in the first lecture, no longer repeat.
    • Eclipse Code style import MyEclipse:
      • Each project should be configured once code style, text size, shortcut keys and so more troublesome, you can use the import and export functions under file. For example, File->general->preferences->export all under Eclipse, generate a *.EPF file, and then use the file->import-> in MyEclipse General->preferences, select the EPF file you just exported to import all.
    • to open MyEclipse, in Window->preferences->myeclipse->servers, select Tomcat 6.x, and the Tomcat home Directory is set to the root directory of Tomcat (for example, I am D:\Program files\ apache-tomcat-6.0.44, by the way, I originally installed the latest version of 8.0.9, but do not know how to integrate into the MyEclipse, because MyEclipse (my version is 8.5) only Tomcat 4/5/6 these few options, So you have to download the 6.0 version of Tomcat to the Apache Tomcat home page and select the radio button below the Tomcat sever to enable, OK.
    • MyEclipse, window->preferences->java->installed jres,add a standard VM, type the native Java installation directory in the JRE home, Go back to the installed JREs directory, select just add the JRE, do not use the myeclipse, and the reason why the JRE is selected here, because Tomcat itself is written in Java, to have the Java Runtime environment to run up. Next, go back to Tomcat 6.x and select the JDK as the one you just added.

After the configuration is complete, in the upper-right corner of the myeclipse there is a named open perspective, click, select MyEclipse Java Enterprise, and then myeclipse in the upper part of a named Run/stop/restart MyEclipse servers icon, click Select to launch Tomcat 6.x. Then enter localhost:8080 in the browser address bar and the Apache Tomcat home page appears for the configuration to succeed.

Develop a simple servlet and JSP using Eclipse in conjunction with Tomcat

Servlets are Java server-side programming, unlike the generic Java applications We wrote earlier, the servlet program runs on the server, there are many kinds of servers, and here we use Tomcat.

    • First build the project:

File->new->web project, named TEST,J2EE specification level, selects Java EE 5.0, OK. Find the Test folder on the hard disk, open Webroot, open web-inf, there are three files Classes,lib, Web. XML, where classes is used to place a Java-compiled ask Price, and Lib is a project-dependent third-party jar,web.xml that is the deployment descriptor for the project.

Second, if you want the project to run in Tomcat, you must tell the Tomcat where the project is:

Go to Tomcat's installation directory, open the Conf directory, open Server.xml, and by setting up this file, Tomcat can find our project and add information before the last few lines of the </Host> tag:

<context path= "/test" docbase= "D:\JavaWeb\test\WebRoot" reloadable= "true"/>

Where context is the meaning of contexts, the Tomcat server can be configured with multiple contexts, each of which corresponds to a Web application; Docbase is the Webroot directory below the project. Path is a logical virtual path (note that the test and project test here have the same name, but there is no relationship between them, you can also change path to "/ABC", etc.), and map to docbase;reloadable to indicate whether you can reload.

Next, in MyEclipse, restart Tomcat, the browser address bar type Localhost:8080/test, display the This is my JSP page, stating that the configuration was successful.

    • We then use the servlet to generate a static page:

Create a new package under the SRC directory named Com.test.servlet; Create a new class Myservlet under the new package, which requires inheriting the HttpServlet class (in general, the servlet needs to inherit this class). Class is implemented as follows:

package Com.test.servlet;import Java.io.ioexception;import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class MyServlet extends httpservlet{@Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Ioexception{process (REQ,RESP);} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, ioexception{ Process (req, resp);} protected void Process (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, ioexception{ Resp.setcontenttype ("text/html"); PrintWriter out = Resp.getwriter (); Out.println ("

Next, configure the Web. XML under the Web-inf directory in MyEclipse, open in the lower left corner, select Source, and delete:

<welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list >

Add in front of </web-app>:

<servlet><servlet-name>MyServlet</servlet-name><servlet-class> Com.test.servlet.myservlet</servlet-class></servlet><servlet-mapping><servlet-name> Myservlet</servlet-name><url-pattern>/myservlet</url-pattern></servlet-mapping>

Restart Tomcat, browser access to Http://localhost:8080/test/MyServlet, "Hello World" is the success.

The process method is slightly modified in the program, the current time of the printing system:

protected void Process (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, ioexception{ Resp.setcontenttype ("text/html"); PrintWriter out = Resp.getwriter (); Out.println ("

Refresh the browser's Http://localhost:8080/test/MyServlet page.

Of course, we can also print the system time in the console, and under what line of statements we just added, add:

SYSTEM.OUT.PRINTLN ("Current system time is:" + new Java.util.Date (). toLocaleString ());

Then refresh the page in the browser several times, and the console also prints out the system time several times.

    • Finally, let's analyze how the servlet executes. Why should we add the above code in Web. Xml? One is to tell the client, and the second is to tell Tomcat how to find my resources, the rule is: in servlet-mapping, according to Url-pattern (where '/' indicates the root path of the current system (here is test), find Servlet-name, Then in the servlet to find a match with his servlet-name, and then find the corresponding Servlet-class, if it is found not instantiated, instantiate it (where the doget processing the client's GET request, Dopost processing the client's post request, And the GET request that is used all through the browser access, immediately executes its Doget method, the Doget method itself calls the process method, after execution, the server returns the following HTML information:

The browser interprets the HTML and displays the "

Hello World

2015-7-2 9:28:13 "

Such a result.

A few things to note:

    • The servlet-name in the servlet-mapping element must match the servlet-name in the servlet element, which is determined by its search rule.
    • The servlet-class in the servlet element must be the package name + class name.
    • Url-pattern, preceded by '/', indicates what URL format the client requests for the servlet, or it can be changed to "/ABC", which requires access to HTTP://LOCALHOST:8080/TEST/ABC in the browser.

The second part _ Build the Java Web Development environment and configure the Tomcat server &jsp detailed

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.