ImageVerifierCode 换一换
你正在下载:

JavaWeb6.docx

[预览]
格式:DOCX , 页数:31 ,大小:257.06KB ,
资源ID:12030770      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/12030770.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JavaWeb6.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JavaWeb6.docx

1、JavaWeb60421JavaWeb之ServletContext对象,会话跟踪技术 王健,5490517011、 今天技术点1:Servlet的线程安全问题: 只有当声明的成员变量接收参数时,才会有线程安全问题。 解决方案:“ 1:最好的方式:就是将成员变量声明成局部变量。-速度快。Servlet也是单例的。 2:可以在方法上添加同步代码块。 3:也可以实现SingleThreadModel,此时Servlet将不再是单例的对象了。package cn.oracle.thread;import java.io.IOException;import javax.servlet.Servlet

2、Exception;import javax.servlet.SingleThreadModel;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ThreadDemo extends HttpServlet implements SingleThreadModel String name; public void doGet(HttpServletRequest

3、 req, HttpServletResponse resp) throws ServletException, IOException name = req.getParameter(name); try Thread.sleep(3000); catch (InterruptedException e) e.printStackTrace(); resp.getWriter().print(YOur name is :+name+,+this); 1:ServletContext对象。 ServletContext对象 一个web项目只有一个ServletContext对象。 当tomca

4、t启动时,由tomcat创建。 全局共享数据。 与所有登录的用户共享数据。 功能: 实现聊天。 实现统计页面的点击量。 获取本地数据并显示,如图片。1.2、ServletContext作为另一个域对象 可以保存数据: 所有的域对象: setAttribute(“name”,Object);保存某个对象。 getAttribute(“name”) : object 获取这个域对象的中的值。; 、 removeAttribute getAttributeNames1.3、获取这个域对象的方式: 1:通过ServletConfig获取这个域对象 ServletContext application

5、= ServletConfig.getServletContext(); 2:也可以在Request中获取 ServletContext application = request.getSession().getServletContext();1.4、功能 共享数据 1:这个程序中,所有的组件中(Servlet,jsp,filter.listener)就可以共享数据以下是共享数据的基本示例:代码如下:先向里面保存数据public class Ctx1Servlet extends HttpServlet public void doGet(HttpServletRequest reques

6、t, HttpServletResponse response) throws ServletException, IOException response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter(); ServletContext application = getServletConfig().getServletContext(); if (application.getAttribute(name) = null) Random random = new Random()

7、; String str = 保存的数是: + random.nextInt(1000); / 将这个随机数 application.setAttribute(name, str); out.print(放成功了:+str); else out.print(已经存在了数据); out.print(返回); 在第二个Servlet中获取数据:public class Ctx2Servlet extends HttpServlet public void doGet(HttpServletRequest request, HttpServletResponse response) throws S

8、ervletException, IOException response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter(); /直接从ServletContext中获取数据 ServletContext sc = request.getSession().getServletContext(); /获取数据 String str = (String) sc.getAttribute(name); out.print(22:+str); 1:保存所有登录的用户共享的数据。功能1:计算点

9、击量:public class CountServlet extends HttpServlet public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException /每一次来访问这个方法 ,都是点击量+1 ServletContext application = getServletContext(); /先从Application中取数据 Integer count = (Integer) application.getAttribu

