Tomcat + JSP Configuration

Source: Internet
Author: User
Tags apache tomcat

Configuration of JSP, Servlet, and JavaBean environments in Tomcat

I often see JSP beginners asking how to configure JSP, Servlet, and bean in Tomcat, So I summarized how to configure

Set JSP, Servlet, and Ben to help beginners.
I. Development Environment Configuration
Step 1: Download j2sdk and tomcat: to Sun official site (http://java.sun.com/j2se/1.5.0/download.jsp) download

J2sdk. Note that the downloaded version is the Windows offline installation SDK, and it is best to download j2se 1.5.0 documentation.

And then go to Tomcat official site (http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi)

Tomcat (download the latest version of Tomcat 5.9 );
Step 2: install and configure your j2sdk and tomcat: Install j2sdk and tomcatProgramAnd then install it according to the default settings.

.
1. After j2sdk is installed, You need to configure the environment variables and add the following to my computer> Properties> advanced> environment variables> system variables.

Environment variable (assuming that your j2sdk is installed in D: \ j2sdk ):

D: \ j2sdk
Classpath
D: \ j2sdk \ Lib \ DT. jar; D: \ j2sdk \ Lib \; D: \ j2sdk \ JRE ;.

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 a file named test. java.
Then open the Command Prompt window, CD to the directory where your test. Java is located, and then type the following command
Javac test. Java
Java Test
If this is a test program. is printed, the installation is successful. If this is not printed, you need

Check your configuration carefully.
2. After Tomcat is installed, add the following environment variables to my computer> Properties> advanced> environment variables> system variables (assuming your Tomcat

Installed in c: \ Tomcat ):
Catalina_home = c: \ Tomcat
Catalina_base = c: \ Tomcat
Then, modify the classpath in the environment variable and append it to the common \ Lib under the Tomat installation directory)

Add servlet. jar to classpath. The modified classpath is as follows:
Classpath =.; % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ tools. jar; % catalina_home %

\ Common \ Lib \ servlet. jar;
Start Tomcat and access http: // localhost: 8080 in IE. If you see the welcome page of Tomcat, install

Success.
Step 3: Create your own JSP app directory
1. Go to the webapps directory of the tomcat installation directory, and you can see the Tomcat built-in directories such as root, examples, and tomcat-docs.

;
2. create a directory named MyApp in the webapps directory.
3. create a directory WEB-INF under MyApp, note that the directory name is case sensitive;
4. create a file web under the WEB-INF. XML, the content is as follows:

Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // "
" http://java.sun.com/dtd/web-app_2_3.dtd ">

my web application

A Application for test.


5. create a 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 and enter http: // localhost: 8080/MyApp/index. if JSP sees the current time, it means it is successful.
Step 4: Create your own servlet:
1. use the editor you are most familiar with (we suggest using Java ide with syntax check) to create a servlet program named 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 ("<HTML> <body> Out. Flush ();
}
}
2. Compile
Place test. Java under c: \ test and compile it with the following command:
C: \ test> javac test. Java
Then a compiled Servlet File: Test. class will be generated under c: \ test.
3. Cut the structure test \ test. Class to % catalina_home % \ webapps \ MyApp \ WEB-INF \ Classes, that is, cut

To the classes directory. If the classes directory does not exist, create a new one. Now webapps \ MyApp \ Web-

test \ test is available under INF \ Classes. class file directory structure
4. modify webapps \ MyApp \ WEB-INF \ Web. XML, add the servlet and servlet-mapping
edited web. XML is as follows, and red is the added content:

Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // "
" 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


Servlet declare the servlet you want to call, servlet-mapping maps the declared servlet to

.

Address/test
5. Restart tomcat, start the browser, and enter http: // localhost: 8080/MyApp/test. If the output of this is

A servlet test. It indicates that the servlet has been compiled successfully.
Note: You must restart tomcat after modifying web. xml and adding a new class.
Step 4: Create your own bean:
1. Use the editor you are most familiar with (we recommend using Java ide with syntax check) to create a Java program named testbean. java.

