Java之servlet一
三種實現方式
- 實現Servlet
- 繼承GenericServlet
- 繼承HttpServlet
實現Servlet
void init(ServletConfig config)
ServletConfig getServletConfig()
void service( ServletRequest req, ServletResponse res)
String getServletInfo()
void destory()
繼承GenericServlet
void service(ServletRequest req,ServletResponse res)
繼承HttpServlet
void doPost(HttpServletRequest request, HttpServletResponse response)
void doGet(HttpServletRequest request, HttpServletResponse response)
部署
WEB_INF/web.xml ---> web-app節點下
<servlet> <servlet-name>ServletDemo1</servlet-name> <servlet-class
>gacl.servlet.test.ServletDemo1</servlet-class> </servlet><servlet-mapping> <servlet-name>ServletDemo1</servlet-name> <url-pattern>/test/test1</url-pattern> </servlet-mapping>
例子
以實現Servlet為例
一 . 編寫MyRawServlet.java檔案
package com.zhou.servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // public不能忘記 throws非throw destroy非destory public class MyRawServlet implements Servlet{ public void init(ServletConfig config) throws ServletException{ } public void destroy(){ } public ServletConfig getServletConfig(){ return null; } public String getServletInfo(){ return null; } public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException{ System.out.println("MyRawServlet"); res.getWriter().println("<h1>MyRawServlet</h1>"); } }
二. 編譯javac -d . MyRawServlet.java;
生成com資料夾,將來放到WEB-INF/classes目錄下(注:是WEB-INF不是WEB_INF)
PS:若提示程式包javax.servlet不存在,則在tomcat伺服器目錄下找到lib目錄下的servlet-api.jar檔案,放在與java檔案一樣的目錄下,切換到該目錄,執行javac -classpath ./servlet-api.jar -d . MyHttpServlet.java
三. 在tomcat伺服器目錄下的webapps建立一個web應用,如zhoutest;
四. 在zhoutest目錄下建立WEB-INF目錄,並在WEB_INF目錄下建立classes資料夾;
五. 編寫web.xml放在WEB-INF目錄下,web.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<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"
metadata-complete="true">
<servlet>
<servlet-name>MyRawServlet</servlet-name>
<servlet-class>com.zhou.servlet.MyRawServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyRawServlet</servlet-name>
<url-pattern>/MyRawServlet</url-pattern>
</servlet-mapping>
</web-app>
六. 啟動tomcat伺服器,輸入 localhost:8080/zhoutest/MyRawServlet ,結果如下:
附錄
繼承GenericServlet方式的MyGenericServlet.java如下:(其他與實現Servlet一致)
package com.zhou.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyGenericServlet extends GenericServlet{
public void service(ServletRequest req,ServletResponse res)
throws ServletException, IOException{
System.out.println("GenericServlet");
res.getWriter().println("<h1>GenericServlet</h1>");
}
}
繼承HttpServlet方式的MyHttpServlet.java如下:
package com.zhou.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyHttpServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException , IOException{
System.out.println("MyHttpServlet");
res.getWriter().println("<h1>MyHttpServlet</h1>");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
doGet(req,res);
}
}