TOMCAT+JSP Classic Configuration Example

Source: Internet
Author: User
Tags file copy apache tomcat

Often see the JSP beginners ask Tomcat How to configure the JSP, servlet and bean problems, so summed up how Tomcat under the configuration JSP, servlet and Ben, hope for those beginners to help.
  
   first, the development environment configuration
  
Step one: Download J2sdk and tomcat: Download J2SDK to Sun official station (HTTP://JAVA.SUN.COM/J2SE/1.5.0/DOWNLOAD.JSP), and note that the download version is Windows Offline Installation SDK, and preferably download J2SE 1.5.0 documentation, then go to the Tomcat official site (http://jakarta.apache.org/site/downloads/downloads _tomcat-5.cgi) Download tomcat (download latest 5.5.9 version of Tomcat);
  
Step Two: Install and configure your J2SDK and Tomcat: Perform J2SDK and tomcat setup, and then install by default.
  
1. After installing the J2SDK, you need to configure the environment variables, add the following environment variables to the system variable, environment variable, properties----let's assume 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 successfully installed:
  
public class test{
public static void Main (String args[]) {
System.out.println ("This was a test program.");
}
}
  
Save the above program as a file named Test.java.
  
Then open a command Prompt window, CD to your Test.java directory, and type the following command
  
Javac Test.java
Java Test
  
At this point, if you see the print out of this is a test program. The installation is successful, if you do not print out this sentence, you need to carefully check your configuration situation.
  
2. After installing Tomcat, add the following environment variable (assuming your Tomcat is installed on the C:\tomcat) in the system variable, advanced environment variable, properties, My Computer
  
Catalina_home=c:\tomcat
Catalina_base=c:\tomcat
  
Then modify the environment variables in the CLASSPATH, the Tomat installation directory under the Common\lib (can be appended according to the actual) Servlet.jar appended to the CLASSPATH, the modified classpath as follows:
  
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;%catalina_home%\common\lib\servlet.jar;
  
You can then 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 the Tomcat installation directory, you can see Root,examples, tomcat-docs such as the directory of Tomcat;
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 Create a new file, Web. XML 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>

If there is a copy error here, please refer to the example of Tomcat to modify
  
5. Create a new test JSP page under MyApp with the file name index.jsp and the file content as follows:
Now time is: <%=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.
  
Fourth step: Build Your own servlet:
  
1. Create a new servlet program with your most familiar editor (recommended Java IDE with Syntax checker), with the file name Test.java and the file contents 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 ("Out.flush ();
}
}
  
2. Compiling
Place Test.java under C:\Test and compile with the following command:
  
C:\test>javac Test.java
  
A compiled servlet file is then 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 into 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 Servlets and servlet-mapping
  
The edited web. XML is shown below, red for 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>
  
The servlet in this passage declares the servlet you want to invoke, while servlet-mapping is "mapping" the declared servlet to the address/test
  
5. OK, restart Tomcat, launch the browser, enter Http://localhost:8080/myapp/Test if you see the output of this is a servlet Test. The servlet written is successful.
  
Note: Modify the Web. XML and the new class to restart Tomcat
  
Fourth step: Build Your own bean:
  
1. Create a new Java program with your most familiar editor (recommended Java IDE with Syntax checker), with the file name Testbean.java and the file contents 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. Compiling
  
Place Testbean.java under C:\Test and compile with the following command:
  
C:\test>javac Testbean.java
  
A compiled bean file is then 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 file 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, launch the browser, enter http://localhost:8080/myapp/TestBean.jsp if you see the output Java Bean name Is:this is a test Java bean. In the description of the written B EAN was successful.
  
This completes the configuration of the JSP, Servlet, and JavaBean under Tomcat as a whole. The next thing to do is to read more books, read other people's good code, self-write code to enhance their ability to develop in this area.
  
The JVM should fill in the
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 fully configure our JSP server, no longer need anything in fact, there are many articles about Apache, in fact, there is no need, the general learning to debug Tomcat is fully competent.
  
After installing the JDK, Tomcat will automatically find the JDK installation path before installation, click "Next", after a period of file copy, finally "Close", complete the installation of ComCat.
  
You'd better download a higher version of Tomcat, like 4.1 or more, because it doesn't need to set too many system variables, right-click "My Computer", select "Properties", "Advanced", "Environment variables", "System variables", create a new tomcat_home, The value is set to the path where your Tomcat is located, such as: D:\Program Files\apache group\tomcat 5.5, configuration complete.
  
To find the Tomcat option from the Start menu, the general opening sequence is: Start with program->apache Tomcat 5.5, select "Start Tomcat" and let the JSP server start running, a DOS-like window will open and some related information will be displayed.

If there is an advanced IDE, the compilation process is not so troublesome,

TOMCAT+JSP Classic Configuration Example

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.