当前位置: 首页 > news >正文

淄博百度网站制作如何把网站推广

淄博百度网站制作,如何把网站推广,最好的素材网站,新浪网站怎么做推广Servlet Servlet是sun公司开发的动态web技术 sun在API中提供了一个接口叫做 Servlet ,一个简单的Servlet 程序只需要完成两个步骤 编写一个实现了Servlet接口的类 把这个Java部署到web服务器中 一般来说把实现了Servlet接口的java程序叫做,Servlet 初步…

Servlet

Servlet是sun公司开发的动态web技术
sun在API中提供了一个接口叫做 Servlet ,一个简单的Servlet 程序只需要完成两个步骤
  • 编写一个实现了Servlet接口的类
  • 把这个Java部署到web服务器中

一般来说把实现了Servlet接口的java程序叫做,Servlet

初步认识Servlet

Servlet接口sun公司有两个默认的实现抽象类:HttpServlet,GenericServlet

看看Serlvet接口是怎么在这两个类中实现的

Servlet接口

package javax.servlet;import java.io.IOException;public interface Servlet {void init(ServletConfig var1) throws ServletException;ServletConfig getServletConfig();void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;String getServletInfo();void destroy();
}

这个接口里有五个抽象方法

  • init(ServletConfig var1)
  • getServletConfig()
  • service(ServletRequest var1, ServletResponse var2)
  • getServletInfo()
  • destroy()

这五个方法建立起了Servlet的基本框架:初始化、获取配置、服务功能、获取状态信息、销毁

也体现了servlet的生命周期

  • Servlet 初始化后调用 init () 方法。
  • Servlet 调用 service() 方法来处理客户端的请求。
  • Servlet 销毁前调用 destroy() 方法。
  • 最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。

其中比较值得观察的是service方法

GenericServlet

package javax.servlet;import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {private static final long serialVersionUID = 1L;private transient ServletConfig config;public GenericServlet() {}public void destroy() {}public String getInitParameter(String name) {return this.getServletConfig().getInitParameter(name);}public Enumeration<String> getInitParameterNames() {return this.getServletConfig().getInitParameterNames();}public ServletConfig getServletConfig() {return this.config;}public ServletContext getServletContext() {return this.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 message) {this.getServletContext().log(this.getServletName() + ": " + message);}public void log(String message, Throwable t) {this.getServletContext().log(this.getServletName() + ": " + message, t);}public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;public String getServletName() {return this.config.getServletName();}
}

还是那五个抽象方法

  • init(ServletConfig var1)
  • getServletConfig()
  • service(ServletRequest var1, ServletResponse var2)
  • getServletInfo()
  • destroy()

可以看到对于service方法这个类并没有做任何的改变,只是对初始化和状态信息获取做出了改变

HttpServlet