, file content:
package test;
public class testbean {
private string name = NULL;
Public testbean (string strname_p) {
This. name = strname_p;
}< br> Public void setname (string strname_p) {
This. name = strname_p;
}< br> Public String getname () {
return this. name;
}< BR >}< br> 2. compile
testbean. run the following command to compile Java in c: \ test:
C: \ test> javac testbean. java
then a compiled bean file testbean is generated under C: \ test. class
3. set testbean. the class file is cut to % catalina_home % \ webapps \ MyApp \ WEB-INF \ Classes \ test,
4. create a new testbean. JSP file with the following content:
<% @ page import = "test. testbean "%>


<%
testbean = new testbean (" this is a test Java Bean. ");
%>
Java Bean name is: <% = testbean. getname () %>

5. restart tomcat, start the browser, and enter http: // localhost: 8080/MyApp/testbean. JSP output

Java Bean name is: this is a test Java Bean. It indicates that the bean has been compiled successfully.
In this way, the configuration of JSP, Servlet, and JavaBean in Tomcat is completed. The next thing we need to do is read more and read more.

Good peopleCodeTo enhance your development capabilities in this area.

JVM should be filled in
C: \ j2sdk \ bin

A simple configuration ::::

JSP environment configuration experience
First of all, we can use JDK + Tomcat to configure our JSP server. There are manyArticleIntroduction

The introduction of Apache is actually not needed at all. Generally, it is sufficient to study and debug tomcat.
After JDK is installed, Tomcat will automatically find the JDK installation path before installation, and click "Next" all the way, after a period of file copying

And "close" to complete comcat installation.
You 'd better download a tomcat version with a higher version, such as 4.1 or above, because it does not need to set too many system variables, right-click "my electricity

Brain ", select" attribute "->" advanced "->" environment variable "->" system variable ", create a tomcat_home, and set the value to the location of your Tomcat

Path, for example, D: \ Program Files \ apache group \ Tomcat 5.5. The configuration is complete.
Find the Tomcat option from the Start Menu. The general opening sequence is: Start> program> Apache Tomcat 5.5. Select Start.

Tomcat ", let the JSP server start running, a DOS-like window will be opened, and some related information will be displayed.
If you use a proxy to access the Internet, you must remove the proxy first, otherwise your JSP program will never be executed. If it is not a proxy

Step.
Open your browser and enter http: // localhost: 8080 in the address bar. If you see a tiger (I don't know whether it is a tiger or a cat)

Congratulations, half of your success.
To enjoy the success, enter the following code:
<HTML>
<Head>
<Title> first page </title>
</Head>
<Body>
<H3> 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 in the address bar of the browser:

Http: // localhost: 8080/first. jsp, (the case sensitivity of the First. jsp file name must be the same as that of the saved file name.) Press enter.

In the format of today is: H Fri 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 it to my own virtual directory? Continue.
To change the directory to your own virtual directory, you need to go to server. xml. This file is a configuration file, under the Tomcat \ conf directory

Open it with any text editing software. First, find the following sentence:
<Connector classname = "org. Apache. Coyote. tomcat4.coyoteconnector"
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 another port instead, but it cannot be the port occupied by the system (0--1023)

.
Next we will find the following statement:
</Context>
</Host>
We should find these two statements. If you do not understand e, you should think that these two statements are good. Then we change the statement as follows:
</Context>
<Context Path = "/myjsp" DEBUG = "0" docbase = "E:/myjsp" reloadable = "true">
</Context>
</Host>
Here, Path = "/myjsp" is the virtual directory we configured. in the address bar, enter http: // localhost: 8080/myjsp.

You can. Docbase = "E:/myjsp" is the local path of the machine. They form a ing relationship through this statement.
Put the above first. jsp file in the E:/myjsp directory and enter http: // localhost: 8080/myjsp/first. jsp. Is there any

