一、 什么是Servlet?
Java Servlet 是運(yùn)行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序莹汤;他是瀏覽器(HTTP客戶端)請求和HTTP服務(wù)器上資源(訪問數(shù)據(jù)庫)之間的中間層鸠珠。
二、什么是Servlet的運(yùn)行環(huán)境紫新?
Servlet容器是Java Servlet程序的運(yùn)行環(huán)境揭厚,比如但绕,常用的tomcat救崔,同時它也是web服務(wù)器惶看;
三、什么是Servlet的生命周期六孵?
Servlet的創(chuàng)建到銷毀的過程稱為Servlet的生命周期纬黎;具體分為三個階段:
- Servlet初期化 init()
- Servlet處理請求 service()
- Servlet銷毀 destroy()
四、創(chuàng)建一個Servlet示例程序
HellowordServlet.java
//@WebServlet(urlPatterns = {"/helloworldServet"})
public class HellowordServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HellowordServlet() {
super();
// TODO Auto-generated constructor stub
}
/**初始化方法
* @see #init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("Servlet初期化");
}
/**
* servlet銷毀方法
* @see #destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
System.out.println("Servlet銷毀");
}
/**
* get請求
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //2.重新定義doGet
response.setContentType("text/html;charset=UTF-8"); //3.設(shè)置響應(yīng)類型
PrintWriter out = response.getWriter(); //4.取得響應(yīng)輸出對象
String name = request.getParameter("name"); //5.取得請求參數(shù)
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("<h1>Hello!" + name + "</h1>"); //6.數(shù)據(jù)輸出到前端
out.print("This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.xml配置
<servlet>
<servlet-name>helloworldServet</servlet-name>
<servlet-class>com.critc.servlet.HellowordServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworldServet</servlet-name>
<url-pattern>/helloworldServet</url-pattern>
</servlet-mapping>
這里面配置一個servlet劫窒,指定名稱和對應(yīng)的class本今,再配置servlet-mapping,指定url映射的類型主巍,這里設(shè)置為/helloworldServet
冠息,意思就是以http://localhost:8080/helloworldServet
開頭的請求都會指向這個servlet進(jìn)行處理。
配置servlet有兩種方式孕索,在web3.0之前逛艰,一般都是通過配置web.xml來進(jìn)行配置,就是上面說的這種搞旭,在web3.0之后散怖,可以采用注解的方式,比如
@WebServlet(urlPatterns = {"/helloworldServet"})
這個注解寫在HelloworldServet.java
的定義上肄渗。這有一個限制镇眷,就是web.xml
的最上方定義得是3.0級以上比如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
這里面的版本version得是3.0以上
這個工程部署,啟動翎嫡,在瀏覽器地址欄輸入:http://localhost:8080/helloworldServet?name=java
可以看到如下界面:
servlet啟動
servlet的get請求欠动,通過獲取name參數(shù),并輸出字符串的值钝的。