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