Servlet项目的建立
- 安装Tomcat
安装Tomcat - 新建JAVA Web项目
新建包
新建Servlet类,点next
设置在url中访问的代称,点finsh
重写service方法
package com.wf.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String str = "<h2>第一个测试程序</h2>";
resp.getWriter().write(str);
}
}
- 配置xml文件
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Servlet_Test</display-name>
<servlet>
<servlet-name>my</servlet-name>
<servlet-class>com.wf.test.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>my</servlet-name>
<url-pattern>/my</url-pattern>
</servlet-mapping>
</web-app>
- 运行第一个程序
启动Tomcat服务器
在浏览器地址栏中输入url
http://localhost:8080/Servlet_Test/my