Eclipse创建Servlet并在html调用

1.  创建项目:Dynamic Web Project,名为 "First"

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

扫描二维码关注公众号,回复: 9134717 查看本文章

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. 创建Servlet,名为“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. 添加jar文件到项目中(servlet-apt.jar)

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.在WebContent目录下创建index.html

set form-action-name as "test"。

action属性要和web.xml的url-pattern一致。本处设为"test",则web.xml必须设为"/test"

5. 用web.xml配置Servlet

url-pattern不是非得和servlet-name一样。只要和上一步index.html的action匹配即可。

6. 启动服务器,开始测试

发布了126 篇原创文章 · 获赞 11 · 访问量 1万+

1.  创建项目:Dynamic Web Project,名为 "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. 创建Servlet,名为“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. 添加jar文件到项目中(servlet-apt.jar)

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.在WebContent目录下创建index.html

set form-action-name as "test"。

action属性要和web.xml的url-pattern一致。本处设为"test",则web.xml必须设为"/test"

5. 用web.xml配置Servlet

url-pattern不是非得和servlet-name一样。只要和上一步index.html的action匹配即可。

6. 启动服务器,开始测试

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/103756627