在开始这一节之前呢,我们还需要把Tomcat配置到Eclipse中,配置的方式很简单,打开Eclipse,Window,Preferences,进入到这个页面

编写第一个Servlet程序

将Tomcat的安装目录配置到Eclipse中。
然后,可以看到左侧多出来一个Servers文件夹:

编写第一个Servlet程序

 

新建一个LoginServlet类,继承HttpServlet

public class LoginServlet extends HttpServlet{

	@Override
	public void init() throws ServletException {
		System.out.println("进入 " +this.getClass().getName());
	}
	
}

  

这里需要引入servlet-api.jar包,可以在tomcat的lib中找到

编写第一个Servlet程序

 

 

在web.xml里面添加Servlet配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	>
	<display-name>spring0507</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>LoginServlet</servlet-name>
		<servlet-class>com.spring.web.servlet.LoginServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>LoginServlet</servlet-name>
		<url-pattern>/login.do</url-pattern>
	</servlet-mapping>

</web-app>

  

按住ctrl,鼠标划上去,如果com.wzry.web.LoginServlet下面出来一个短横线,说明引入正确。
如果是Eclipse,还要修改编译路径:

编写第一个Servlet程序

这个路径是错误的,因为web项目中,Java类编译后的class文件都应该在这个地方:

编写第一个Servlet程序

部署项目,启动Tomcat,然后打开浏览器,输入:

http://localhost:8080/spring0507/login.do

编写第一个Servlet程序

 

 这就说明,我们的流程已经走通了。

 

转自:https://www.cnblogs.com/skyblue-li/p/8251215.html

相关文章:

  • 2022-12-23
  • 2021-12-16
  • 2019-01-23
  • 2021-12-01
  • 2021-11-03
  • 2021-05-03
  • 2021-12-28
猜你喜欢
  • 2022-01-04
  • 2021-10-10
  • 2021-10-17
  • 2021-09-01
  • 2021-11-03
相关资源
相似解决方案