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

Source: Internet
Author: User
Tags apache tomcat

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

I. Development Environment Configuration

Step 1: Download j2sdk and tomcat: To the sun official site (http://java.sun.com/j2se/1.5.0/download.jsp) download j2sdk, pay attention to download the version of Windows offline installation SDK, at the same time it is best to download j2se 1.5.0 documentation, go to the Tomcat official site (http: // jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi) to download Tomcat (the latest version of Tomcat 5.5.9 );

Step 2: install and configure your j2sdk and tomcat: Execute the j2sdk and tomcat installation programs and install them according to the default settings.

1. after installing j2sdk, You need to configure the environment variables, choose my computer> Properties> advanced> environment variables> system variables to add the following environment variables (assuming your j2sdk is installed in C:/j2sdk1.5.0 ):

Java_home = C:/j2sdk1.5.0
Classpath =.; % java_home %/lib/dt. jar; % java_home %/lib/tools. jar; (.; must not be small, 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 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 to carefully check your configuration.

2. after Tomcat is installed, add the following environment variables to my computer> Properties> advanced> environment variables> system variables (assuming your Tomcat is installed in C:/tomcat ):

Catalina_home = C:/tomcat
Catalina_base = C:/tomcat

Modify the classpath in the environment variable, and append servlet. Jar under the Common/lib directory under the Tomat installation directory (you can append it according to the actual situation) 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;

Start Tomcat and access http: // localhost: 8080 in IE. If you see the welcome page of Tomcat, the installation is successful.

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 under the webapps directory;
3. Create a directory WEB-INF under MyApp, note that the directory name is case sensitive;
4. Create a file web. xml under the WEB-INF with the following content:

<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Display-Name> my web application </display-Name>
<Description>
A Application for test.
</Description>
</Web-app>

5. Create a test JSP page under MyApp. The file name is index. jsp. The file content is as follows:
<HTML> <body> <center>
Now time is: <% = new java. util. Date () %>
</Center> </body>

6. Restart Tomcat

7. Open the browser and enter http: // localhost: 8080/MyApp/index. jsp to view the current time.

Step 4: Create your own servlet:

1. Use the editor you are most familiar with (it is recommended to use 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
Put 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 in C:/test.

3. structure Test/test. class cut to % catalina_home %/webapps/MyApp/WEB-INF/classes, that is, cut the test directory to 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 test/test. Class

4. Modify webapps/MyApp/WEB-INF/Web. XML, add Servlet and servlet-Mapping

The edited web. XML is shown as follows, and red indicates the added content:

<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Display-Name> my web application </display-Name>
<Description>
A Application for test.
</Description>
<Servlet>
<Servlet-Name> test </servlet-Name>
<Display-Name> test </display-Name>
<Description> A test servlet </description>
<Servlet-class> Test. Test </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> test </servlet-Name>
<URL-pattern>/test </url-pattern>
</Servlet-mapping>
</Web-app>

In this section, Servlet declares the servlet you want to call, while servlet-mapping maps the declared servlet to the address/test.

5. Restart tomcat, start the browser, and enter http: // localhost: 8080/MyApp/test. If this is a servlet test, the servlet is successfully written.

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

Place testbean. Java in C:/test and compile it with the following command:

C:/test> javac testbean. Java

Then a compiled bean file testbean. class will be generated in 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 content:

<% @ Page import = "test. testbean" %>
<HTML> <body> <center>
<%
Testbean = new testbean ("this is a test Java Bean .");
%>
Java Bean name is: <% = testbean. getname () %>
</Center> </body>

5. restart tomcat, start the browser, and enter http: // localhost: 8080/MyApp/testbean. JSP if the output Java Bean name is: this is a test Java Bean. it indicates that the bean is successfully written.

In this way, the configuration of JSP, Servlet, and JavaBean in Tomcat is completed. What needs to be done next is to read more books and read more good code from others. You need to write more code to 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. We don't need anything. Many articles have introduced Apache, but we don't need it at all, general learning and debugging of Tomcat is fully qualified.

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 finally "close", 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 computer ", select "property"-> "advanced"-> "environment variable"-> "system variable" to create a tomcat_home, and set the value to the path of your tomcat, 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 to start running the JSP server, in this case, a DOS-like window is opened, and some related information is 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, skip this step.

Open your browser and enter http: // localhost: 8080 in the address bar. If you see a picture of 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. put JSP in the root directory of Tomcat, and enter: http: // localhost: 8080/first in the address bar of the browser. JSP, (first. JSP is the same as the file name we saved.) Press enter. If not, you can see the result 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 to your own virtual directory, you need to go to the server. XML is coming. This file is a configuration file. In the tomcat/conf directory, you can open it using 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. Enter http: // localhost: 8080/myjsp in the address bar. 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 input http: // localhost: 8080/myjsp/first. jsp. Is there 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 it at first. What's even more puzzling is that there are nine different statements about ten people, this makes us even more confused. In fact, this problem is not as complicated as we think. We will illustrate it with an example:

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. In fact, it is equivalent to a directory, that is, simplebean. class will be stored in the hall directory. It will be saved temporarily and will be used up in the future.

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 classes, of course, simplebean. the class also needs to be moved, while simplebean. java and beantest. put JSP in the same directory (you can try it yourself if you don't need it ).

Restart the machine (this step is required if you try it multiple times). Enter http: // localhost: 8080/myjsp/beantest in the browser. JSP, what do you see? Oh, don't tell me you haven't seen anything. It must have been a problem with your settings.

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 a beginner entry, so that it can be helpful for beginners!

First, download the tool:

I suggest using editplus + JDK for beginners. I think it is easier to use editplus + JDK for beginners, such as JB, eclipse, and jcreator, but it does not know how to configure environment variables for beginners,

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/movie/-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 a simple program such as helloworld will traverse the JVM.

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 file that can be run. "This error.

Then we should write the program:

The first is (helloworld. Java). Open editplus and create a new Java file. Enter the following information to keep it case-insensitive:

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. helloworld. Java is not helloworld. Java or other

Run the following command: Start-> Run-> cmd

Switch the directory to the current directory in 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 compiles helloworld. Java into helloworld. Class.

Java is the command to explain. JVM will explain and execute helloworld. Class.

At this time:

1. If exception occurs in thread "Main" Java. Lang. noclassdeffounderror: helloworld

That is, you didn't add that. (DOT) to the environment variable)

2. If exception in thread "Main" Java. Lang. nosuchmethoderror: Main appears

Or helloworld. Java: 1: Public class helloworld must be defined in a file called

"Helloworld. Java ".

That is, you did not write this helloworld case-insensitive, or you did not save it as helloworld. Java

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

Let's talk about the problem of environment variables. Next, let's talk about how to compile and run the environment variables in editplus, and 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. Then 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 (the latest version 5.5): Latest:

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:

In the console, go to the C:/tomcat/bin directory and run startup. Then, a window appears, which means that the server has been 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 .*

Then the servlet in C:/tomcat/common/lib should be put. copy the JAR file to C:/JDK/JRE/lib/EXT and compile it again!

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. The Error 404 -- not found is returned by the server.

What's going on?

Servlet must use the C:/tomcat/webapps/root/WEB-INF directory under the Web. xml file registration, open this web. xml file with EP, 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 then enter http: // localhost: 8080/servlet/helloworld. Then, a large Hello, world! Waiting for you.

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.