早期請求的jsp實際上就是一個java類,這個類到底是什么呢?
10年積累的網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計制作后付款的網(wǎng)站建設(shè)流程,更有寧蒗免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
out.println("helloworld");
%>
以上jsp生成的部分代碼如下
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {}
發(fā)現(xiàn)HttpJspBese這個類的繼承結(jié)構(gòu)如下:
public abstract class org.apache.jasper.runtime.HttpJspBase extends javax.servlet.http.HttpServlet implements javax.servlet.jsp.HttpJspPage {}
總結(jié):javax.servlet.http.HttpServlet就是Servlet的核心類,請求一個jsp頁面實際上就是請求一個Servlet
Servlet 是一個 Java程序,是在服務(wù)器上運行以處理客戶端請求并做出響應(yīng)的程序,Java Servlet是和平臺無關(guān)的服務(wù)器端組件,它運行在Servlet容器中Servlet容器負責Servlet和客戶的通信以及調(diào)用Servlet的方法,Servlet和客戶的通信采用“請求/響應(yīng)”的模式
Servlet可完成如下功能
創(chuàng)建并返回基于客戶請求的動態(tài)HTML頁面。
創(chuàng)建可嵌入到現(xiàn)有 HTML 頁面中的部分 HTML 頁面(HTML 片段)
與其它服務(wù)器資源(如數(shù)據(jù)庫或基于Java的應(yīng)用程序)進行通信
該類的源代碼如下:
package javax.servlet;
import java.io.IOException;
public interface Servlet {
public void init(ServletConfig config) throws ServletException;
public ServletConfig getServletConfig();
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
public String getServletInfo();
public void destroy();
}
該接口的源代碼如下
package javax.servlet;
import java.util.Enumeration;
public interface ServletConfig {
public String getServletName();
public ServletContext getServletContext();
public String getInitParameter(String name);
public Enumeration getInitParameterNames();
}
注意:一個Servlet對象與一個ServletConfig對象一一對應(yīng)
源代碼如下:
package javax.servlet;
import java.io.IOException;
import java.util.Enumeration;
public abstract class GenericServlet
implements Servlet, ServletConfig, java.io.Serializable{
private transient ServletConfig config;
public GenericServlet() { }
public void destroy() {}
public String getInitParameter(String name) {
return getServletConfig().getInitParameter(name);
}
public Enumeration getInitParameterNames() {
return getServletConfig().getInitParameterNames();
}
public ServletConfig getServletConfig() {
return config;
}
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
}
public String getServletInfo() {
return "";
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
public void init() throws ServletException {
}
public void log(String msg) {
getServletContext().log(getServletName() + ": "+ msg);
}
public void log(String message, Throwable t) {
getServletContext().log(getServletName() + ": " + message, t);
}
public abstract void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
public String getServletName() {
return config.getServletName();
}
}
注意:該類是一個抽象類,對Servlet和ServletConfig接口中的大部分方法做了默認的實現(xiàn),并且增加了log等方法
部分源代碼如下:
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
…
}else if(method.equals(METHOD_POST)) {
…
}
}
package cn.org.kingdom.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//如果在Servlet中用到response對象向客戶端響應(yīng)時,此時要在得到流之前設(shè)置contentType
response.setContentType("text/html;charset=utf-8");
PrintWriter ps = response.getWriter();
ps.write("");
ps.write("helloServlet ");
ps.write("你好 Servlet");
ps.write("");
ps.close();
}
}
web04
index.jsp
MyServlet
cn.org.kingdom.servlet.MyServlet
MyServlet
/MyServlet
在前面講解Servlet接口的api中就提到生命周期的概念
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:
The servlet is constructed, then initialized with the init method.
Any calls from clients to the service method are handled.
The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.
圖解:
可以設(shè)置當服務(wù)器啟動時,調(diào)用構(gòu)造和init方法,在web.xml中設(shè)置以下代碼
LifeServlet
cn.org.kingdom.servlet.LifeServlet
1
LifeServlet
/LifeServlet
當訪問多次的時候,發(fā)現(xiàn)Servlet的構(gòu)造方法不會執(zhí)行多次,所以Servlet是一個單例設(shè)計,要求不要再servlet中定義成員變量(這個成員變量會被多個線程共享),有可能會產(chǎn)生線程安全問題
在web.xml中的servlet節(jié)點配置初始化參數(shù)
LifeServlet
cn.org.kingdom.servlet.LifeServlet
name
hello LifeServlet
1
使用ServletConfig來進行獲取初始化參數(shù)的值
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println(this.getServletConfig().getInitParameter("name"));
}
在web.xml中配置,該節(jié)點與Servlet節(jié)點處于同一級
name
context-value
使用ServletContext對象獲取上下文參數(shù)的值
String name = this.getServletContext().getInitParameter("name");
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。