Is it a pleasant sensation?
What I see most in the Forum is that many people do not know where to put the JavaBean file. To be honest, I do not know at first, which is even more confusing.

Yes, there are nine different statements about ten people, which makes us even more confused. In fact, this problem is not as complicated as we think.

Sub-description:
Create a Java program with the following code:
Package Hall;
Public class simplebean {
Private string message = "no message specified ";
Public String getmessage (){
Return (Message );
}
Public void setmessage (string message ){
This. Message = message;
}
}
Save as simplebean. java. After compilation, a package will be generated, which is actually equivalent to a directory, that is, simplebean. class will be saved.

Put it in the hall directory for the moment and save it for future use.
Enter the following code:
<HTML>
<Head>
<Title> reusing JavaBeans in JSP </title>
</Head>
<Body>
<Center>
<Table border = 5>
<Tr> <TH class = "title">
Reusing JavaBeans in JSP </table>
</Center>
<P>
<JSP: usebean id = "test" class = "Hall. simplebean"/>
<JSP: setproperty name = "test" property = "message" value = "Hello www"/>
<H1> message: <I>
<JSP: getproperty name = "test" property = "message"/>
</I> </Body>
Save it in the virtual directory e:/myjsp we just created and name it beantest. jsp.
Where should we put the Hall (Package) directory now? Don't worry, we first create a folder WEB-INF under E:/myjsp, then

Create a classes folder under the WEB-INF, and finally put the hall directory under the classes, of course, the bytecode file under the hall

Simplebean. Class also needs to be moved, while simplebean. Java is put in the same directory as beantest. jsp (you do not need

Put it, try it on your own ).
Okay, it's done. Restart the machine (if you have tried it many times, you must do this) and enter the following in the browser:

Http: // localhost: 8080/myjsp/beantest. jsp. What do you see? Oh, don't tell me you haven't seen anything. It must be

The problem you set is incorrect.
Well, I am only a cainiao after I finish writing the article, so please advise me if you are not prepared. Wish you a pleasant trip to JSP!

!!
Java learning-technical document center
For example, how to configure environment variables and How to Run Servlet? There are too many such problems. Now I want to write

Beginners must read this document to guide beginners!
First, download the tool:
I suggest using editplus + JDK for beginners. I think it is convenient to use editplus + JDK, such as JB, eclipse, and jcreator at the beginning.

But it makes it hard for beginners to know how to configure environment variables,
Thus it is difficult to reach the point of knowing and knowing why.
You can download it at 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)
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 installed it under the c: \ JDK directory:
Then there is the classpath problem:
Just as the operating system uses path to search for executable programs, the Java Runtime Environment will traverse classpath to find classes, even if

JVM also traverses a simple program like helloworld.
Classpath defines every path until the corresponding file is found.
I believe that if your system is not 2 K or XP, you should set the path as follows:
My computer-> properties-> advanced-> Environment Variables
Append the following path to the environment variable: C: \ JDK \ bin;.; C: \ JDK \ Lib.
You can also configure c: \ JDK \ bin;.; C: \ JDK \ Lib \ DT. jar; C: \ JDK \ Lib \ tools. jar.
★Remember: In the environment variables, remember not to be less. It indicates the current path. If there are fewer errors, you will say it!
DT. jar is a class library about the runtime environment, and tools. jar is a class library about some tools.
If c: \ JDK \ bin is not configured, "javac" is not an internal or external command, or a program or batch

. "This error.
Then we should write the program:
The first is (helloworld. Java). Open editplus and create a new Java file. Follow the instructions below to enter a unique word and distinguish it from each other.

Case sensitivity:
Public class helloworld {
Public static void main (string [] ARGs ){
System. Out. println ("Hello, world! ");
}
}
Then save the file (CTRL + S) to helloworld. java. Remember to distinguish the case sensitivity. It is helloworld. Java, not

