首页 > 代码库 > Servlet的入门案例

Servlet的入门案例

 


编写入门案例

* 建立一个web项目

* 定义一个类,然后让该类去实现Servlet接口, 重写方法

* 配置servlet的访问路径


import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

public class ServletDemo1 implements Servlet {

         @Override

         public void destroy(){        

         }

         @Override

         public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

                   System.out.println("哦....ServletDemo1...被访问了................");

         }

         @Override

         public ServletConfig getServletConfig() {

                   return null;

         }

         @Override

         public String getServletInfo() {

                   return "";

         }

         @Override

         public void init(ServletConfig config) throws ServletException {

                   System.out.println("init....ServletDemo1................");

         }

}

在web.xml<web-app></web-app>中配置: 

  <servlet>

                <servlet-name>servletDemo1</servlet-name>

                <servlet-class>com.heima.servlet.ServletDemo1</servlet-class>

                <load-on-startup>5</load-on-startup>

  </servlet>

  <servlet-mapping>

                <servlet-name>servletDemo1</servlet-name>

                <url-pattern>/haha</url-pattern>   注:这是代表要访问的路径,可以随意定义,这是浏览器要输入的路径

  </servlet-mapping>

 

Servlet的入门案例