Configuration of JSP, Servlet, JavaBean, and Struts environments under Tomcat

xiaoxiao2021-03-06  63

I often see how JSP's beginners ask how to configure JSP, servlet, and bean under Tomcat, so I have summarized how to configure JSP, Servlet, and Ben under Tomcat, I hope to help those beginners.

Step 1: Download J2SDK and Tomcat: to Sun Official Site (http://java.sun.com/j2se/1.4.2/download.html) Download J2SDK, note the download version of the SDK for Windows Offline Installation, and best Download J2SE 1.4.2 Documentation, then download Tomcat (download the latest 4.1.x version of Tomcat) to Tomcat Official Site (http://www.apache.org/dist/jakarta/tomcat-4/);

Step 2: Install and configure your J2SDK and Tomcat: Perform the J2SDK and Tomcat installer, then press the default settings.

1. After installing J2SDK, you need to configure the environment variable, add the following environment variables in my computer -> Properties -> Advanced -> Environment Variables -> System Variables (assuming your J2SDK 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

Then you can write a simple Java program to test whether J2SDK has been installed successfully:

Public class test {

Public static void main (string args []) {

System.out.println ("This Is A Test Program.");

}

}

Save the above program as file named Test.java file.

Then open the command prompt window, CD to your Test.java, and type the following command

Javac Test.java

Java Test

At this time, if you see the THIS A TEST Program. If you have successful installation, if you don't print this sentence, you need to check your configuration carefully.

2. After installing tomcat, add the following environment variables in my computer -> Properties -> Advanced -> Environment Variables -> System Variable (assuming your Tomcat is installed in C: / Tomcat):

Catalina_Home = C: / Tomcat;

Catalina_base = C: / Tomcat;

Then modify the classpath in the environment variable, add servlet.jar under the Tomat installation directory to the ClassPath, the modified ClassPath is 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: 8080 in IE, if you see the Tomcat's welcome page, the installation is successful.

Step 3: Establish your own JSP app directory

1. To Tomcat's installation directory's webapps directory, you can see the directory of Tomcat's Tomcat, which root, example, tomcat-docs.

2. Create a directory in the webapps directory, namely myapp; 3. MYAPP New Directory Web-INF, note that the directory name is case sensitive;

4.Web-infault new file web.xml, the content is as follows:

Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"

"http://java.sun.com/dtd/web-app_2_3.dtd">

My Web Application

A Application for Test.

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

Now Time Is: <% = new java.util.date ()%>

6. Restart Tomcat

7. Open the browser, enter http: // localhost: 8080 / myApp / index.jsp See the current time is successful.

Step 4: Establish your own servlet:

1. Use the editor you are most familiar with (it is recommended to use the Java IDE with syntax check), the file name is Test.java, the file content is as follows:

Package test;

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 test extends httpservlet {

Protected Void Doget (HttpservletRequest Request, HttpservletResponse Response)

Throws servletexception, ioException {

PrintWriter out = response.getwriter ();

Out.println ("

this is a servlet test. ");

Out.flush ();

}

}

2. Compilation

Put Test.java under C: / Test, compile the following command:

C: / test> javac test.java

Then generate a compiled servlet file under C: / Test: Test.class

3. Cut the structure Test / Test.class to% catalina_home% / WebApps / myApp / web-inf / class, that is, cut that Test directory to the class content, if the class content does not exist, will create a new one. Now WebApps / MyApp / Web-INF / CLASSES has a Test / Test.class file directory structure 4. Modify WebApps / MyApp / Web-INF / Web.xml, add servlet and servlet-mapping

The edited Web.xml as follows, red is added:

Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"

"http://java.sun.com/dtd/web-app_2_3.dtd">

My Web Application

A Application for Test.

test

Test

a test servlet

test.test

test

/ test

This paragraph declares that the servlet you want to call, and servlet-mapping is a represented servlet "mapping" to address / TEST

5. Ok, start Tomcat, start the browser, enter http: // localhost: 8080 / myapp / test If you see the output this is a servlet test. The written servlet is successful.

Note: Modify Web.xml and new Class, you must restart Tomcat

Step 4: Establish your own bean:

1. Creating a Java program with the editor you are most familiar with (it is recommended to use the Java IDE with syntax), the file name is TestBean.java, the file content is as follows:

Package test;

Public class testbean {

Private string name = NULL;

Public TestBean (String Strname_P) {

THIS.NAME = STRNAME_P;

}

Public void setname (String strname_p) {

THIS.NAME = STRNAME_P;

}

Public string getname () {

Return this.name;

}

}

2. Compilation

Place TestBean.java in C: / Test, compile the following command: C: / test> Javac TestBean.java

Then generate a compiled bean file under C: / Test: TestBean.class

3. Cut the testbean.class file to% catalina_home% / WebApps / myapp / web-inf / classes / TEST,

4. Create a TestBean.jsp file, the file content is:

<% @ Page import = "Test.TestBean"%>

<%

TestBean Testbean = New TestBean ("This Is A Test Java Bean);

%>

Java bean name is: <% = TestBean.getName ()%>

5. Ok, restart Tomcat, start the browser, enter http: // localhost: 8080 / myapp / testbean.jsp If you see the output Java Bean name is: this is a test java bean. The written bean is successful.

This completes the configuration of JSP, Servlet, and JavaBean under Tomcat. The next thing you need is to read more books, read more people's good code, yourself to write your code to enhance your ability to develop in this regard.

Configuring struts

After extracting the downloaded jakarta-struts-1.1.zip, there is a Struts-Blank folder in the Jakarta-Struts-1.1 / WebApps directory. This is an empty configuration Struts folder, so copy it directly to Tomcat. WebApps directory.

转载请注明原文地址:https://www.9cbs.com/read-88447.html

New Post(0)