创建、配置Servlet
1、创建Servlet
2、选择继承的类及需要覆盖的方法
3、Servlet结构
package com.sysker.servlet; 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 SecondServlet */ @WebServlet("/SecondServlet") public class SecondServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SecondServlet() { 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 } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
4、部署及配置
(1)注解的形式:@WebServlet("/SecondServlet") 或者@WebServlet(name="secondServlet",urlpatterns={"/second"})
(2)在web.xml中配置:
<servlet> <servlet-name>secondServlet</servlet-name> <servlet-class>com.sysker.servlet.SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>secondServlet</servlet-name> <url-pattern>/SecondServlet</url-pattern> </servlet-mapping>
5、调用
4(1)中对应的访问方式地址是SecondServlet,或者./SecondServlet,@WebServlet(name="secondServlet",urlpatterns={"/second"})对应的访问地址为second
4(2)中对应的访问访问地址为seconServlet