Configuration of JSP, Servlet, and JavaBean Environment under Tomcat

zhaozj2021-02-08  196

Configuration of JSP, Servlet, and JavaBean Environment under Tomcat

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.2classpath =.;% 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 writes a simple Java program to test if 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. Then open the command prompt window, CD to your directory in your test.java, and type the following command javac test.javajava test At this time, if you see this is a test program. If you have successful, if you don't print out 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 Variables (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; then you can start Tomcat, access http: // localhost: 8080 in IE, if you see Tomcat welcome page The words are successful installation.

Step 3: Establish your own JSP App Content 1. WebApps directory for Tomcat installation directory, you can see the directory of Tomcat, which root, example, tomcat-docs, and Tomcat; 2. New directory in the WebApps directory, Name called myapp; 3. MYApp New Directory Web-INF, note that the directory name is case sensitive; 4.Web-infers new file web.xml, the content is as follows:

My Web Application A Application for Test. 5. Newly built a test in MyApp, files Named index.jsp, the file content is as follows:

now time is: <% = new java.util.date ()%> 6. Restart Tomcat7. Open the browser, enter http: // localhost: 8080 / myApp / index.jsp See the current time is successful.

Step 4: Establish your own servlet: 1. Use your most familiar editor (recommended using a syntax-check Java IDE) to create a new servlet.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 <. / Body> (/HTML "";} }2. Compile Test.java under C: / Test, compile: C: / test> javac test.java then in C : / Test will generate a compiled servlet file: Test.class3. Cut the structure Test / Test.class to% catalina_home% / WebApps / myapp / web-inf / class, that is, the cut Test directory to In the CLASSES directory, if the classes directory does not exist, create a new one. Now WebApps / MyApp / Web-INF / CLASSES has the file directory structure 4. Modify WebApps / MyApp / Web-INF / Web.xml, add servlet and servlet-mapping, Web.xml as follows Sign, red is added:

My Web Application a application for test. test test a test servlet test.test test / test This paragraph in this paragraph declares that the servlet you want to call, and servlet-mapping is a servlet that will declare. " 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. Use your most familiar editor (recommended using a syntax check Java IDE) to create a new Java program, 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 Compile TestBean.java Under C: / Test, compile: C: / test> javac testBean.java then generate a compiled bean file under C: / Test: TestBean.class3. Testbean .class file cut to% catalina_home% / WebApps / myapp / web-inf / class Under, 4. New TestBean.jsp file, the file content is: <% @ page import = "test.testbean"%> < HTML>

<% 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.

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

New Post(0)