Tomcat

xiaoxiao2021-03-31  243

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.

First, development environment configuration

Step 1: Download J2SDK and Tomcat: To Sun Official Station (http://java.sun.com/j2se/1.5.0/download.jsp) Download J2SDK, pay attention to the download version of the SDK for Windows Offline Installation, and best Download J2SE 1.5.0 Documentation, then download Tomcat official site (http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi) Download Tomcat (download the latest 5.5.9 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 Variable (assuming your J2SDK installation in c: /j2sdk1.5.0):

Java_Home = C: /J2SDK1.5.0CLASSPATH =.;% 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.javajava 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: / tomcatcatalina_base = C: / Tomcat

Then modify the ClassPath in the environment variable, add 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;

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, you can see the directory of Tomcat's Tomcat's Tomcat, which. 2. Create a directory in the webapps directory, named myapp; 3. MYAPP new one Directory web-inf, note that the directory name is case sensitive; 4.Web-infers 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:

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 () }}

Compile Test.java in 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 Test / Test.class file directory structure

4. Modify WebApps / MyApp / Web-INF / Web.xml, add servlet and servlet-maping

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

PUBLIC "- // sun microsystems, inc." "Http://java.sun.com/dtd/web-app_2_3.dtd">

My Web Application

A Application for Test.

Testtesta test servlettest.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, restart 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 Under C: / Test, compile with 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:

Java Bean Name is:

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

Give you a simple configuration ::::

JSP environmental configuration experience

The first thing to say is, using JDK Tomcat to fully configure our JSP server, no longer need any stuff, there are many articles to introduce apache, in fact, there is no need to use, the general learning debug Tomcat is completely competent.

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 5.5, the configuration is completed. From the start menu, find the Tomcat option, the order is: Start -> Program -> Apache Tomcat 5.5, select "Start Tomcat", let the JSP server start running, open a Window similar to the DOS, 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 first enjoy the joy of success, please enter the following code:

Today is: h

Save the program as: first.jsp, put it in the Tomcat's root directory, then enter: http: // localhost: 8080 / first.jsp, (first.jsp) in the browser's address bar. The case should be consistent) Enter, if not accident, you should see the result of Today IS: HFRI APR 11 08:32:38 CST 2003.

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 sentence:

port = "8080" minProcessors = "5" maxProcessors = "75" enableLookups = "true" redirectPort = "8443" acceptCount = "100" debug = "0" connectionTimeout = "20000" useURIValidationHack = "false" disableUploadTimeout = "true" />

The port = "8080" here is the port, we can use other ports instead, but cannot be ports occupied by the system (0--1023), simply mention it here.

Below we will find the following statement:

We should find these two statements. If you don't understand E text, you will identify these two statements. Then we change this statement as follows:

The path = "/ myjsp" here is the virtual directory we configured, and then enter http: // localhost: 8080 / myjsp in the address bar. 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 explain one example:

Set up a Java program first, the code is as follows:

package hall; public class SimpleBean {private String message = "No message specified"; public String getMessage () {return (message);} public void setMessage (String message) {this.message = message;}}

Saved as SimpleBean.java, after compiling, it will generate a package, which is equivalent to a directory, that is, simpleBean.class will be stored in the Hall directory, ready to stand, in the future.

Enter the following code:

Reusing JavaBeans in JSP

Message:

Save the virtual directory E: / myjsp we just created 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).

Restart the machine (if you have tried it many times, this step must be done), type in the browser: 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.

Java Learning - Technical Article Center

The initiator asked, for example,: "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, I don't know how to configure environment variables, I don't know how to configure environment variables.

It is difficult to achieve it, know how it is

Can be downloaded by the following address:

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 on their own to find it online much) JDK (the latest version is Java2sdk1_5_0): http: //192.18.97.54/ECom/EComTicketServlet/BEGIN30AA3B63E5C2F61C8C26F84B78970A98/-2147483648/926882595/1/627578/627410/926882595/2ts /westCoastFSEND/jdk- 1.5.0_04-OTH-JPR / JDK-1.5.0_04-OTH-JPR: 3 / JDK-1_5_0_04-Windows-I586-P.exe (this is for Windows)

Then install JDK, I put it below the C: / JDK directory:

Then the ClassPath problem:

Just as the operating system uses PATH to search the executable, the Java running environment will also traverse the classpath to find classes, even if the HelloWorld is simple, JVM will also traverse

ClassPath defines each path until the corresponding file is found.

I believe that the system used is not 2K is XP, and then set the path as follows:

