Often see JSP novice ask Tomcat How to configure JSP, servlet problem, so summed up how to configure the JSP under Tomcat, servlet, hope for those beginners to help.
The first step: Download J2sdk and Tomcat: to the Sun official site Download the latest JDK for 1.5,tomcat 5.5, recommended jdk1.4 above, tomcat4.0 above.
Step Two: Install and configure your J2SDK and Tomcat: Perform J2SDK and tomcat setup, and then set up the path to install.
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.4.2):
java_home=c:\j2sdk1.4.2 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 is the 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:\tomcatcatalina_base:c:\tomcattomcat_home:c:\tomcat
|
Then modify the classpath in the environment variable, add the Servlet.jar under the Tomat installation directory to classpath, and modify Classpath as follows:
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;%catalina_home%\common\lib\servlet.jar;
|
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 the directory of Tomcat, such as Root,examples,tomcat-docs;
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:
6. Restart Tomcat
7. Open the browser, enter the http://localhost:8080/myapp/index.jsp, see the current time the description of the success.
Step Fourth: Build Your own servlet:
Write to 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 (" |
Then use Javac Helloworld.java to compile this file, if there is no import JAVAX.SERVL
et.*
Then you should copy the Servlet.jar file from the C:\Tomcat\common\lib to the C:\JDK\jre\lib\e.
XT, compile again, there's no problem.
Then, in the C:\Tomcat\webapps\ROOT inside the Tomcat directory, press the following file structure:
root\index.htmlroot\welcom.jsp
|
Root\web-inf\lib\myservlet.jar (If your servlet's. Class is typed as a. jar file, it is placed in the
LIB below) Root\web-inf\classes\helloworld.class (put the Helloworld.class file generated above in this) and then type in the browser 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 C:\Tomcat\webapps\ROOT\WEB-INF This directory, open the Web.xml file with an EP, and use the following procedure:
<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 >
|
Replace with:
<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 >
|
Why, then? Because such a structure
<servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>helloworld </servlet-class></servlet>
|
Represents the specified servlet class to include. And the following structure:
<servlet-mapping><servlet-name>HelloWorld</servlet-name> <url-pattern>/servlet/ Helloworld</url-pattern> </servlet-mapping>
|
Indicates which URL pattern the specified helloservlet should be mapped to. After modifying the web.xml, restart the server, and then enter the Http://localhost:8080/servlet/HelloWorld, so a hello,world! waiting for you.