Tomcat JSP Classic Configuration instance

Source: Internet
Author: User
Tags current time modify variables version variable tomcat
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->-> 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->-> 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:
  
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 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:

<body>
<center>
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:
  
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. 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 "%>
<body>
<center>
<%
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.

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.