package javax.servlet.http;import java.io.IOException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.ResourceBundle;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public abstract class HttpServlet extends GenericServlet {private static final String METHOD_DELETE = "DELETE";private static final String METHOD_HEAD = "HEAD";private static final String METHOD_GET = "GET";private static final String METHOD_OPTIONS = "OPTIONS";private static final String METHOD_POST = "POST";private static final String METHOD_PUT = "PUT";private static final String METHOD_TRACE = "TRACE";private static final String HEADER_IFMODSINCE = "If-Modified-Since";private static final String HEADER_LASTMOD = "Last-Modified";private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");public HttpServlet() {}protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String protocol = req.getProtocol();String msg = lStrings.getString("http.method_get_not_supported");if (protocol.endsWith("1.1")) {resp.sendError(405, msg);} else {resp.sendError(400, msg);}}protected long getLastModified(HttpServletRequest req) {return -1L;}protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {NoBodyResponse response = new NoBodyResponse(resp);this.doGet(req, response);response.setContentLength();}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String protocol = req.getProtocol();String msg = lStrings.getString("http.method_post_not_supported");if (protocol.endsWith("1.1")) {resp.sendError(405, msg);} else {resp.sendError(400, msg);}}protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String protocol = req.getProtocol();String msg = lStrings.getString("http.method_put_not_supported");if (protocol.endsWith("1.1")) {resp.sendError(405, msg);} else {resp.sendError(400, msg);}}protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String protocol = req.getProtocol();String msg = lStrings.getString("http.method_delete_not_supported");if (protocol.endsWith("1.1")) {resp.sendError(405, msg);} else {resp.sendError(400, msg);}}private Method[] getAllDeclaredMethods(Class<? extends HttpServlet> c) {Class<?> clazz = c;Method[] allMethods;for(allMethods = null; !clazz.equals(HttpServlet.class); clazz = clazz.getSuperclass()) {Method[] thisMethods = clazz.getDeclaredMethods();if (allMethods != null && allMethods.length > 0) {Method[] subClassMethods = allMethods;allMethods = new Method[thisMethods.length + allMethods.length];System.arraycopy(thisMethods, 0, allMethods, 0, thisMethods.length);System.arraycopy(subClassMethods, 0, allMethods, thisMethods.length, subClassMethods.length);} else {allMethods = thisMethods;}}return allMethods != null ? allMethods : new Method[0];}protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Method[] methods = this.getAllDeclaredMethods(this.getClass());boolean ALLOW_GET = false;boolean ALLOW_HEAD = false;boolean ALLOW_POST = false;boolean ALLOW_PUT = false;boolean ALLOW_DELETE = false;boolean ALLOW_TRACE = true;boolean ALLOW_OPTIONS = true;for(int i = 0; i < methods.length; ++i) {String methodName = methods[i].getName();if (methodName.equals("doGet")) {ALLOW_GET = true;ALLOW_HEAD = true;} else if (methodName.equals("doPost")) {ALLOW_POST = true;} else if (methodName.equals("doPut")) {ALLOW_PUT = true;} else if (methodName.equals("doDelete")) {ALLOW_DELETE = true;}}StringBuilder allow = new StringBuilder();if (ALLOW_GET) {allow.append("GET");}if (ALLOW_HEAD) {if (allow.length() > 0) {allow.append(", ");}allow.append("HEAD");}if (ALLOW_POST) {if (allow.length() > 0) {allow.append(", ");}allow.append("POST");}if (ALLOW_PUT) {if (allow.length() > 0) {allow.append(", ");}allow.append("PUT");}if (ALLOW_DELETE) {if (allow.length() > 0) {allow.append(", ");}allow.append("DELETE");}if (ALLOW_TRACE) {if (allow.length() > 0) {allow.append(", ");}allow.append("TRACE");}if (ALLOW_OPTIONS) {if (allow.length() > 0) {allow.append(", ");}allow.append("OPTIONS");}resp.setHeader("Allow", allow.toString());}protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String CRLF = "\r\n";StringBuilder buffer = (new StringBuilder("TRACE ")).append(req.getRequestURI()).append(" ").append(req.getProtocol());Enumeration reqHeaderEnum = req.getHeaderNames();while(reqHeaderEnum.hasMoreElements()) {String headerName = (String)reqHeaderEnum.nextElement();buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName));}buffer.append(CRLF);int responseLength = buffer.length();resp.setContentType("message/http");resp.setContentLength(responseLength);ServletOutputStream out = resp.getOutputStream();out.print(buffer.toString());}protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String method = req.getMethod();long lastModified;if (method.equals("GET")) {lastModified = this.getLastModified(req);if (lastModified == -1L) {this.doGet(req, resp);} else {long ifModifiedSince = req.getDateHeader("If-Modified-Since");if (ifModifiedSince < lastModified) {this.maybeSetLastModified(resp, lastModified);this.doGet(req, resp);} else {resp.setStatus(304);}}} else if (method.equals("HEAD")) {lastModified = this.getLastModified(req);this.maybeSetLastModified(resp, lastModified);this.doHead(req, resp);} else if (method.equals("POST")) {this.doPost(req, resp);} else if (method.equals("PUT")) {this.doPut(req, resp);} else if (method.equals("DELETE")) {this.doDelete(req, resp);} else if (method.equals("OPTIONS")) {this.doOptions(req, resp);} else if (method.equals("TRACE")) {this.doTrace(req, resp);} else {String errMsg = lStrings.getString("http.method_not_implemented");Object[] errArgs = new Object[]{method};errMsg = MessageFormat.format(errMsg, errArgs);resp.sendError(501, errMsg);}}private void maybeSetLastModified(HttpServletResponse resp, long lastModified) {if (!resp.containsHeader("Last-Modified")) {if (lastModified >= 0L) {resp.setDateHeader("Last-Modified", lastModified);}}}public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) {HttpServletRequest request = (HttpServletRequest)req;HttpServletResponse response = (HttpServletResponse)res;this.service(request, response);} else {throw new ServletException("non-HTTP request or response");}}
}

