China Naming Network - Ziwei Dou Shu - How to establish JSP environment?

How to establish JSP environment?

More complicated:

Step 1: Download j2sdk and tomcat:

Step 2: Install and configure your j2sdk and tomcat: Execute the installation program of j2sdk and tomcat, and then install according to the default settings.

1. After installing j2sdk, you need to configure environment variables in My Computer->; Attribute-> Advanced-> Environmental variables->; Add the following environment variables to the system variables (assuming that your J2SSDK is installed in C: \ J2SSDK1.4.2):

JAVA_HOME=E:\J2sdk 1.4.2

Classpath =. ; e:\ j2sdk 1 . 4 . 2 \ lib \ dt . jar; e:\ j2sdk 1 . 4 . 2 \ lib \ tools . jar; (.; Not less, because it represents the current path)

path=E:\J2sdk 1.4.2\bin

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

Common class test {

Public static void main (strinargs []) {

System.out.println ("This is a test program." );

}

}

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

Then open a command prompt window, cd to the directory where your Test.java is located, and then type the following command.

Javac Test.java

Java test

At this point, if you see that this is a printed test program, the installation is successful. If you don't print this sentence, you need to check your configuration carefully.

2. after installing Tomcat, on my computer->; Attribute-> Advanced-> Environmental variables->; Add the following environment variables to the 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 and append servlet.jar under common\lib in 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 and visit/dtd/web-app _ 2 _ 3.dtd ">

& ltwe B- app & gt;

& lt/display-name & gt; my Web application & lt/display-name >

& lt description & gt

Test application.

& lt/description & gt;

& lt/we B- app & gt;

5. Create a new jsp page for testing under myapp. The file name is index.jsp, and the contents of the file are as follows:

& lthtml & gt& ltbody & gt& lt center & gt

Now the time is:<% = newjava.util.date ()% >

& lt/center & gt; & lt/body & gt; & lt/html & gt;

6. restart Tomcat

7. Open a browser and type/dtd/web-app _ 2 _ 3.dtd ">

& lt/we B- app & gt;

& lt/display-name & gt; my Web application & lt/display-name >

& lt description & gt

Test application.

& lt/description & gt;

-

& ltservlet & gt

& ltservlet-name & gt; Test < lt/servlet-name >;

& lt/display-name & gt; test & lt/display-name >

& lt description & gt test Servlet & lt/description & gt;;

& ltservlet-class & gt; Testing. Test ;

& lt/servlet & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; Test < lt/servlet-name >;

& lturl mode & gt/Test & lt;; /URL-pattern & gt;

& lt/servlet-mapping & gt;

-

& lt/we B- app & gt;

The Servlet in this article declares the servlet you want to call, and servlet-mapping "maps" the declared servlet to the address/test.

5. Ok, start Tomcat, start the browser, and enter http://localhost: 8080/myapp/test. If you see that this is the output of a servlet test, then the servlet is written successfully.

-

Important: If you want to add a new servlet in the future, you only need to copy the above paragraph and modify the corresponding servlet-name, servlet-class (if the servlet uses a package structure, you need to point out the package where the class file is located) and url-pattern (must) to the corresponding attributes of the new servlet.

-

Note: After modifying web.xml and adding new classes, Tomcat must be restarted. (but it seems to be just a refresh. )

Step 4: Create your own Bean:

1. Create a new java program with your most familiar editor (java IDE with syntax checking is recommended). The file name is TestBean.java, and the contents of the file are as follows:

Packaging test;

Public class TestBean{

Private string name = null

Public testbean (stringstername _ p) (

this . name = strName _ p;

}

public void set name(String strName _ p){

this . name = strName _ p;

}

Public string getName(){

Return this.name

}

}

compile

Put TestBean.java under c:\test and compile it with the following command:

c:\ Test & gt; Javac TestBean.java

Then a compiled bean file TestBean.class will be generated under c:\Test.

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

& lt%@ pages import="test. TestBean " % & gt

& lthtml & gt& ltbody & gt& lt center & gt

& lt%

TestBean testBean=new TestBean ("This is a test java bean." );

% & gt

The name of Java bean is:<% = testbean.getname ()% >

& lt/center & gt; & lt/body & gt; & lt/html & gt;

5. Ok, restart Tomcat, start the browser, and enter http://localhost: 8080/myapp/testbean.jsp. If you see the output Java bean named: This is a test Java bean, it means that the bean was successfully written.

This completes the configuration of jsp, servlet and javabean under Tomcat. The next thing to do is to read more books, read other people's good code, and write more code yourself to enhance your development ability in this area.