servlet(設置映射類)
新建項目:名稱:servletDemo
- 創(chuàng)建類
ServletDemo implements Servlet
- 實現(xiàn)方法service:方法體
System.out.println("hello servlet");
- 部署到服務器
- 菜單:window-->preperence-->MyEclipse-->Servers-->Tomcat 7.x-->選擇當前tomcat根路徑
- Servers選項卡 Tomcat 7.x-->右鍵菜單-->add Delopyment-->選擇項目servletDemo
- 配置服務器
- web.xml
<servlet> <servlet-name>servletDemo</servlet-name> <servlet-class>com.demo.ServletDemo</servlet-class> <!-- 當服務器啟動時就直接實例化枷颊,這個數(shù)字越大優(yōu)先級越小激挪,最小寫1 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>servletDemo</servlet-name> <url-pattern>/demo1</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index2.jsp</welcome-file> </welcome-file-list>
- 啟動服務器筛峭,訪問:
http://localhost:8080/servletDemo/demo1
- 查看控制臺是否輸出:
hello servlet
- ServletDemo類(生命周期)
public class ServletDemo implements Servlet{
/**
* 當應用被卸載時執(zhí)行
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* 返回服務器配置
*/
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
/**
* 返回服務器信息
*/
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
/**
* 實例化(第一次訪問時執(zhí)行)
*/
public ServletDemo(){
System.out.println("×××實例化×××");
}
/**
* 初始化(第一次訪問時執(zhí)行)
*/
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("×××初始化×××");
}
/**
* 每次被訪問時執(zhí)行
*/
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("×××應用響應×××");
}
}