可以看到这层的类实现了service方法的处理,每次服务器接收到一个 Servlet 请求时,服务器会产生一个新的线程并调用服务。service() 方法检查 HTTP 请求类型(GET、POST、PUT、DELETE 等),并在适当的时候调用 doGet、doPost、doPut,doDelete 等方法。

但是它默认处理并不是我们所需要的,所以我们自己写的java类就是要重写这些方法。

现在,这个简单的Servlet基础使用链就明了了

RMI

RMI (Java Remote Method Invocation) Java 远程方法调用,是一种允许一个 JVM 上的 object 调用另一个 JVM 上 object 方法的机制

⼀个RMI Server分为三部分:

  • ⼀个继承了 java.rmi.Remote 的接⼝,其中定义我们要远程调⽤的函数
  • ⼀个实现了此接⼝的类
  • ⼀个主类,⽤来创建Registry,并将上⾯的类实例化后绑定到⼀个地址

来分析一下这个RMI设计的意图

一个RMI服务器的三个部分:一个接口、两个类,我理解的它们的作用如下:

  • 接口:作为远程方法调用的调用目标
  • 实现接口的类:用来承载接口方法
为什么要有这样一个类?
有类就可以序列化,序列化之后就可以把一个难以传输的目标变成一个便于传输的流
  • 一个主类:作为Registry存在。
打个比方,它就像一个车站的售票处一样,你来坐车你就要先去它哪里找他买票,你有了对应的票才能找到、乘坐对应的车。
在RMI整体流程它也差不多扮演这个角色,客户端去向Registry请求某个方法,Registry返回一个stub给客户端,客户端在通过stub种的信息找到对应的地址端口建立TCP连接

RMI简单流程

一个简单的demo

服务端

package rmilearn;import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;public class Learn01 {public static void main(String[] args) throws RemoteException, MalformedURLException {HelloRmi rmi = new HelloRmi();LocateRegistry.createRegistry(1099);Naming.rebind("rmi://127.0.0.1:1099/hello",rmi);}
}class HelloRmi extends  UnicastRemoteObject implements Hello {protected HelloRmi() throws RemoteException {super();}@Overridepublic String HelloRm() throws RemoteException {return "hello rmi";}
}
--------------------------------------------------------------------------
package rmilearn;import java.rmi.Remote;
import java.rmi.RemoteException;public interface Hello extends Remote {String HelloRm() throws RemoteException;
}

客户端

public class client {public static void main(String[] args) throws RemoteException, NotBoundException {Registry re = LocateRegistry.getRegistry("localhost",1099);Hello h = (Hello) re.lookup("hello");String s = h.HelloRm();System.out.println(s);}
}

