创建配置Servlet

Posted 云中志

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建配置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

以上是关于创建配置Servlet的主要内容,如果未能解决你的问题,请参考以下文章

servlet

Servlet上

servlet和filter的区别

引入servlet概念

jsp初识servlet转发转发与重定向的比较

Java基础——JSP