做网站聚合做权重难吗,域名和空间都有了怎么做网站,开通建立企业网站,做景观私活的网站1、ServletContext 概念ServletContext官方叫servlet上下文。服务器会为每一个工程创建一个对象#xff0c;这个对象就是ServletContext对象。这个对象全局唯一#xff0c;而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。Web应用程序是Servlet、JSP页…1、ServletContext 概念ServletContext官方叫servlet上下文。服务器会为每一个工程创建一个对象这个对象就是ServletContext对象。这个对象全局唯一而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。Web应用程序是Servlet、JSP页面和内容的集合被Eclipse自动部署在Tomcat服务器URL名称空间的特定目录(如/catalog)下。注意有时候可能通过.war文件部署。对于在其部署描述符中标记为distributed的Web应用程序每个虚拟机中都有一个上下文实例这个实例称为上下文对象。例如当前的Tomcat中部署了WebProject01WebProject02……WebProject07共7个Web应用程序那么在启动Tomcat时将分别为每一个Web应用程序创建一个上下文对象。在这种情况下上下文不能用作共享全局信息的位置而使用外部资源比如数据库、或者文件服务器等。因为这些信息不是真正的全局信息。作用是一个域对象可以读取全局配置参数可以搜索当前工程目录下面的资源文件可以获取当前工程名字了解2. ServletContext接口2.1 获取ServletContext对象ServletConfig接口中定义的getServletContext方法由于自定义的Servlet类间接实现了ServletConfig接口因此可以直接调用getServletContext方法返回ServletContext对象 JSP文件中使用上下文对象的方法JSP文件的内置对象application即上下文对象可以调用ServletContext接口中的任意方法Servlet中获取以下两种都可以protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取Servlet上下文引用 ServletContext context1 this.getServletContext(); ServletContext context2 request.getServletContext(); }jsp中获取% ServletContext context1 this.getServletContext(); ServletContext context2 request.getServletContext(); %2.2 域对象常用方法void setAttribute(String key,Object value) 往域对象里面添加数据添加时以key-value形式添加Object getAttribute(String key) 根据指定的key读取域对象里面的数据void removeAttribute(name); 根据指定的key从域对象里面删除数据2.3 读取全局配置参数方法String getInitParameter(String path) 返回上下文参数的值EnumerationString getInitParameterNames() 获取所有参数名称列表代码案例1在web.xml中配置全局参数!-- 全局配置参数因为不属于任何一个servlet但是所有的servlet都可以通过servletContext读取 这个数据 -- context-param param-nameuserName/param-name param-value86_god/param-value /context-param context-param param-namee-mail/param-name param-value2584966199qq.com/param-value /context-param context-param param-nameurl/param-name param-valuehttps://www.zhihu.com/people/he-lai-xin-huan/param-value /context-param2在Servlet中获取全局变量protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context this.getServletContext(); String userName context.getInitParameter(userName); System.out.println(userName); //获取所有全局变量的参数名称 EnumerationString list context.getInitParameterNames(); //遍历所有参数 while (list.hasMoreElements()) { String name list.nextElement(); String value getServletContext().getInitParameter(name); System.out.println(name : value ); } }2.4 可以搜索当前工程目录下面的资源文件getServletContext().getRealPath(path) 根据相对路径获取服务器上资源的绝对路径getServletContext().getResourceAsStream(path) 根据相对路径获取服务器上资源的输入字节流getServletContext().getContextPath() 获取项目名称protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //根据相对路径获取服务器上资源的绝对路径 System.out.println(getServletContext().getRealPath(web.xml)); //根据相对路径获取服务器上资源的输入字节流 System.out.println(getServletContext().getResourceAsStream(web.xml)); //获取项目名称 System.out.println(getServletContext().getContextPath()); }3. 代码案例实现网络聊天室image.png实现一个网上在线多人聊天室页面比较丑主要写后端直接上代码页面部分代码1index.jsp% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8% !DOCTYPE html html head meta charsetUTF-8 titleInsert title here/title /head frameset cols80%,* frame nameindex srcchatFrame.jsp scrollingno / frame nameonlineList srconlineList.jsp scrollingno / noframes body 对不起您的浏览器不支持框架,请使用更好的浏览器。 /body /noframes /frameset /html2chatFrame.jsp% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8% !DOCTYPE html html head meta charsetUTF-8 title聊天室/title /head frameset rows80%,* frame namechattingRecords srcchattingRecords.jsp scrollingno / frame nameinputFrame srcinputFrame.jsp scrollingno / noframes body 对不起您的浏览器不支持框架,请使用更好的浏览器。 /body /noframes /frameset /html3chattingRecords.jsp%page importjava.util.Date% %page importjava.text.SimpleDateFormat% %page importcom.company.project.po.Message% %page importjava.util.ArrayList% %page importorg.apache.jasper.tagplugins.jstl.core.ForEach% % page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8% % ArrayListMessage messages (ArrayListMessage) getServletContext().getAttribute(messages); ArrayListString userList (ArrayListString) getServletContext().getAttribute(userList); String userId request.getSession().getId(); if (messages null) { messages new ArrayList(); } if(userList null){ userList new ArrayList(); } if(!userList.contains(userId)){ userList.add(userId); SimpleDateFormat df new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//设置日期格式 String sendTime df.format(new Date());// new Date()为获取当前系统时间 Message message new Message(); message.setUserName(系统提示); message.setContent(欢迎request.getSession().getId()加入); message.setSendTime(sendTime); messages.add(message); } getServletContext().setAttribute(userList, userList); getServletContext().setAttribute(messages, messages); % !DOCTYPE html html head meta charsetUTF-8 title聊天记录/title script function fresh() { location.reload(); } window.setInterval(function() { fresh(); }, 1000); /script /head body % if (messages ! null) for (Message message : messages) { % %message.getSendTime()%nbsp;%message.getUserName()%说%message.getContent() % br % } % /body /html4inputFrame.jsp% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8% % String path request.getContextPath(); String basePath request.getScheme() :// request.getServerName() : request.getServerPort() path; % !DOCTYPE html html head meta charsetUTF-8 title输入框/title script typetext/javascript function IsNull() { var mess document.getElementById(inputText).value; var send document.getElementById(sendForm); console.log(mess); console.log(send); if(mess ! null mess ! ){ send.submit(); } } /script /head body form action%basePath %/send-message methodget idsendForm textarea idinputText name inputText stylewidth: 100% /textarea input typebutton value发送 onclickIsNull() /form /body /html5onlineList.jsp%page importjava.util.ArrayList% % page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8% % ArrayListString userList (ArrayListString) getServletContext().getAttribute(userList); if(userList null){ userList new ArrayList(); } % !DOCTYPE html html head meta charsetUTF-8 title在线列表/title script function fresh() { location.reload(); } window.setInterval(function() { fresh(); }, 1000); /script /head body h2在线用户列表(目前在线人数%userList.size() %)h2 % for(String user:userList){ % %user %br % } % /body /html模型model代码Message.javapackage com.company.project.po; public class Message { private String userName; private String sendTime; private String content; public String getUserName() { return userName; } public String getContent() { return content; } public void setUserName(String userName) { this.userName userName; } public void setContent(String content) { this.content content; } public String getSendTime() { return sendTime; } public void setSendTime(String sendTime) { this.sendTime sendTime; } Override public String toString() { return Message [userName userName , sendTime sendTime , content content ]; } }Servlet代码SendMessage.javapackage com.company.project.servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.company.project.po.Message; WebServlet(/send-message) public class SendMessage extends HttpServlet { private static final long serialVersionUID 1L; public SendMessage() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置相应内容类型 request.setCharacterEncoding(utf-8); response.setContentType(text/html;charsetutf-8); response.setCharacterEncoding(utf-8); HttpSession session request.getSession(); String inputText (String)request.getParameter(inputText); SimpleDateFormat df new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//设置日期格式 String sendTime df.format(new Date());// new Date()为获取当前系统时间 Message message new Message(); message.setUserName(session.getId()); message.setContent(inputText); message.setSendTime(sendTime); ArrayListMessage messages (ArrayListMessage)getServletContext().getAttribute(messages); if(messages null) { messages new ArrayList(); } messages.add(message); getServletContext().setAttribute(messages, messages); response.sendRedirect(page/inputFrame.jsp); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }越努力越幸运 我们亦是拾光者AI大模型学习福利作为一名热心肠的互联网老兵我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。一、全套AGI大模型学习路线AI大模型时代的学习之旅从基础到前沿掌握人工智能的核心技能因篇幅有限仅展示部分资料需要点击文章最下方名片即可前往获取二、640套AI大模型报告合集这套包含640份报告的合集涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师还是对AI大模型感兴趣的爱好者这套报告合集都将为您提供宝贵的信息和启示。因篇幅有限仅展示部分资料需要点击文章最下方名片即可前往获三、AI大模型经典PDF籍随着人工智能技术的飞速发展AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型如GPT-3、BERT、XLNet等以其强大的语言理解和生成能力正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。因篇幅有限仅展示部分资料需要点击文章最下方名片即可前往获四、AI大模型商业化落地方案因篇幅有限仅展示部分资料需要点击文章最下方名片即可前往获作为普通人入局大模型时代需要持续学习和实践不断提高自己的技能和认知水平同时也需要有责任感和伦理意识为人工智能的健康发展贡献力量