Creating a servlet project with eclipse

Source: Internet
Author: User

1. Open Eclipse, select File | new | project

2. After selecting "Tomcat Project", click Next, enter the project name "Testtomcat", select Next, tick "Can update context definition" and click Done.

3. Create a servlet class:

Right-click on the "Testtomcat" project in the Navigator, clicking "New" | "Class", where the package name writes "Test", the name of the class is "HelloWorld", the name of the superclass is written "Javax.servlet.http.HttpServlet", After clicking Finish, a new package test appears under TESTTOMCAT/WEB-INF/SRC, and there is a newly created HelloWorld class in the package.

4. Write the contents of the Servlet class:

Right-click the contents of the "Helloworld.java" file, in the pop-up menu, after clicking "Source code" | "Overwrite/implement method" in the dialog box, select the check box before the "doget ()" function, click "OK".

Enter the content:

PrintWriter Out=arg1.getwriter ();
Out.print ("HelloWorld");

Right-click PrintWriter, select Source code | Organize import, then right-click the Testtomcat project, select Properties | Java build path | Sort and Export | Select All | OK Get the full contents of the Helloworld.java source file 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 on the/testtomcat/web-inf folder, select Create "file", enter the file name "Web. xml" After opening it, fill in the file with the following code:

<?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 that the file structure in the Testtomcat project in the final eclipse 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:

Launch Tomcat integrated into Eclipse, open ie, enter in the Address bar:

Http://localhost:8080/TestTomcat/test/HelloWorld, you can see the result of the servlet running:

HelloWorld

8. Here are the steps to publish the JSP:

9. Right-click the Testtomcat project folder, select "New" | "File", enter the file name "index.jsp" after clicking "Finish".

10. Enter the following code:

<title>helloworld</title>
<body>
<center>
Now time is: <%=new java.util.Date ()%>
</center>
</body>

11. After saving the file, restart Tomcat inside Eclipse and enter it in the IE Address bar:

http://localhost:8080/TestTomcat/index.jsp

To publish the JSP file you just created, the results 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. Here's how to access the Oracle database using a JSP file:

Create a JSP file in the "Testtomcat" project with the file name "2.jsp"

Fill in the contents of the file:

<%@ 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 the Oracle JDBC driver into the following directory: F:\excise\TestTomcat\WEB-INF\lib, set the Add Oracle Jar driver in the properties of the project, and then restart Tomcat in Eclipse. Enter the following address in IE to open "2.jsp" and access the Oracle database: http://localhost:8080/TestTomcat/2.jsp

Access results such as:

Reference: "Master Eclipse Web Development" People's post and Telecommunications publishing house, published in October 2006

Creating a servlet project with eclipse

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.