helloworld. java or other
the following code runs: Start-> Run-> CMD
switch the directory to the current directory on the console:
javac helloworld. java
JAVA helloworld
you will see the output Hello, world on the console! (Not coming out? I have eaten my computer :))
javac is a compilation command that uses helloworld. compile Java into helloworld. class
JAVA is an interpreted command. JVM sets helloworld. class explanation execution.
at this time:
1. If exception in thread "Main" Java. Lang. noclassdeffounderror: helloworld
occurs, the. (DOT)
2 is not added to the environment variable. 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 did not write the helloworld in case-insensitive mode, or you did not save it as helloworld. java
This name must be the same as the public class name
for the question of environment variables, let's talk about how to compile and run it in editplus, choose tools> parameter settings>

Configure user tools
1. Add a tool (add an Application)
Menu text: compile Java program
Program: C: \ JDK \ bin \ javac.exe
Parameter: File Name
Initial Directory: file directory
2. Add a tool (add an Application)
Menu text: run Java program
Program: C: \ JDK \ bin \ java.exe
Parameter: File Name (excluding the extension)
Initial Directory: file directory
You can add a tool group name as needed, such as debug Java program.
Then, in the drop-down menu of tools, you will see the compile Java program and run Java program options.

You can use Ctrl + 1 to compile and CTRL + 2 to run the program.

The following describes how to run servlet:
To run servlet, JSP/servlet iner is required. We recommend that you use Tomcat for beginners.
Tomcat (Latest Version 5.5): http://apache.justdn.org/jakarta/tomcat-5/v5.5.9/bin/jakarta-tomcat-

5.5.9.exe then decompress the package:
C: \ Tomcat
Then configure the environment variable:
Add three system variables:
Java_home: C: \ JDK
Tomcat_home: C: \ Tomcat
Classpath: % java_home % \ Lib; % tomcat_home % \ Lib
The environment variables of Tomcat are configured. Check whether Tomcat can run as follows:
Go to the C: \ Tomcat \ bin directory in the console and run startup. Then, a window appears, a bunch of things are skipped, And the last table is displayed.

Indicates that the server is running
Enter http: // localhost: 8080 in the browser. If the welcome page appears, it means Tomcat is okay.
Write your first Servlet as above.
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 ("<HTML> Out. println ("this is my first servlet ");
Out. println ("</title> Out. println ("Out. println ("</body>

}
}
Then use javac helloworld. Java to compile the file. If the file cannot be imported javax. servlet .*
The servlet. jar file in c: \ Tomcat \ common \ Lib should be copied

In C: \ JDK \ JRE \ Lib \ Ext, re-compile and there is no problem!
Then, in the Tomcat directory c: \ Tomcat \ webapps \ Root, follow the following file structure:
Root \ index.html
Root \ Welcom. jsp
Root \ WEB-INF \ Lib \ myservlet. Jar (If your servlet. Class is typed into a. jar file, put it under Lib)
Root \ WEB-INF \ Classes \ helloworld. Class (put the above generated helloworld. Class file in this)
Then, enter http: // localhost: 8080/servlet/helloworld in the browser, and the server will return an error: error.

404 -- not found
What's going on?
Servlet must use the c: \ Tomcat \ webapps \ Root \ WEB-INF directory under the web. xml file for registration, open this with EP

Web. xml file,
Add
<Servlet>
<Servlet-Name> helloworld </servlet-Name>
<Servlet-class> helloworld </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> helloworld </servlet-Name>
<URL-pattern>/servlet/helloworld </url-pattern>
</Servlet-mapping>
Such a structure
<Servlet>
<Servlet-Name> helloworld </servlet-Name>
<Servlet-class> helloworld </servlet-class>
</Servlet>
Indicates the specified servlet class.
The following structure
<Servlet-mapping>
<Servlet-Name> helloworld </servlet-Name>
<URL-pattern>/servlet/helloworld </url-pattern>
</Servlet-mapping>
Specifies the URL mode to which helloservlet maps.
After modifying web. XML, restart the server and enter http: // localhost: 8080/servlet/helloworld,

In this case, the "hello", "world", is big! Waiting for you. Congratulations.
Flat :)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.