Tomcat JSP Classic Configuration [Nicktai Posted on 2005-3-16 14:13:54]
Tomcat's JSP, Servlet, and JavaBean environments often see how JSP's beginners ask how to configure JSP, servlet, and bean under Tomcat, so I summarize how to configure JSP, servlet, and Ben under Tomcat, I hope to have a beginner Helped. First, the development environment Configuration first step: Download J2SDK and Tomcat: to Sun Official Site (http://java.sun.com/j2se/1.4.2/download.html) Download J2SDK, pay attention to the download version is Windows Offline Installation SDK, it is best to download J2SE 1.4.2 documentation, then download Tomcat (http://www.apache.org/dist/jakarta/tomcat-4/) download Tomcat (download the latest 4.1.x version of Tomcat); 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 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 where you test.java, and type the following command Javac Test.java Java Test At this time, if you see this is 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 Variables (assuming your Tomcat is installed in c: / tomcat): Catalina_Home = C: / Tomcat catalina_base = C: / Tomcat then modifies the classpath in the environment variable, adding the common / lib under the Tomat installation directory (can be added according to the actual addition) servlet.jar 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's Welcome page The instructions are successful.
Step 3: Establish your own JSP app directory 1. WebApps directory for the Tomcat installation directory, you can see the directory of Tomcat, which root, example, tomcat-docs, etc.; 2. Creating a 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: XML Version = "1.0" Encoding = "ISO-8859-1"> my web application display-name> web-app> 5. Newly built a new one under MyApp The JSP page of the test, the file name is index.jsp, the file content is as follows:
Now time is: <% = new java.util.date ()%> center> body> html> 6. Restart Tomcat 7. Open the browser, enter http: // localhost: 8080 / myapp / index.jsp See the current time when it is successful.
Step 4: Establish your own servlet: 1. Creating a servlet program with your most familiar editor (recommended using syntax-check Java IDE), 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 h1> <. / body> html> "); out.flush ();}} 2. Compile Test.java Under C: / Test, compile: C: / Test> Javac Test.java then in C : / Test will generate a compiled servlet file: Test.class 3. Cut the structure test / test.class to% catalina_home% / WebApps / myapp / web-inf / class, that is, cut the Test directory 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: XML Version = "1.0" encoding = "ISO-8859-1"> My Web Application display-name> a application for test. description> test servlet-name> test display-name> a test servlet description> test.test < / servlet-class> servlet> test servlet-name> / test url-pattern> servlet-maping> web-app > The servlet in this paragraph declares the servlet you want to call, and servlet-mapping is the declared servlet "mapping" to address / TEST 5. Ok, restart Tomcat, start your browser, enter http : // localhost: 8080 / myapp / test If you see the output this is a servlet test. The written servlet is successful.
Note: Web.xml and new Class, you must restart Tomcat Step 4: Establish your own bean: 1. Create a new Java program with the editor you are most familiar with (recommended using syntax-check Java IDE). file name TestBean.java, file contents 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, use the following command to compile: C: / test> javac testBean.java then in C: / Test will generate a compiled bean file: TestBean.class 3. Cut the TestBean.class file to% catalina_home% / WebApps / myapp / web-inf / class, 4. New TestBean.jsp file, The content of the file is: <% @ page import = "test.testbean"%>
<% testbean testbean = new testbean ("this is a test java bean.");%> Java bean name IS: <% = TestBean.getName ()%> center> body> html> 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. JVM should be filled in c: / j2sdk / bin gives you a simple configuration ::: JSP environment configuration The first thing is to say, using JDK Tomcat to configure our JSP server, no longer need any stuff, There are many articles to introduce apache, in fact, it is not necessary, and the general learning debug Tomcat can be eligible. After installing JDK, Tomcat will automatically find the JDK installation path before installing, and click "Next". After a period of file copying, final "close", complete the COMCAT installation.
You'd better download a higher version of Tomcat, such as more than 4.1, because it does not need to set too much system variable, right click "My Computer", select "Properties" -> "->" environment variable "->" System Variable ", new Tomcat_Home, value set to your Tomcat where the path is located, such as: D: / Program Files / Apache Group / Tomcat 4.1, the configuration is completed. From the start menu, find the Tomcat option, the order is: Start-> Program -> Apache Tomcat 4.1, select "Start Tomcat", let the JSP server start running, this time open a similar DOS window, will display some related information. If you use the agent to go online, you must first remove the agent, or your JSP program will never be executed. If it is not an agent, this step is skipping. Open the browser and enter: http:// localhost: 8080, if you see a tiger (I don't know whether it is a tiger or cat), congratulations, you have succeeded half. Let's enjoy the joy of success, please enter the following code:
first page title> head>
Today is: h <% = new java. Util.date ()%> h3> body> html> Save the program as: first.jsp, put it in the root directory of Tomcat, and enter: http: / in the browser's address bar /LOCALHOST:8080/first.jsp, (First.jsp) Enter the case of the file name saved by us) Enter, if you don't accident, you should see the shape is like Today is: h fri Apr 11 08:32: 38 CST 2003 results. Note: root is the default virtual directory of Tomcat. What should I do if I want to change my own virtual directory? Please continue to see it.
To change into your own virtual directory, you have to ask Server.xml, this file is a configuration file, in the Tomcat / Conf directory, use any text editing software to open it, let's find the following: The port is the port, we can use other ports to replace it, but cannot be ports occupied by the system (0--1023), Here is simple to mention. Let's find the following statement: context> host> We should find these two statements, if you don't understand E text, you will determine these two statements. Then we change this statement as follows: context> context> host> here's PATH = "/ myjsp" is the virtual directory we configured, and enter http: // localhost: 8080 / myjsp in the address bar later. DOCBASE = "E: / myjsp" is the machine local path, and they form a mapping relationship through this statement. Put the above first.jsp file in the E: / myjsp directory, enter http: // localhost: 8080 / myjsp / first.jsp, is there a feeling of happiness? In the forum, I have seen the most is that many people don't know where the javabean file is put, honestly, I don't know, it is even more unexpected, and ten people have nine different statements, which makes us feel awkward. In fact, this problem is not as complex, we explained in an example: first create a Java program, the code is as follows: package hall; public class simplebean {private string message = "no message specified"; public string getMessage () {Return (});} public void setment (String Message) {this.Message = message;}} Saved as SimpleBean.java, generated a package after compiling, actually equivalent to a directory, that is, simpleBean.class will be stored in Hall In the catalog, it is time to save, and it will be spare.
Enter the following code:
Reusing javabeans in jsp title> head>
Message: i> h1> body> Save the virtual directory E: I have just created E: / myjsp under, and named: beantest.jsp. Where should we put the Hall (package) directory? Don't worry, let's create a folder web-infruit under E: / myjsp, then build a CLASSES folder under web-infers, and finally put the Hall directory under the class, of course, the byte code file under HALL SimpleBean.class also has to move, and SimpleBean.java is placed in the same directory with beantest.jsp (you can try it yourself). Ok, I'm big, restart the machine (if you have tried it many times, this step must be done), enter: http:// localhost: 8080 / myjsp / beantest.jsp, what did you see? ? Oh, don't tell me what you have not seen, it is definitely what you set up. Ok, the article is written, I am just a rookie, so there is a way you write, please advise.
I wish you a JSP trip! ! ! Java Learning - Technical Article Center's initiator asked: "How to configure environment variables" "How to run servlet"? This kind of problem is too much. Now I will write a beginner entry to read, so that I will have a guiding role in beginners! The first is the download tool: I suggest that beginners use EditPlus JDK, I think if I use, for example, JB, Eclipse, JCREATOR, although it is convenient to start, but even how to configure environment variables, it is difficult to achieve it. It is known to download: editplus (the latest version is V2.11): http://count.skycn.com/softdown.php?id = 3641 & url = http: //sc-http.skycn. Net / down / EPP211A_CN.EXE (I will find it yourself by the registration code, there are many online) JDK (the latest version is java2sdk1_4_2): http://count.skycn.com/softdown.php? id = 3116 & url = http: / /sc-http.skycn.net/down/j2sdk-1_4_2-windows-i586.exe (this is for windows) then installing JDK, I am installing it from the C: / JDK directory: then is the problem with classpath Just as the operating system uses PATH to search for executables, the Java runtime also traversed ClassPath to find classes, even if the HelloWorld is simple, JVM will also traverse each path defined by classpath until the corresponding file is found. . I believe that the system used is not 2K is XP, then set the path: My Computer -> Properties -> Advanced -> Environment Variables and add: C: / JDK / Bin ;; / JDK / lib can also be configured such: C: / JDK / BIN;; C: /JDK/LIB/dt.jar; c: /jdk/lib/tools.jar ★ Remember: Environment variables. Remember Less, it represents the current path, if there is less error, etc.! Dt.jar is about the class library of running the environment, and Tools.jar is about the class library of some tools. If there is no configuration: C: / JDK / BIN, "JavaC 'is not internal or external command, nor can it run Or batch files. "This error.
Then you will write the program below: First is (HelloWorld.java), open EditPlus, create a new Java file, please enter the following input, you want a word unlike, and select the case: public class helloworld {public static void main String [] args) {system.out.println ("Hello, World!");}} Then save this file (Ctrl S) to HelloWorld.java, remember the case must be divided, is helloworld.java is not HelloWorld.java or other below, start -> Run -> CMD Switch to the current directory in the console: Javac HelloWorld.java Java HelloWorld You will see the output Hello, World! Didn't come out? I have eaten the computer :)) Javac is the compile command, which compiles helloWorld.java to HelloWorld.class Java is the explanation command, and JVM explains the HelloWorld.class. At this time: 1. If Exception in Thread "Main" java.lang.noclassdefounderror: HelloWorld is that you didn't add that in the environment variable. (Dot) 2.
If an Exception in Thread "Main" java.lang.nosuchmethoder: main or helloWorld.java: main or helloworld.java: PUBLIC CLAS HELLOWORLD MUST BE Defined in a file caled "HelloWorld.java". That is, you didn't have a case write to this HelloWorld Or keep it as helloWorld.java this name must be said to the environment variable as the name of the public class, let me first say how to compile and run in EditPlus, set in Tools-> parameter settings -> Configuring User Tools 1. Add Tools (Add Apps) Menu Words: Compile Java Program Programs: C: /JDK/bin/javac.exe Parameters: File Name Initial Directory: File Directory 2. Add Tool (Add App) Menu text: Run Java Program program: c: /jdk/bin/java.exe parameter: file name (excluding extension) Initial directory: File Directory Tool Group Name You can add, such as Debug Java Program then dropped in Tools drop-down menu You will see two options for Compile Java Program and Run Java Program, you can use Ctrl 1 compile and Ctrl 2 running the program to discuss the run of the servlet: First, you need to run the servlet, you need JSP / servlet Container, I suggest that beginners use Tomcat Tomcat (latest version 5.0): http://cvs.apache.org/builds/jakarta-tomcat-5-bin-20030725.zip then put this compression Packing to: c: / tomcat and then configure environment variables: Add three system variables: java_home: C: / JDK Tomcat_Home: C: / Tomcat ClassPath:% java_home / lib;% Tomcat_Home% / lib Tomcat environment variable After the configuration is complete, check whether tomcat can run: Go to the C: / Tomcat / bin in the console, run the startup, then there is a window, and jump a big string, and finally Represents Serv ER has been run in the browser to enter http: // localhost: 8080, the welcome interface, indicating that tomcat is no problem, then writing your first servlet import java.io. *; import javax.servlet . *; import javax.servlet.http *;. public class HelloWorld extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "text / html"); PrintWriter out = response.getWriter (); out.println ("