【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
2) Give a Name to your Project and click Next
3) Check Generate web.xml Deployment Descriptor and click Finish
4) Now, the complete directory structure of your Project will be automatically created by Eclipse IDE.
2. Create my First Servlet named "MyServlet"
1) Click on First project, go to Java Resources -> src. Right click on src select New -> Servlet
2) Give Servlet class name and click Next
3) Give your Servlet class a Nma of your choice.(use default here)
4) Leave everything else to default and click Finish
5) Now your Servlet is created, write some code inside it. You can take reference from the code in the picture below.
or like below
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
2) Click on Add External JARs
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
4. Create test index.html in WebContent Folder, set form-action-name as "test"
5. Append Servlet-conf to web.xml
6. Now all you have to do is Start the server and run the application.
参考:https://www.studytonight.com/servlet/creating-servlet-in-eclipse