Apache + Tomcat

Source: Internet
Author: User

Environment configuration has always been an annoying task! The purpose of this article is to easily install and configure the SERVLET and JSP execution environment for you.

First, go to java.apache.org to download a tomcat3.1 and tomcat3.2. I use atat3.1. Download a ZIP file. Export it to a directory. For example, c: below. A jakatar-tomcat directory appears under drive C. You can change the directory name. I changed the directory name to tomcat. Open the server. xml file in the c: omcatconf directory. Find the ContextManager tag and you will see:

<Connector className = "org. apache. tomcat. service. simpletcpconne">

<Parameter name = "handler" value = "org. apache. tomcat. service. http.

HttpConnectionHandler "/>

<Parameter name = "port" value = "80"/>

</Connector>

Because tomcat can be used as an independent server, it has its own port number. You can modify the default port number here. Be sure to change it to a port number that is not occupied on your machine. I changed it to 9000.

Now under the c: omcatin directory, you can see a batch file of startup. bat. Before executing this file, if you are using JDK1.2.1 or JDK1.2.2, put the JDK_HOMELIBools.jar file in classpath.

(Note: To set classpath, right-click "my computer" and select "attribute". If no environment variable is available, add one, if yes, add JDK_HOMELIBools.jar)

Now you can execute the startup. bat file. (If an out of environment space prompt appears during execution, the buffer size of your DOS window will be larger. Specific practice: in the DOS window menu, choose Properties> memory> total and increase in the initial environment)

After the execution, startup. bat will start tomcat in another window. Now you can enjoy your achievements. Open your browser and go to the address bar: http: // localhost: 9000/

Then press Enter. You should have seen a jakatar-tomcat. Enter the following in the address bar:

Http: // localhost: 9000/examples/servlet/, and press Enter. You should see a servlet index page. Click the example servlet to check it. Enter

Http: // localhost: 9000/examples/jsp/, and press Enter. You should see a page similar to the servlet index page just now. Click the jsp Example. If the Error 500 is displayed after you click a JSP Example and no other error information is displayed, select Internet option> advanced in the tool menu of IE5, remove the friendly HTTP Error option, and then IE will report the specific error information. The reason is that you did not put tools. jar in classpath.

Now we need to connect tomcat to apache. First download an Apache Web Server from www.apache.org. I use Apache1.3.12. Go to java.apache.org to download a Jserv. I use Jserv1.1.1. Install Apache Web Server first (for example, in the c: apache directory ). After installation, modify the c: apacheconfhttpd. conf file, find the Servername, change it to your favorite name, find the port, and change it to an unused port. Then install Jserv (for example, in the c: Jserv directory ). To install Jserv, you must specify the JVM directory. You can specify the JDK_HOMEBIN directory. You need to specify the directory where the JSDK is located. I want to specify the directory where the JSDK is installed that I downloaded from java.sun.com. (In fact, it is looking for the servlet. jar file. You can specify atatlibservlet. jar for a try. I have not tried it like this)

Apache now supports Servlet, but does not support JSP. Therefore, you need to modify the c: apacheconfhttpd. conf file. Open httpd. conf in a text editor and add: Include "Tomcat_Homeconfomcat-apache.conf" at the end of the file (note that the tomcat-apache.conf file is automatically generated by tomcat after tomcat runs. So you must first open tomcat and then Apache ).

Start tomcat (c: omcatinstartup. bat). When another window appears, Start Apache (c: apacheinapache.exe or Start Apache through the Start Menu ). If Apache is started through the latter at this time, a DOS window disappears after a flash, indicating that there is still a problem with the configuration of Apache + JServ + tomcat. The first method should be used to start Apache, then you can see the specific error information on the screen. If the error message is: apj *: can't be specified more than once per host. (* Represents some strings, such as ApJServLogLevel). You need to modify the c: Jservconfjserv. conf file and comment out the related lines of apj *. You may need to comment out 5-6 lines. If Apache is started normally, you can enter http: // localhost: apache_port/examples/servlet/in the address bar to debug the Servlet through Apache + tomcat. Here, apache_port is the Apache port number, you can modify c: apacheconfhttpd. modify the port number of the conf file. In the address bar, type http: // localhost: apache_port/examples/jsp/to debug JSP.

If you are careful, you will find that the examplesjsp directory is used in the previous test. Now let's show you how to create your own directory. First, create the testdir directory under the c: omcatwebapps directory. Create the jsp directory and classes directory under the testdir directory. Open server. xml in the c: omcatconf directory in the text editor. Before the end tag of <ContextManager>, add:
<Context path = "/testdir" docBase = "webapps/testdir" debug = "0" reloadable = "true">

</Context>

Then put the c: omcatwebappsestdirclasses Directory into the classpath environment variable. Use a text editor to edit the following file.

<Html>

<Body>

<%

Int I;

For (I = 1; I <6; I ++ ){

Out. println ("

}

%>

</Body>

</Html>

Stored as c: omcatwebappsestdi#sphelloworld. jsp. In the address bar, type:

Http: // localhost: apache_port/testdir/jsp/helloworld. jsp check the result.

Test the Servlet. Use a text editor to edit the following file.

Import java. io .*;

Import java. text .*;

Import java. util .*;

Import javax. servlet .*;

Import javax. servlet. http .*;

Public class HelloWorld extends HttpServlet {

Public void service (HttpServletRequest request,

HttpServletResponse response)

Throws IOException, ServletException

{

Response. setContentType ("text/html ");

ServletOutputStream out = response. getOutputStream ();

Out. println ("

Out. println ("

String title = "Hello World ";

Out. println ("<title>" + title + "</title> ");

Out. println ("

Out. println ("<body bgcolor =" white "> ");

Out. println ("<body> ");

Out. println ("

Out. println ("</body> ");

Out. println ("

}

}

Save as c: omcatwebappsestdirclassesHelloWorld. java. After compiling with javac, type http: // localhost: apache_port/testdir/servlet/HelloWorld in the address bar to check the result.


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.