新建一个Java web项目,名字为web01
在项目中新建一个servlet,名字为SimpleHello
修改doGet()方法,为:
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //得到PrintWriter对象 PrintWriter out=resp.getWriter(); //向客户端发送字符数据 out.println("hello world"); out.close(); }
配置web.xml为
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>SimpleHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
部署
浏览器中输入:localhost:8080/web01/hello
来访问刚才的servlet
结果截图:
-------------------------------------------