Configuring JSP Serverlet Tomcat Configuration and testing ***********__jsp under Tomcat

Source: Internet
Author: User


First step: Download J2sdk and Tomcat: to the Sun official site (http://java.sun.com/j2se/1.4.2/ download.html) Download J2SDK, note that the download version of the Windowsofflineinstallation SDK, while it is best to download j2se1.4.2documentation, and then to the Tomcat official site (http:// www.apache.org/dist/jakarta/tomcat-4/) Download tomcat (Download the latest 4.1.x 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 variables-> System variables
(assuming your j2sdk is installed in c:/j2sdk1.4.2):
(1) java_home=c:/j2sdk1.4.2
(2) classpath=.; %java_home%/lib/dt.jar;%java_home%/lib/tools.jar; (.;;; Must not be less because it represents the current path)
(3) 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 (stringargs[]) {
System.out.println ("Thisisatestprogram."); Note: Do not use the package, System s to capitalize
}
}
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
Javactest.java (compiled)
Javatest (execution)

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.
------------------------------------------------------------------
<-----How to create your own directory under Tomcat webapp----->
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):
(1) Catalina_home=c:/tomcat;
(2) Catalina_base=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:
(3) classpath=.; %java_home%/lib/dt.jar;%java_home%/lib/tools.jar;%catalina_home%/common/lib/servlet.jar;
You can then start Tomcat and access http://localhost:8080 in IE (or maybe 8086) if you see the Tomcat Welcome page, the installation is successful.

The following starts the directory creation

1 Create a MyApp directory underneath the WebApp to create a index.jsp in the directory
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "Java.util.Date"%>
<body>
Now time Is:<%=new Date ()%>
</body>

Under WebApp, create a folder named Web-inf, and create a file named Web.xml in the Web-inf folder:
<?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>
</web-app>
2 Edit the-server.xml file under the Conf folder under the Tomcat directory
Join:
<context path= "/myapp" docbase= "MyApp" debug= "0" reloadable= "true"/>
3 Http:localhost:8086/myapp will be able to see the current time
-----------------------------------------------------------------
<-------set up your own servlet----->
(1)
D: Set up a folder test, create a Test.java with Notepad
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 ("This is a servlet test");
Out.flush ();
}
}
(2)
CMD under D:/test>javac Test.java after the D:/test directory to generate a Test.class file
Copy the test directory to the classes under MyApp (if you don't have a new one)
(3)
Rewrite the Web-inf web.xml in the MyApp directory

<?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>
        <servlet>
         <servlet-name>a test</servlet-name>
         <servlet-class>test. Test</servlet-class>
      </servlet>
        <SERVLET-MAPPING>
        <servlet-name>a test</ Servlet-name>
        <url-pattern>/test</url-pattern>
       </servlet-mapping>
 </web-app>
Note: Sevelet-name as long as the agreement can be achieved
      Servlet-class must be clear: package name. java filename
(4) Start Startup.exe   Http://localhost:8086/myapp/Test Note that the/test in the MyApp below corresponds to the/test

in Web.xml.

<-----set up JavaBean---->
(1) Establish a Testbean.java under the D:/test
Package test;
public class Testbean
{
Private String Name=null;
Public Testbean ()
{ }
public void SetName (String strname_p)
{
this.name=strname_p;
}
Public String GetName ()
{
return this.name;
}
}
(2) cmd compiler D:/test>javac Testbean.java
The Testbean.class file will be visible below d:/test
Copy him under the webapps--myapp--web-inf--classes.
(3)
Establish testbean.jsp under the Webapps-myapp

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "test. Testbean "%>

<jsp:usebean id= "A" scope= "request" class= "test." Testbean "/>
<body>
<jsp:setproperty name= "A" property= "name" value= "Tang Hui day Morning"/>
This is: <jsp:getproperty name= "A" property= "name"/>
</body>
(4) Start Startup.exe
http://localhost:8086/myapp/TestBean.jsp
The This is: Tang Hui good morning.
-----------------------------------------------------------------------------------------------------------

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.