早期請求的jsp實際上就是一個java類,這個類到底是什么呢?

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<%
out.println("helloworld");
%>
</body>
</html>以上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程序,是在服務器上運行以處理客戶端請求并做出響應的程序,Java Servlet是和平臺無關的服務器端組件,它運行在Servlet容器中Servlet容器負責Servlet和客戶的通信以及調(diào)用Servlet的方法,Servlet和客戶的通信采用“請求/響應”的模式
Servlet可完成如下功能
創(chuàng)建并返回基于客戶請求的動態(tài)HTML頁面。
創(chuàng)建可嵌入到現(xiàn)有 HTML 頁面中的部分 HTML 頁面(HTML 片段)
與其它服務器資源(如數(shù)據(jù)庫或基于Java的應用程序)進行通信


該類的源代碼如下:
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對象一一對應

源代碼如下:
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對象向客戶端響應時,此時要在得到流之前設置contentType
response.setContentType("text/html;charset=utf-8");
PrintWriter ps = response.getWriter();
ps.write("<html>");
ps.write("<head><title>helloServlet</title></head>");
ps.write("<body>你好 Servlet</body>");
ps.write("</html>");
ps.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>web04</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>cn.org.kingdom.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>

在前面講解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.圖解:

可以設置當服務器啟動時,調(diào)用構(gòu)造和init方法,在web.xml中設置以下代碼
<servlet>
<servlet-name>LifeServlet</servlet-name>
<servlet-class>cn.org.kingdom.servlet.LifeServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>LifeServlet</servlet-name>
<url-pattern>/LifeServlet</url-pattern>
</servlet-mapping>當訪問多次的時候,發(fā)現(xiàn)Servlet的構(gòu)造方法不會執(zhí)行多次,所以Servlet是一個單例設計,要求不要再servlet中定義成員變量(這個成員變量會被多個線程共享),有可能會產(chǎn)生線程安全問題
在web.xml中的servlet節(jié)點配置初始化參數(shù)
<servlet>
<servlet-name>LifeServlet</servlet-name>
<servlet-class>cn.org.kingdom.servlet.LifeServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>hello LifeServlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
使用ServletConfig來進行獲取初始化參數(shù)的值
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println(this.getServletConfig().getInitParameter("name"));
}
在web.xml中配置,該節(jié)點與Servlet節(jié)點處于同一級
<context-param>
<param-name>name</param-name>
<param-value>context-value</param-value>
</context-param>使用ServletContext對象獲取上下文參數(shù)的值
String name = this.getServletContext().getInitParameter("name");另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享標題:Servlet詳解-創(chuàng)新互聯(lián)
本文鏈接:http://chinadenli.net/article16/edcgg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、外貿(mào)網(wǎng)站建設、企業(yè)網(wǎng)站制作、標簽優(yōu)化、全網(wǎng)營銷推廣、品牌網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)