【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

说明:学习过程中参考了很多资料,但此文章主要在https://www.studytonight.com/servlet/creating-servlet-in-eclipse的基础上调整完成,如果侵权请联系我!


Eclipse IDE is the most popular Java IDE used in the Industry. It is developed by an open source community and can be downloaded for free from Eclipse.org


To create a Servlet application in Eclipse IDE you will need to follow the following steps:

1.  Create a Dynamic Web Project, named "First"

1) Goto File -> New -> Dynamic Web Project

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

2) Give a Name to your Project and click Next

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

3) Check Generate web.xml Deployment Descriptor and click Finish

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

4) Now, the complete directory structure of your Project will be automatically created by Eclipse IDE.

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

2. Create my First Servlet named "MyServlet"

1) Click on First project, go to Java Resources -> src. Right click on src select New -> Servlet

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

2) Give Servlet class name and click Next

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

3) Give your Servlet class a Nma of your choice.(use default here) 

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

4) Leave everything else to default and click Finish

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

5) Now your Servlet is created, write some code inside it. You can take reference from the code in the picture below.

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

or like below

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

3. Add servlet-api.jar JAR file to your project.

1)  Click on Libraries, right click on Web App Librariesselect Build Path -> Configure Build Path

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

2) Click on Add External JARs

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

3) Select servlet-api.jar from Apache Tomcat Directory, click "Open" button, this JAR is now added to your project's build path, click OK to finish

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

4. Create test index.html in WebContent Folder, set form-action-name as "test"

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

5. Append Servlet-conf to web.xml

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

6. Now all you have to do is Start the server and run the application.

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例

【Servlet学习笔记-1】使用Eclipse创建第一个Servlet并在html页面调用示例


参考:https://www.studytonight.com/servlet/creating-servlet-in-eclipse