package test;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloServlet implements Servlet{
/** service方法是专门用于处理请求和响应的 */
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
System.out.println("Hello Servlet被访问了"); //控制台打印出这句话说明访问成功
}
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
实现service方法,处理请求并响应数据。
到web.xml中去配置servlet程序的访问地址。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- servlet标签给服务器Tomcat配置Servlet程序 -->
<servlet>
<!-- servlet-name标签给Servlet程序起一个别名(一般是类名) -->
<servlet-name>HelloServlet</servlet-name>
<!-- servlet-class是Servlet程序的全类名 -->
<servlet-class>test.HelloServlet</servlet-class>
</servlet>
<!-- servlet-mappong给servlet程序配置 访问地址 -->
<servlet-mapping>
<!-- 标签的作用是告诉服务器,我当前配置的地址给哪个Servlet程序使用 -->
<servlet-name>HelloServlet</servlet-name>
<!-- url-pattern标签配置访问地址 / 斜杠在服务器解析的时候表示地址为http://ip:port/工程路径 /hello 表示地址为:http://ip:port/工程路径/hello -->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_47511190/article/details/120203672
内容来源于网络,如有侵权,请联系作者删除!