selvlet開發入門

Servlet入門

  1.    在tomcat中新建一個day04web應用,然後在web應用中新建一個WEB-INF/classes目錄
  2. 在classes目錄中新建一個FirstServlet.java
           package cn.itcast;
           import java.io.*;
           import javax.servlet.*; 
           public class FirstServlet extends GenericServlet{
                public void service(ServletRequest req, ServletResponse res)throws ServletException,java.io.IOException{
                       OutputStream out = res.getOutputStream();
                      out.write("hello servlet!!!".getBytes());
             }
         }
      3.set classpath= %classpath%C:\apache-tomcat-8.0.35\lib\servlet.jar    編譯servlet
     4.在WEB-INF目錄中新建一個web.xml文件,配置servlet對外訪問路徑

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">
    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>cn.itcast.FirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/FirstServlet</url-pattern>
    </servlet-mapping>

</web-app>

   5.啓動tomcat訪問
     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章