大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
早期请求的jsp实际上就是一个java类,这个类到底是什么呢?
10年积累的网站建设、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有宁蒗免费网站建设让你可以放心的选择与我们合作。<%@ 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 {}
发现HttpJspBese这个类的继承结构如下:
public abstract class org.apache.jasper.runtime.HttpJspBase extends javax.servlet.http.HttpServlet implements javax.servlet.jsp.HttpJspPage {}
总结:javax.servlet.http.HttpServlet就是Servlet的核心类,请求一个jsp页面实际上就是请求一个Servlet
Servlet 是一个 Java程序,是在服务器上运行以处理客户端请求并做出响应的程序,Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中Servlet容器负责Servlet和客户的通信以及调用Servlet的方法,Servlet和客户的通信采用“请求/响应”的模式
Servlet可完成如下功能
创建并返回基于客户请求的动态HTML页面。
创建可嵌入到现有 HTML 页面中的部分 HTML 页面(HTML 片段)
与其它服务器资源(如数据库或基于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接口中的大部分方法做了默认的实现,并且增加了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("");
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.
图解:
可以设置当服务器启动时,调用构造和init方法,在web.xml中设置以下代码
LifeServlet
cn.org.kingdom.servlet.LifeServlet
1
LifeServlet
/LifeServlet
当访问多次的时候,发现Servlet的构造方法不会执行多次,所以Servlet是一个单例设计,要求不要再servlet中定义成员变量(这个成员变量会被多个线程共享),有可能会产生线程安全问题
在web.xml中的servlet节点配置初始化参数
LifeServlet
cn.org.kingdom.servlet.LifeServlet
name
hello LifeServlet
1
使用ServletConfig来进行获取初始化参数的值
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println(this.getServletConfig().getInitParameter("name"));
}
在web.xml中配置,该节点与Servlet节点处于同一级
name
context-value
使用ServletContext对象获取上下文参数的值
String name = this.getServletContext().getInitParameter("name");
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。