JS often see JSP beginners ask Tomcat How to configure the JSP, servlet and bean issues, and then summed up how Tomcat configuration jsp, servlet and Ben, want to help those beginners.
I. Development environment Configuration
Step one: Download J2sdk and tomcat: Download J2SDK to the Sun official station (HTTP://JAVA.SUN.COM/J2SE/1.5.0/DOWNLOAD.JSP), note that the download version is Windows Offline Installation SDK, it is also best to download J2SE 1.5.0 documentation and then to the 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 Two: Install and configure your J2SDK and Tomcat: Perform J2SDK and tomcat setup, and then install it by default settings.
1. After installing J2SDK, you need to configure the environment variables to add the following environment variables in My Computer-> properties-> Advanced-> environment variable-> system variable (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 less because it represents the current path)
Path=%java_home%\bin
You can then write a simple Java program to test whether the J2SDK has been installed successfully:
public class test{
public static void Main (String args[]) {
System.out.println ("This are a test program.");
}
}
Save the above program as a file with a file name of Test.java.
Then open the Command Prompt window, the CD to your Test.java directory, and then type the following command
Javac Test.java
Java Test
At this point, if you see the print this are a test program, the installation is successful, if you do not print out this sentence, you need to carefully check your configuration.
2. After installing Tomcat, add the following environment variables in My Computer-> properties-> Advanced-> environment variable-> system variable (assuming your Tomcat is installed in C:\tomcat):
Catalina_home=c:\tomcat
Catalina_base=c:\tomcat
Then modify the classpath in the environment variable, the Tomat installation directory under the Common\lib (can be added according to the actual) Servlet.jar appended to the CLASSPATH, after the modified classpath are as follows:
Then you can start Tomcat, access http://localhost:8080 in IE, and if you see the Tomcat Welcome page, the installation is successful.
Step three: Build your own JSP app directory
1. To the WebApps directory of Tomcat's installation directory, you can see Root,examples, Tomcat-docs, such as Tomcat's own directory;
2. Create a new directory under the WebApps directory, named MyApp;
3.myapp Create a new directory Web-inf, note that the directory name is case-sensitive;
4.web-inf a new file Web.xml, which reads as follows:
<?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. Under MyApp, create a new JSP page for the test, the file name is index.jsp, and the contents are as follows:
Now: <%=new java.util.Date ()%>
</center></body>
6. Restart Tomcat
7. Open the browser, enter http://localhost:8080/myapp/index.jsp to see the current time, the description is successful.
Step Fourth: Build Your own servlet:
1. Create a new servlet program with your most familiar editor (the recommended syntax-checked Java IDE), with the file named Test.java, which reads as follows:
2. Compile
Place the Test.java under C:\Test and compile with the following command:
C:\test>javac Test.java
Then a compiled servlet file is generated under C:\Test: Test.class
3. Cut the structure Test\test.class 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, Just create a new one. Now, there's a Test\test.class file directory structure under Webapps\myapp\web-inf\classes.
4. Modify Webapps\myapp\web-inf\web.xml, add servlet and servlet-mapping
The edited Web.xml is as follows, red for 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>
The servlet section of this passage declares the servlet you want to invoke, while servlet-mapping "maps" the declared servlet to the 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 servlet that wrote the note was successful.
Note: The Web.xml is modified and the new class is added to restart Tomcat
Step Fourth: Build Your own bean:
1. Create a new Java program with your most familiar editor (the recommended syntax-checked Java IDE), with the file named Testbean.java, which reads 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 the Testbean.java under C:\Test and compile with the following command:
C:\test>javac Testbean.java
Then a compiled bean file is generated under C:\Test: Testbean.class
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:
<%@ page import= "test. Testbean "%>
<%
Testbean testbean=new Testbean ("This is a test Java bean.");
%>
Java Bean name is: <%=testbean.getname ()%>
</center></body>
5. OK, restart Tomcat, start the browser, and enter http://localhost:8080/myapp/TestBean.jsp if you see the output Java Bean name Is:this is a test Java bean. Written on the note b The EAN succeeded.
This completes the configuration of the JSP, Servlet, and JavaBean under Tomcat. The next thing to do is to read more books, reading other people's good code, and more hands-on writing code to enhance their ability to develop in this area.
The JVM should fill in
C:\j2sdk\bin
Give you a simple configuration::::
JSP Environment Configuration Experience
The first thing to say is that the use of Jdk+tomcat can completely configure our JSP server, no longer need in fact any dongdong, there are many articles introduced Apache, in fact, do not need, the general learning to debug Tomcat fully competent.
After installing the JDK, Tomcat automatically finds the JDK installation path before installation, clicking "Next", after a period of file replication, and finally "close" to 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 many system variables, right click on "My Computer", select "Properties"-> "Advanced"-> "Environment variables"-> "System Variables", create a new tomcat_home, The value is set to the path of your Tomcat, for example: D:\Program files\apache group\tomcat 5.5, configuration complete.
To find the Tomcat option from the Start menu, the general open order is: Start-> program->apache Tomcat 5.5, select "Start Tomcat", let the JSP server start running, this time will open a DOS-like window, will display some relevant information.
If you use a proxy internet, you must first remove the agent, or your JSP program will never be implemented. If it is not an agent, this step is skipped.
Open the browser and enter in the Address bar: http://localhost:8080, if you see a tiger (I do not know is a tiger or a cat) screen, congratulations, you have succeeded half.
First, enjoy the joy of success, please enter the following code:
<title>first page</title>
<body>
<%= new Java.util.Date ()%>
</H3>
</body>
Save the program as: first.jsp, put it in the root directory of Tomcat, and then enter it in the browser's address bar: http://localhost:8080/First.jsp (first.jsp consistent with the case of the file name we saved) carriage return , if not unexpectedly, you should see the results of the form like today is:h Fri APR 08:32:38 CST 2003.
Note: Root is the default virtual directory for Tomcat, what if you want to change it to your own virtual directory? Please continue to look down.
To change to its own virtual directory, please come out Server.xml, the file is a configuration file, in the tomcat\conf directory, using any text editing software can open it, we first find the following sentence:
Here the port= "8080" is the port, we can use other ports to replace, but not the system is occupied by the port (0--1023), here a simple mention.
Next we look down and find the following statement:
</Context>
</Host>
We should find these two statements, if you do not understand E, you will decide that the two statements are good. Then we'll change the statement as follows:
Here the path= "/myjsp" is the virtual directory we have configured, and then enter http://localhost:8080/myjsp in the Address bar. and docbase= "e:/myjsp" is the machine local path, they form a mapping relationship through this statement, other copy.
Is there a kind of beaming feeling to put the first.jsp file in the e:/myjsp directory and enter the http://localhost:8080/myjsp/First.jsp?
What I see most in the forum is that many people do not know where to put the JavaBean file, honestly, I do not know, even more puzzling is that 10 people have nine different kinds of statements, which makes us dazed. In fact, the problem is not as complicated as we think, and we illustrate in one example:
First set up a Java program, 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;
}
}
Save As Simplebean.java, compiled will generate a package, in fact, is equivalent to a directory, that is, Simplebean.class will be stored in Hall directory, for the time being to save, future standby.
Save it under the virtual directory e:/myjsp we just created, and name it: beantest.jsp.
Where should we put the Hall (package) catalogue now? Don't worry, we'll set up a folder Web-inf under the e:/myjsp, then create a classes folder under Web-inf, and finally put the Hall directory under classes, of course, Hall byte code file Simplebean.class also want to move over, and Simplebean.java and beantest.jsp put in the same directory (you can not need to put, try it yourself).
Reboot the machine (if you have tried many times, this step must do), in the browser input: http://localhost:8080/myjsp/BeanTest.jsp, what do you see? Oh, don't tell me you didn't see anything, that must be the problem you set up.
Java Learning-Technology article Center
Beginners ask such as: "How to configure the environment variable" How to run the servlet ah? Such a problem too much, now I write a beginner must read, in order to guide the beginners!
The first is the download tool:
I suggest beginners use EDITPLUS+JDK, I think if use such as Jb,eclipse,jcreator, although the beginning of the time is more convenient, but it does not know how to configure the Novice door environment variables,
So that it is difficult to reach the point of knowing it.
Can be downloaded from the following address:
EditPlus (latest version is v2.11): http://count.skycn.com/softdown.php?id=3641&url=http://sc-http.skycn.net/down/epp211a _cn.exe (to follow the registration code on their own to find it, a lot of online)
JDK (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 the JDK is installed, and I'm putting it under the C:\JDK directory:
And then there's the question of classpath:
Just as the operating system uses path to search for executable programs, the Java runtime also traverses classpath to find classes, and even a simple program like HelloWorld, the JVM traverses
Classpath each path defined until the appropriate file is found.
Believe that you use the system is not 2k is XP, and then you should set the path as follows:
My Computer-> Properties-> Advanced-> Environment variables
Then append to the path behind the environment variable: C:\JDK\bin;. C:\JDK\lib
Can also be configured like this: C:\JDK\bin; C:\JDK\lib\dt.jar; C:\JDK\lib\tools.jar
★ Remember: In the environment variable. Remember that it is not less, it represents the current path, if the missing error will be said!
Dt.jar is a class library about the runtime environment, Tools.jar is a class library for some tools
If not configured: C:\JDK\bin, "javac′ is not an internal or external command, it is not a running program or a batch file." "Such a mistake.
Then it's time to write the program:
First is (Helloworld.java), open the EditPlus, create a new Java file, please follow the following input, to a word without leakage, and distinguish case:
public class helloworld{
public static void Main (string[] args) {
System.out.println ("hello,world!");
}
}
Then save the file (Ctrl + S) to Helloworld.java, and remember that the case must be distinguished by Helloworld.java not helloworld.java or anything else.
Now it's time to run and start-> run->cmd
To switch directories to the current directory in the console:
Javac Helloworld.java
Java HelloWorld
You'll see the output hello,world! on the console. (not coming out?) I ate the computer:)
Javac is a compiler command that compiles Helloworld.java into Helloworld.class
Java is the interpretation of commands, and the JVM interprets Helloworld.class.
At this time:
1. If exception in thread "main" Java.lang.NoClassDefFoundError:HelloWorld appears
That's what you don't add to the environment variables. (dot)
2. If exception in thread "main" Java.lang.NoSuchMethodError:main appears
or helloworld.java:1: public class HelloWorld must is defined in a file called
"Helloworld.java".
It's that you don't know the case. Write to this HelloWorld, or save time without saving as Helloworld.java
The name must be the same as the name of public class.
To the problem of environment variable here, I first said how to compile and run in EditPlus, tools-> parameter set-> Configure user tool
1. Add tool (Add application)
Menu Text: Compile Java Program
Program: C:\JDK\bin\javac.exe
Parameters: File name
Initial directory: File directory
2. Add tool (Add application)
Menu Text: Run Java Program
Program: C:\JDK\bin\java.exe
Parameters: File name (without extension)
Initial directory: File directory
Tool group names can be added casually, such as the debug Java program
Then, in the Tools Drop-down menu, you'll see the compile Java program and run Java programs, and then you can use CTRL + 1 to compile and ctrl +2 to run the application.
The following is a discussion of the operation of the servlet:
To run the Servlet first, you need Jsp/servlet container, and I recommend 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 and then unzip the compressed package to:
The Tomcat environment variable is configured to verify that Tomcat is able to run the following:
Go to the C:\Tomcat\bin directory in the console, run startup, and then return to a window, jump a bunch of things, and finally indicate that the server is running
Enter http://localhost:8080 in the browser, and the Welcome interface appears, which means that Tomcat is fine.
And then, like the top, 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 ("This is the My A-Servlet");
Out.println ("</title>Out.println ("Out.println ("</body>
}
}
Then use Javac Helloworld.java to compile this file, if there is no import javax.servlet.*
Then you should put the C:\Tomcat\common\lib inside the Servlet.jar (according to the actual) file copy to C:\JDK\jre\lib\ext, compile again, there is no problem!
Then, in the C:\Tomcat\webapps\ROOT inside the Tomcat directory, press the following file structure:
Root\index.html
root\welcom.jsp
Root\web-inf\lib\myservlet.jar (If your servlet's. Class hits the. jar file, put it under Lib)
Root\web-inf\classes\helloworld.class (Put the Helloworld.class file that was generated above in this)
Then in the browser input http://localhost:8080/servlet/HelloWorld, so the server is expected to complain: error 404--not Found
What's going on?
The servlet must register using the Web.xml file below the C:\Tomcat\webapps\ROOT\WEB-INF directory, open the Web.xml file with the EP, and add
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.