10、te(count); /如果是第一个人来访问则为null if(count=null) count=1; else count+; /放回到applicatioin application.setAttribute(count,count); response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter(); out.println(); out.println(); out.println( A Servlet); out.println( ); out.print(这是一个新的信

11、息,。这个新闻被点击了:+application.getAttribute(count); out.println(); out.flush(); out.close(); 为了保证数据的原子性,应该同步,但在doGet方法k使用同步又很慢:就应该考虑开一个线程:public class CountServlet extends HttpServlet public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / 每一次来访问这

12、个方法 ,都是点击量+1 final ServletContext application = getServletContext(); new Thread() public void run() new AddOne(application).increment(); ; .start(); response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter(); out.println(); out.println(); out.println( A Servlet); out.pr

13、intln( ); out.print(这是一个新的信息,。这个新闻被点击了: + application.getAttribute(count); out.println(); out.flush(); out.close(); / 在这个代码中添加1class AddOne private ServletContext application; public AddOne(ServletContext sc) this.application=sc; public synchronized void increment() / 先从Application中取数据 Integer count

14、 = (Integer) application.getAttribute(count); / 如果是第一个人来访问则为null if (count = null) count = 1; else count+; / 放回到applicatioin application.setAttribute(count, count); 功能2:实现一个聊天程序:第一步:开发一个servlet先写出一个表单:public class ChatServlet extends HttpServlet public void doGet(HttpServletRequest request, HttpServ

15、letResponse response) throws ServletException, IOException /直接去请求doPost,只有用户第一次来这个页面时才会走这个doGet方法 doPost(request, response); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(text/html;charset=UTF-8); PrintWriter

16、 out = response.getWriter(); /1:写出一个表单 String form = + 发言: + ; form+=; out.print(form); 第二步:生成一个文本域第三步:接收参数 完整的代码如下:package cn.oracle.count;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.ServletException;import javax

17、.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ChatServlet extends HttpServlet SimpleDateFormat sdf = new SimpleDateFormat(HH:mm:ss); public void doGet(HttpServletRequest request, HttpServletResponse response) throws

18、ServletException, IOException /直接去请求doPost,只有用户第一次来这个页面时才会走这个doGet方法 doPost(request, response); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter();

19、/1:写出一个表单 String form = + 发言: + ; form+=; out.print(form); /接收参数 request.setCharacterEncoding(UTf-8); String msg = request.getParameter(msg); if(msg!=null & !msg.trim().equals()/是否为null /获取用户的ip String ip = request.getRemoteAddr(); ip = ip.substring(ip.lastIndexOf(.)+1); String time = sdf.format(new

20、 Date(); msg=ip+(+time+):+msg; /将参数放到application ,先获取以前有没有人说话 String msgs = (String) getServletContext().getAttribute(msgs); if(msgs=null) msgs=msg; else msgs+=rn+msg; /再放回到app getServletContext().setAttribute(msgs,msgs); String msgBox = +getServletContext().getAttribute(msgs) +; out.print(msgBox);

21、2:ServletContext获取项目的真实的目录 java.lang.StringgetRealPath(java.lang.Stringpath) Returns a String containing the real path for a given virtual path.获取项目的真实的路径。基本的示例: public class RealpathServlet extends HttpServlet public void doGet(HttpServletRequest request, HttpServletResponse response) throws Servle

22、tException, IOException response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter(); out.print(你的项目的真实的路径为:+getServletContext().getRealPath(/index.jsp); out.print(你的项目的真实的路径为:+getServletContext().getRealPath(/WEB-INF); 示例:显示某个目录下所有的图片:public class ImgServlet extends Http

23、Servlet public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter(); /1:获取这个目录 String path = getServletContext().getRealPath(/imgs); /2:构造成一个file对象 File file

24、= new File(path); /3:就可以列出这个目录下的所有文件名 String fs = file.list(); String contextPath = request.getContextPath(); for(String nm:fs) String a = ; String img=; img = a+img+; out.print(img); 3、获取在web.xml中配置的全局的初始化参数 示例代码:/先获取sc对象 ServletContext sc = getServletContext(); String name = sc.getInitParameter(na

25、me); String addr = sc.getInitParameter(addr); out.print(name+,+addr);2、 会话技术a) 在服务器上,有一个对象,可以记录某一个特定的用户的状态。记录用户状态的技术,会话技术。b) 因为网页HTTP协议,HTTP协议是无状态的协议 。2:会话跟踪技术 session Session是某个会话(某个浏览器)相关的。一个会话就是一个session.互不影响。 Session也是一个域对象。 - 只与一个用户相关。示例:说明session是对某个用户相关的数据:第一步:先开发Aserveltpublic class AServlet

26、 extends HttpServlet public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(text/html;charset=UTF-8); PrintWriter out = response.getWriter(); /设置对post有效的编码 request.setCharacterEncoding(UTF-8); /接收用户的参数 String name = request.getParameter(name); if(name!=null & name.trim().length()=3) out.print(你登录成功); /将用户信息名称放到三个域中去 request.setAttribute(name,name); /获取Session HttpSession session = request.getSession(); /将用户名称,也放到sesion session.setAttribute(name

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1