1.创建DispatcherServlet
package myservlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Dispatcher
*/
@WebServlet("/Dispatcher")
public class Dispatcher extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Dispatcher() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
2.将创建好的Servlet配置到web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>myservlet.DispatcherServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.将项目部署到tomcat下即可。
总结说明:
1.创建Servlet需继承Tomcat下servlet-api.jar下的类库
2.web.xml是项目的配置文件,需要将Servlet配置到该配置文件下
3.Servlet是项目入口
(初学者记录学习中的总结,希望各位大牛进行点评指正,谢谢大家!)