import javax.servlet.*; import java.io.IOException; import java.io.PipedWriter; import java.io.PrintWriter; /** * Created with IntelliJ IDEA. * User: wbb * Date: 14-6-17 * Time: 上午11:56 * To change this template use File | Settings | File Templates. */ public class HelloWorldServlet implements Servlet { private ServletConfig config; @Override public void init(ServletConfig config) throws ServletException { this.config = config; } @Override public ServletConfig getServletConfig() { return config; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { PrintWriter out = servletResponse.getWriter(); out.println("Hello World"); out.close(); } @Override public String getServletInfo() { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public void destroy() { } }
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app 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 <span style="white-space:pre"> </span> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!--用于声明Servlet--> <servlet> <!--指定Servlet的名字,在同一个Web应用程序中,每一个Servlet的名字必须是唯一的,该元素的内容不能为空--> <servlet-name>HelloWorldServlet</servlet-name> <!--指定Servlet的完整限定名(即该类所在的路径)--> <servlet-class>wbb.servlet.HelloWorld.HelloWorldServlet</servlet-class> <!--配置初始化参数--> <init-param> <param-name>greeting</param-name> <param-value>Welcome you</param-value> </init-param> </servlet> <!--用于在Servlet跟URL之间定义一个映射--> <servlet-mapping> <!--此处的Servlet名字必须跟<servlet>中的子元素<servlet-name>给的名字相同--> <servlet-name>HelloWorldServlet</servlet-name> <!--用于指定该Servlet的URL路径,该路径是相对Web应用程序上下文根的路径--> <url-pattern>/helloworld</url-pattern> </servlet-mapping>
在浏览器中输入:http://localhost:8080/helloworld即可,此处可以看出helloworld即为web.xml中定义的<url-pattern>
Servlet在web.xml中的配置,布布扣,bubuko.com
时间: 2024-11-03 22:47:58