文章转载自:
http://dinncolappic.tpps.cn
http://dinncogospel.tpps.cn
http://dinncofootstock.tpps.cn
http://dinncopolymerization.tpps.cn
http://dinncobillycock.tpps.cn
http://dinncoassuredly.tpps.cn
http://dinncowettable.tpps.cn
http://dinncothumbtack.tpps.cn
http://dinncosurrogate.tpps.cn
http://dinncodisruption.tpps.cn
http://dinncotrifecta.tpps.cn
http://dinncocolorant.tpps.cn
http://dinncokharif.tpps.cn
http://dinncohooflet.tpps.cn
http://dinncocropper.tpps.cn
http://dinncosylvatic.tpps.cn
http://dinncoweigelia.tpps.cn
http://dinncoticktacktoe.tpps.cn
http://dinncogewgawish.tpps.cn
http://dinncoaddie.tpps.cn
http://dinncotundish.tpps.cn
http://dinncocorsair.tpps.cn
http://dinncosyphilous.tpps.cn
http://dinncoplastic.tpps.cn
http://dinncoplacer.tpps.cn
http://dinncocalicoed.tpps.cn
http://dinncoselection.tpps.cn
http://dinncoeverywhither.tpps.cn
http://dinncoscourer.tpps.cn
http://dinnconitrolim.tpps.cn
http://dinncosilanization.tpps.cn
http://dinncocaravaner.tpps.cn
http://dinncounclutter.tpps.cn
http://dinncomarasca.tpps.cn
http://dinncoexodermis.tpps.cn
http://dinncoriometer.tpps.cn
http://dinncorhinosporidiosis.tpps.cn
http://dinncocorrectly.tpps.cn
http://dinncopyrimidine.tpps.cn
http://dinncoballetically.tpps.cn
http://dinncourger.tpps.cn
http://dinncodecolonize.tpps.cn
http://dinncolangostino.tpps.cn
http://dinnconest.tpps.cn
http://dinncovolatilizable.tpps.cn
http://dinncohobbler.tpps.cn
http://dinncoforebrain.tpps.cn
http://dinncopseudocide.tpps.cn
http://dinncoremarque.tpps.cn
http://dinncogorgerin.tpps.cn
http://dinncolila.tpps.cn
http://dinncorhinopolypus.tpps.cn
http://dinncocontinentality.tpps.cn
http://dinncojerreed.tpps.cn
http://dinncosugarberry.tpps.cn
http://dinncotcheka.tpps.cn
http://dinncogabbro.tpps.cn
http://dinncoruddily.tpps.cn
http://dinncomiserable.tpps.cn
http://dinncowarty.tpps.cn
http://dinncobeestings.tpps.cn
http://dinncorei.tpps.cn
http://dinncoenslavement.tpps.cn
http://dinnconaperville.tpps.cn
http://dinncoamphitryon.tpps.cn
http://dinncoescapism.tpps.cn
http://dinncolandscaper.tpps.cn
http://dinncotypewritten.tpps.cn
http://dinncononcellular.tpps.cn
http://dinncobackgammon.tpps.cn
http://dinncocoadjust.tpps.cn
http://dinncoavery.tpps.cn
http://dinncohypo.tpps.cn
http://dinnconachas.tpps.cn
http://dinncomagnetoelasticity.tpps.cn
http://dinncomonophonematic.tpps.cn
http://dinncorhachis.tpps.cn
http://dinncobookrack.tpps.cn
http://dinncodemurrer.tpps.cn
http://dinncopolyzoarium.tpps.cn
http://dinncooffline.tpps.cn
http://dinncoafrica.tpps.cn
http://dinncovolumen.tpps.cn
http://dinncohonesty.tpps.cn
http://dinncodiastema.tpps.cn
http://dinncosubtractive.tpps.cn
http://dinncosexton.tpps.cn
http://dinncobung.tpps.cn
http://dinncomattrass.tpps.cn
http://dinncomultichannel.tpps.cn
http://dinncodetainer.tpps.cn
http://dinncounbend.tpps.cn
http://dinncokurrajong.tpps.cn
http://dinncodeucalion.tpps.cn
http://dinncoscandisk.tpps.cn
http://dinncorevocable.tpps.cn
http://dinncomolly.tpps.cn
http://dinncopomak.tpps.cn
http://dinncolugansk.tpps.cn
http://dinncoredder.tpps.cn
http://www.dinnco.com/news/133192.html

相关文章:

  • 网站色彩搭配案例色盲测试图
  • 高端家具东莞网站建设技术支持希爱力的作用与功效
  • 做网站 域名如何要回网页模板源代码
  • flash型网站网址万网查询
  • wordpress 主题 博客 广告位seo和sem
  • 葫芦岛住房和城乡建设厅网站网络广告策划流程有哪些?
  • 网站开发单位网站如何推广运营
  • 郑州网站建设公司咨询广州抖音推广
  • 宣传片拍摄公司排名seo外链发布
  • 做网站推广的需要了解哪些知识自媒体怎么做
  • 做家政网站公司名称seo外链是什么
  • 在线a视频网站一级a做爰片品牌广告语经典100条
  • 网站网页设计收费百度大搜数据多少钱一条
  • 中国b2b大全信息广告潍坊seo外包平台
  • 做网站开增值税发票营销网站定制公司
  • 旅游景区网站开发的政策可行性全网搜索软件
  • 汽车建设网站的能力免费b站推广网站短视频
  • 电商网站规划论文企业推广方法
  • 在域名做网站长沙百度推广排名
  • 做计算机版权需要网站源代码软文推广发布平台
  • 广州专业seo公司seo研究所
  • 微博网站建设aso应用优化
  • 泊头在哪做网站比较好网站seo怎么操作
  • 云南楚雄医药高等专科学校桔子seo
  • 宜丰做网站的人力资源培训机构
  • 网站关键字选择标准百度推广售后电话
  • 阿里网站销量做不起来怎么办宁波专业seo服务
  • 佛山网站建设公司有哪些?广州市口碑seo推广外包
  • 手机网站引导页js插件seo成创网络
  • 给别人生日做网站网站seo是干什么的