Windows Tomcat configuration Daquan

Source: Internet
Author: User

Configuration of the JSP, servlet, and JavaBean environments under Tomcat

First step: Download J2sdk and Tomcat: to the Sun official site () Download j2sdk, note the download version of the SDK for Windows Offline installation, and preferably download j2se 1.4.2 documentation, Then go to the Tomcat official site (http://www.apache.org/dist/jakarta/tomcat-4/) to download tomcat (download the latest 4.1.x version of Tomcat);

Step Two: Install and configure your J2SDK and Tomcat: Perform J2SDK and tomcat setup, and then install by default.

1. After installing the J2SDK, you need to configure the environment variables, add the following environment variables to the system variable, environment variable, properties----let's assume your j2sdk is installed in c:\j2sdk1.4.2:
java_home=c:\j2sdk1.4.2
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar; (.; Must not be less because it represents the current path)
Path=%java_home%\bin
You can then write a simple Java program to test whether the J2SDK has been successfully installed:

 1  public  class   test{ 2   Public  static  void   Main (String args[]) { 3  System. out . println ( " this is a test program.   " );  4  }  5 } 

Save the above program as a file named Test.java.
Then open a command Prompt window, CD to your Test.java directory, and type the following command
Javac Test.java
Java Test
At this point, if you see the print out of this is a test program. The installation is successful, if you do not print out this sentence, you need to carefully check your configuration situation.

2. After installing Tomcat, add the following environment variable (assuming your Tomcat is installed on the C:\tomcat) in the system variable, advanced environment variable, properties, My Computer
Catalina_home=c:\tomcat;
Catalina_base=c:\tomcat;
Then modify the environment variables in the CLASSPATH, the Tomat installation directory under the Common\lib under the Servlet.jar appended to the CLASSPATH, the modified classpath as follows:
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;%catalina_home%\common\lib\servlet.jar;
You can then start Tomcat, Access http://localhost:8080in IE, and if you see the Tomcat Welcome page, the installation is successful.
Step three: Build your own JSP app directory
1. To the WebApps directory of the Tomcat installation directory, you can see Root,examples, tomcat-docs such as the directory of Tomcat;
2. Create a new directory under the WebApps directory named MyApp;
3.myapp Create a new directory Web-inf, note that the directory name is case-sensitive;
4.web-inf Create a new file, Web. XML with the following content:

1 <?XML version= "1.0" encoding= "Iso-8859-1"?> 2 <!DOCTYPE Web-app3 Public "-//sun Microsystems, INC.//DTD Web application 2.3//en"4 "Http://java.sun.com/dtd/web-app_2_3.dtd"> 5 <Web-app> 6 <Display-name>My WEB Application</Display-name> 7 <Description> 8 A application for test.9 </Description> Ten </Web-app> 

5. Create a new test JSP page under MyApp with the file name index.jsp and the file content as follows:

1 <HTML>2   <Body>3     <Center> 4Now time is:<%=Newjava.util.Date ()%> 5     </Center>6    </Body>7 </HTML>


6. Restart Tomcat
7. Open the browser, enter http://localhost:8080/myapp/index.jsp to see the current time, the description is successful.
Fourth step: Build Your own servlet:

1 Package test;2 import java.io.IOException;3 import Java.io.PrintWriter;4 import javax.servlet.ServletException;5 import Javax.servlet.http.HttpServlet;6 import javax.servlet.http.HttpServletRequest;7 import Javax.servlet.http.HttpServletResponse;8  Public classTest extends HttpServlet {9 protected voiddoget (httpservletrequest request,httpservletresponse response)Ten throws Servletexception, IOException { OnePrintWriter out=Response.getwriter (); A            out. println (" -Test.");  -            out. Flush (); the      }  -}
Test

2. Compiling
Place Test.java under C:\Test and compile with the following command:
C:\test>javac Test.java
A compiled servlet file is then generated under C:\Test: Test.class
3. Cut the structure Test\test.class to%catalina_home%\webapps\myapp\web-inf\classes, that is, cut the Test directory into the classes directory, if the classes directory does not exist, Just create a new one. Now there's a Test\test.class file directory structure under Webapps\myapp\web-inf\classes.
4. Modify Webapps\myapp\web-inf\web.xml, add Servlets and servlet-mapping
The edited web. XML is shown below, red for the added content:

1 <?XML version= "1.0" encoding= "Iso-8859-1"?> 2 <!DOCTYPE Web-app3 Public "-//sun Microsystems, INC.//DTD Web application 2.3//en"4 "Http://java.sun.com/dtd/web-app_2_3.dtd"> 5 <Web-app> 6 <Display-name>My WEB Application</Display-name> 7  <Description> 8 A application for test.9  </Description> Ten <servlet>  One   <Servlet-name>Test</Servlet-name>  A   <Display-name>Test</Display-name>  -   <Description>A Test Servlet</Description>  -  <Servlet-class>Test. Test</Servlet-class>  the  </servlet>  -  <servlet-mapping>  -    <Servlet-name>Test</Servlet-name>  -    <Url-pattern>/test</Url-pattern>  +  </servlet-mapping>  - </Web-app> 


The servlet in this passage declares the servlet you want to invoke, while servlet-mapping is "mapping" the declared servlet to the address/test
5. OK, start Tomcat, start the browser, enter Http://localhost:8080/myapp/Test if you see the output of this is a servlet Test. The servlet written is successful.
Note: Modify the Web. XML and the new class to restart Tomcat
Fourth step: Build Your own bean:
1. Create a new Java program with your most familiar editor (recommended Java IDE with Syntax checker), with the file name Testbean.java and the file contents as follows:

1  Packagetest;2  Public classtestbean{3 PrivateString name =NULL; 4  PublicTestbean (String strname_p) {5        This. name=strname_p;6 } 7  Public voidsetName (String strname_p) {8        This. name=strname_p;9 } Ten  PublicString GetName () { One      return  This. Name; A    }  -}

2. Compiling
Place Testbean.java under C:\Test and compile with the following command:
C:\test>javac Testbean.java
A compiled bean file is then generated under C:\Test: Testbean.class
3. Cut the Testbean.class file to%catalina_home%\webapps\myapp\web-inf\classes\test,
4. Create a new testbean.jsp file with the following file contents:

1 <%@ PageImport= "Test. Testbean " %> 2 <HTML>3   <Body>4    <Center> 5      <% 6 TestbeanTestbean=newTestbean ("This is a test Java bean.");7      %> 8Java Bean name is:<%=testbean.getname()%> 9    </Center>Ten   </Body> One </HTML> 

5. OK, restart Tomcat, launch the browser, enter http://localhost:8080/myapp/TestBean.jsp if you see the output Java Bean name Is:this is a test Java bean. In the description of the written B EAN was successful.
This completes the configuration of the JSP, Servlet, and JavaBean under Tomcat as a whole. The next thing to do is to read more books, read more people's good code, and write more code

Windows Tomcat configuration Daquan

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.