1. Open Eclipse, select "File" | "new" | "Project"
2. Select "Tomcat Project" and click Next, enter the project name "Testtomcat", select Next, and click "Can update context definition" to hit the finish.
3. Create a servlet class:
In the Navigator, right-click the Testtomcat project, click New | class, where the package name is "Test", the name of the class is "HelloWorld", and the name of the superclass is written "Javax.servlet.http.HttpServlet". When you click Finish, a new package test appears under TESTTOMCAT/WEB-INF/SRC with a HelloWorld class that has just been created.
4. Write the contents of the Servlet class:
Right-click the contents of the "Helloworld.java" file, select the check box in the dialog box before selecting the "doget ()" function in the pop-up menu, clicking "Source code" | "Overwrite/implementation method", and point "OK".
Enter the content:
PrintWriter Out=arg1.getwriter ();
Out.print ("HelloWorld");
Right-click PrintWriter, select Source | Organize import, then right-click "Testtomcat" Project, select "Properties" | "Java Build path" | "Sort and export" | "Select All" | "OK", Get Helloworld.java source file full 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 HelloWorld extends HttpServlet {
@Override
protected void doget (HttpServletRequest arg0, HttpServletResponse arg1) throws Servletexception, IOException {
PrintWriter Out=arg1.getwriter ();
Out.print ("HelloWorld");
}
}
5. Create the deployment file for the project (Web.xml):
Right-click the/testtomcat/web-inf folder, select Create "file", enter the filename "Web.xml" and open the file, fill in the following code in the file:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<web-app>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>test. Helloworld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
6. Check the final eclipse. The file structure in the TESTTOMCAT project is:
/---Testtomcat
| . classpath
| . cvsignore
| . Project
| . tomcatplugin
|
+---Web-inf
| | . cvsignore
| | Xml
| |
| +---Classes
| | /---Test
| | Helloworld.class
| |
| +---Lib
| /---SRC
| /---Test
| Helloworld.java
|
/---Work
Tldcache.ser
7. Run the established servlet:
Start Tomcat integrated in Eclipse, open ie, and enter in the Address bar:
Http://localhost:8080/TestTomcat/test/HelloWorld, you can see the results of the servlet's run:
HelloWorld
8. The following steps are to publish the JSP:
9. Right-click the Testtomcat Engineering folder, select "New" | "File", enter file name "index.jsp" and click "Finish".
10. Enter the following code:
<title>helloworld</title>
<body>
<center>
Now: <%=new java.util.Date ()%>
</center>
</body>
11. After you save the file, restart Tomcat in Eclipse and enter it in the IE Address bar:
http://localhost:8080/TestTomcat/index.jsp
Can be released just created the JSP file, the results of the operation are as follows:
12. Set the index.jsp file you just created as the default home page:
Add the following code to the Web.xml file:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
After saving, enter the following address in IE to access the default home page:
http://localhost:8080/TestTomcat/
The results of the operation are as follows:
13. Final file directory structure:
/---Testtomcat
| . classpath
| . cvsignore
| . Project
| . tomcatplugin
| index.jsp
|
+---Web-inf
| | . cvsignore
| | Xml
| |
| +---Classes
| | /---Test
| | Helloworld.class
| |
| +---Lib
| /---SRC
| /---Test
| Helloworld.java
|
/---Work
| Tldcache.ser
|
/---Org
/---Apache
/---JSP
Index_jsp.class
Index_jsp.java
14. The following methods are used to access the Oracle database using JSP files:
Create a JSP file in the "Testtomcat" project named "2.jsp"
Fill in the contents of the document is:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<HTML>
<BODY>
<% Connection Con=null;
Statement Sql=null;
ResultSet Rs=null;
Try{class.forname ("Oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e) {}
Try
{con=
Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:aa", "Scott", "Tiger");
Sql=con.createstatement ();
Rs=sql.executequery ("SELECT * from emp");
Out.print ("<table border>");
Out.print ("<TR>");
Out.print ("<th width=100>" + "EMPNO");
Out.print ("<th width=50>" + "ename");
Out.print ("</TR>");
while (Rs.next ())
{Out.print ("<TR>");
int N=rs.getint (1);
Out.print ("<td >" +n+ "</TD>");
String e=rs.getstring (2);
Out.print ("<td >" +e+ "</TD>");
Out.print ("</TR>");
}
Out.print ("</Table>");
Con.close ();
}
catch (SQLException E1) {out.print ("" +e1);}
%>
</BODY>
</HTML>
15. Copy Oracle's JDBC driver to the following directory: f:/excise/testtomcat/web-inf/lib, and then set up the Add Oracle Jar driver in the engineering properties. When you are sure to restart Tomcat inside Eclipse, enter the following address in IE to open "2.jsp" and access the Oracle database: http://localhost:8080/TestTomcat/2.jsp
The results of the visit are shown below:
Reference: "Proficient in eclipse Web development" People's posts and Telecommunications Press, published in October 2006