My Computer -> Properties -> Advanced -> Environmental Variables

Then add: C: / JDK / LIB after the path of the environment variable: C: / JDK / lib

It can also be configured: C: / JDK / BIN;; c: /jdk/lib/dt.jar; c: /jdk/lib/tools.jar

★ Remember: Environment variables. Remember can not be small, it represents the current path, if there is less error, etc. will say!

Dt.jar is about the class library of the operating environment, Tools.jar is the class library about some tools.

If there is no configuration: C: / JDK / BIN, "Javac 'is not internal or external command will appear, nor is it a running program or batch file." This error.

Then you write below:

The first is (HelloWorld.java), open EditPlus, create a new Java file, please enter the following input, you want a word without leakage, and write 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

Let's run, start-> Run -> cmd

Switch the directory to the current directory in the console:

Javac HelloWorld.javajava HelloWorld

You will see the output of Hello, World! (Didn't come out? I have a computer :))

Javac is a compiled command, which compiles helloWorld.java into HelloWorld.class

Java is an explanation command, and JVM explains the HelloWorld.class.

at this time:

1. If Exception in Thread "Main" java.lang.noclassdefounderror: HelloWorld

That is, you didn't add that in the environment variable. (Dot) 2. If Exception in Thread "Main" java.lang.nosuchmethoderror: main

Or HelloWorld.java: 1: Public Class HelloWorld Must Be Defined In A File Called

"HelloWorld.java".

That is, you don't have a case where you write this HelloWorld, or if you have not saved as HelloWorld.java

This name must be the same as the name of the public class.

For the problem of environment variables, I will say this, let me say how to compile and run in Editplus, in Tools-> Parameter -> Configuring User Tools

1. Add a tool (add application)

Menu text: Compile Java Program

Procedure: c: /jdk/bin/javac.exe

Parameters: File Name

Initial directory: file directory

2. Add Tools (Add Applications)

Menu text: Run Java Program

Program: c: /jdk/bin/java.exe

Parameters: File Name (excluding extensions)

Initial directory: file directory

The tool set name can be added casually, such as Debug Java Program

Then in the Tools drop-down menu, you will see two options of Compile Java Program and Run Java Program, you can use Ctrl 1 compile and Ctrl 2 running programs.

Let's discuss the run of the servlet:

First of all, you need to run the servlet, you need JSP / Servlet Container, I suggest that beginners use Tomcat

Tomcat (Latest Version 5.5): http://apache.justdn.org/jakarta/tomcat-5/v5.5.9/bin/jakarta-tomcat-5.5.9.exe then extracting this compressed package to:

C: / Tomcat

Then configure environment variables:

Add three system variables:

Java_Home: C: / JDKTOMCAT_HOME: C: / TomcatclassPath:% java_home% / lib;% Tomcat_Home% / LIB

Tomcat's environment variable is configured, the following verifies whether Tomcat can run:

Go to the C: / Tomcat / bin in the console, run startup, then return a window, and jump a big string, and finally the Server has run

In the browser, enter http: // localhost: 8080, the welcome interface, indicating that Tomcat is no problem.

Then write your first servlet as above, write 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 (" ""); out.println ("Hello, World!"); Out.println ("");}}

Then use Javac HelloWorld.java to compile this file, if there is an import javax.servlet. *

Then you should copy the servlet.jar in C: / Tomcat / Common / Lib to C: / JDK / JRE / LIB / EXT, compile again, no problem!

Then in the C: / Tomcat / WebApps / root inside the Tomcat directory, follow the following file structure:

Root / index.htmlroot / welcom.jsproot / web-inf / lib / myservlet.jar (if your servlet is piloted. JAR file, put it under LIB) root / web-inf / classes / helloworld. Class (placing the helloworld.class file of the above in this)

Then enter http: // localhost: 8080 / servlet / helloWorld, which is returned by the Server Hope: Error 404 - Not Found

What's going on?

Servlet must use the web.xml file below the c: / tomcat / webapps / root / web-infront of the directory, open this web.xml file with EP, join inside

HelloWorldhelloworld

HelloWorld / Servlet / HelloWorld

Such structure

HelloWorldhelloworld

Represents the specified servlet class.

HelloWorld / Servlet / HelloWorld

Indicates which URL schema that specifies the Helloservlet to map.

After modifying Web.xml, restart Server, then enter http: // localhost: 8080 / servlet / helloworld, then a Hello, World! Waiting for you.

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

New Post(0)