JavaWeb重点分析

更新时间:2024-01-08 16:57:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

JavaWeb

Java Web,是用Java技术来解决相关web互联网领域的技术总和。web包括:web服务器和web客户端两部分。Java在客户端的应用有java applet不过现在使用的很少,Java在服务器端的应用非常的丰富,比如Servlet,JSP和第三方框架等等。Java技术对Web领域的发展注入了强大的动力。

简单来说JavaWeb,由java程序开发的运行在Web服务器上的一个的java Sorket程序。

相关的web技术:

1、Web服务器分类

Web服务器可以简单的分类两大类:普通的web服务器,企业级web服务器。 其中普通服务器:resin、apache、tomcat、nginx、IIS(微软),重点来学习tomcat 企业级服务器:weblogic、websphere、jboss。

区别:所谓的企业级服务器就是在普通服务器的基础上拓展出了许多新的强大的功能。

搭建Tomcat服务器 Tomcat的目录层次结构

? ? ? ? ?

Bin:存放启动和关闭tomcat的脚本文件 Conf:存放tomcat服务器的各种配置文件 其中包含的配置文件有:

Server.xml:该文件用于配置和server相关的信息 Lib:存放tomcat服务器的支撑jar包 Logs:存放tomcat的日志文件

Temp:存放tomcat运行时产生的临时文件

Webapps:web应用所在的目录,即提供外接访问web资源的存放目录 ? Work:tomcat的工作目录

servlet

servlet是什么?

Servlet是在服务器上运行的类文件,由服务器进行调用。调用的过程是:由客服端发

出一个请求到服务器端,服务器端在接收到请求后就会进行处理,如果请求的资源是个Servlet那么服务器端就会根据相应的配置文件去找到这个Servlet并运行,然后将运行后的结果返回到客户端,完成服务器端的响应。

为什么会出现servlet?

比如需求:我们希望用户可以发帖子,可以回复….这样一些和用户可以交互的功能,用我们普通的java技术就完成不了了,sun公司就开发了servlet技术供程序员使用。

Servlet技术的特点:

1 他由服务器端调用和执行 2 他是用java语言编写的 3 他是按照servlet规范开发的 4 功能强大,可以完成几乎所有的网站功能 5 是我们学习jsp的基础

Servlet快速入门案例

1开发servlet有三种方式:实现servlet接口(讲解servlet的生命周期很方便),继承genericServlet,继承httpservlet

? 用servlet接口来实现我们的servlet,要求该servlet可以显示hello,world同时显示当前

的时间

package com.xy.servlet;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.Servlet;

import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;

import com.sun.org.apache.xml.internal.dtm.Axis;

public class MyFirstServlet implements Servlet{ 一次

//销毁我们的servlet,该函数也会被调用一次 @Override

public void destroy() { // TODO Auto-generated method stub }

//得到一个servletconfig对象 @Override

public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; }

//得到servlet的一些配置信息 @Override

public String getServletInfo() { // TODO Auto-generated method stub return null; }

//用于初始化我们的servlet,就是把servlet装载到内存中,该函数只会被调用@Override

public void init(ServletConfig arg0) throws ServletException { // TODO Auto-generated method stub }

//该函数是服务函数,我们的业务逻辑代码就是写在这里,该函数每次都会调@Override

}

public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println(\ PrintWriter pw=arg1.getWriter(); pw.println(\}

以下是我们的配置文件

index.jsp

MyFirstServlet

com.xy.servlet.MyFirstServlet

MyFirstServlet /MyFirstServlet

我们要注意的:在配置我们的配置文件的时候两个servlet-name要相同,servlet-class是我们的包名加上我们的java文件的名字,不要带上.java,在配置我们的url的时候一定不要忘记写“/”

servlet的生命周期是怎么样的或者说servlet究竟是怎么工作的?

通过实现servlet接口的方式来更好的讲解我们的servlet生命周期,我们的servlet的init方法是初始化我们的servlet,只会调用一次,将我们的servlet加载到内存中来,当我们再次访问的时候是不会被调用的。

? 继承genericServlet来实现我们的servlet package com.xy.testservlet;

import java.io.IOException;

import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;

public class TestServlet2 extends GenericServlet{ @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub res.setCharacterEncoding(\

res.getWriter().println(\我继承了GenerciServlet\ } }

当然了,我们还是需要配置我们的xml文件的,与之前我们的配置方式是一样的 ? 使用继承HttpServlet

1 在软件公司90%都是通过该方法来开发 2 举例说明:

package com.xy.testservlet;

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestHttpServlet extends HttpServlet{ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException{ resp.setCharacterEncoding(\ resp.getWriter().println(\我是dopost\ } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException{ resp.setCharacterEncoding(\

}

}

resp.getWriter().println(\我是doget\

MyHtml.html

用我们的myeclipse直接来开发我们的servlet

在我们的包里面直接new servlet即可

? Servlet的一些细节问题

1 一个已经注册的servlet可以被多次映射

MyServlet1

com.xy.testServlet.MyServlet1

MyServlet1 /MyServlet1

MyServlet1 /ltx

2 当我们映射一个servlet的时候可以有多层映射

MyServlet1

com.xy.testServlet.MyServlet1

MyServlet1 /MyServlet1

MyServlet1 /ltx

MyServlet1 /ltx/my.html

不要认为后缀名是.html不一定就是html有可能是假象 3 在对一个servlet进行映射的时候,我们可以使用通配符 有两种格式:

一:*.扩展名 比如*.do *.ss 二:/名字/*

注意:1 在匹配的时候,谁的匹配度高,就匹配谁 2 *.do 的优先级最低

4 servlet的单例问题

当servlet第一次被访问后,就被加载到内存,以后该实例对各个请求进行服务。即在使用时是单例的:

package com.xy.testServlet;

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 MyServlet1 extends HttpServlet { int i=0; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(\ this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { i++; System.out.println(\ response.getWriter().println(\ }}

简单的用户登录,在主界面中可以显示用户名和自己的密码

? 登录界面

package com.xy.testServlet;

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 LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ response.setCharacterEncoding(\ PrintWriter out = response.getWriter();

}

out.println(\用户登录界面\out.println(\

out.println(\用户名:
\out.println(\密码:
\out.println(\登录'/>
\out.println(\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

}

? 处理界面

package com.xy.testServlet;

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 LoginClServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { response.setContentType(\ response.setCharacterEncoding(\ PrintWriter out = response.getWriter(); String name=request.getParameter(\ String pwd=request.getParameter(\ System.out.println(\ if(name.equals(\ response.sendRedirect(\ }else{ response.sendRedirect(\这里是我们的项目名字加上我们要跳转的那个servlet的url

} } public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { this.doGet(request, response); } }

? 主界面,有一个超链接可以返回重新登录

package com.xy.testServlet;

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 Main extends HttpServlet { /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get.

* * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { response.setContentType(\ PrintWriter out = response.getWriter();

out.println(\out.println(\重新登录\ } public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { this.doGet(request, response); } }

可以使用我们的java技术来实现

可以使用httpServlet中的sendRedirec来实现 可以使用Session

HttpServletResponse

在我们的HttpServletResponse中有OutputStream和PrintWriter都可以实现向我们的浏览器回送数据不同:PrintWriter向客户端回送字符数据 OutputStream可以回送字符数据,可以回送字节数据 选择: 我们如果回送的是字符数据,就是用PrintWriter,效率高 如果回送的是字节数据就是用OutputStream PrintWriter out = response.getWriter();

out.println(\

OutputStream os=response.getOutputStream();

os.write(\ os.write(\重新登录\注意:不能同时使用这两个

Sendredirect:

package com.xy.testServlet;

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 LoginClServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ response.setCharacterEncoding(\ PrintWriter out = response.getWriter(); String name=request.getParameter(\ String pwd=request.getParameter(\ System.out.println(\ if(name.equals(\ response.sendRedirect(\ }else{ response.sendRedirect(\ } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }

小结:

response.sendRedirect(\优点:传送速度快

缺点:只能传送字符串,不能传送一个对象

注意:在?前面的是我们要跳转的那个页面的url,url和变量之间要有问号,当我们传递多

个参数的时候要用&(地址符)隔开

中文乱码

既然说到中文乱码,那么在什么情况下才会出现中文乱码呢? 发生中文乱码有三种情况: 1) Form表单的提交

? Post提交的时候,我们在LoginClServlet中写上request.setCharacterEncoding(\? Get

我们的工具类

public class MyToll { public static String getNewString (String s) { String newString =\ try { newString=new String(s.getBytes(\ } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return newString; } }

response.setContentType(\ PrintWriter out = response.getWriter(); // request.setCharacterEncoding(\ String name=request.getParameter(\ String pwd=request.getParameter(\ System.out.println(\ System.out.println(\2) 超链接 重新登录

3) SendRedirect发生乱码

Response.sendRedirect(“url?username=小王”);

response.setContentType(\ PrintWriter out = response.getWriter(); // request.setCharacterEncoding(\ String name=request.getParameter(\ //String name2=request.getParameter(\ //System.out.println(\ String pwd=request.getParameter(\ System.out.println(\ System.out.println(\ response.sendRedirect(\

httpServletRequest

HttpServletRequest对象代表客户端的请求,当客户端通过HTTp协议,访问服务器时,HTTP请求头中的所有信息都封装到这个对象中,开发人员通过这个对象的方法,可以获得这些客户的信息。

HttpServletRequest获得客户机信息

url:url=http://localhost:8080/TestRequesServlet/GetInfoServlet uri: uri=/TestRequesServlet/GetInfoServlet 我们的一个HTTP请求是这样的

http://localhost:8080/TestRequesServlet/GetInfoServlet?username=ltx&pwd=123 调用getQueryString方法得到的是username=ltx&pwd=123

我们用getRemoteAddr得到的ip是127.0.0.1并不是我自己机器的ip

String add=request.getRemoteAddr();

if(add.equals(\ response.sendRedirect(\ }

request.getHeader(name)与request.getHeaderNames();的用法,了解即可

Enumerationheads=request.getHeaderNames(); while(heads.hasMoreElements()){ String name=heads.nextElement(); System.out.println(name+\ }

如何获取用户提交的信息

提交的表单

package com.xueye.testservlet;

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 MyForm extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(\PrintWriter out = response.getWriter();

out.println(\

out.println(\用户名:
\ out.println(\密 码:
\ out.println(\性别:男\ out.println(\女'>女
\ out.println(\爱好:音乐
\

out.println(\体育'>体育
\ out.println(\旅游'>旅游
\ out.println(\所在城市:
\

out.println(\自我介绍:\

}

out.println(\我隐藏了' name='hidden'>\out.println(\提交'>\out.println(\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

}

处理表单的servlet

package com.xueye.testservlet;

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 FormCl extends HttpServlet {

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

response.setContentType(\request.setCharacterEncoding(\PrintWriter out = response.getWriter();

String name=request.getParameter(\String pwd=request.getParameter(\String sex=request.getParameter(\

String []hobby=request.getParameterValues(\if(hobby!=null){

for (int i = 0; i < hobby.length; i++) {

out.println(\我的爱好是:\}

}else{

out.println(\你没有爱好!\}

String city=request.getParameter(\

String jieshao=request.getParameter(\String hidden=request.getParameter(\out.println(\城市是:\out.println(\名字是:\out.println(\密码是:\out.println(\性别是:\out.println(\我的介绍是:\out.println(\隐藏的内容是:\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

实现请求转发

? 所谓请求转发是指:一个web资源收到客户端的请求后,通知服务器去调用另一个web

资源进行处理

? 我的第一个测试servlet

package com.xueye.TestRequestford;

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 TestFord extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ PrintWriter out = response.getWriter(); out.println(\用户登录\ out.println(\ out.println(\用户名:
\ out.println(\密 码:
\ out.println(\登录'>\ out.println(\ out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }

? 处理我们提交的数据的servlet

package com.xueye.TestRequestford;

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 ForWordCl extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); String name=request.getParameter(\ String pwd=request.getParameter(\ System.out.println(\ System.out.println(\ request.setAttribute(\ //request.getRequestDispatcher(\ response.sendRedirect(\ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

}

? 第三个servlet

package com.xueye.TestRequestford;

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 Ok extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)

}

}

throws ServletException, IOException {

response.setContentType(\request.setCharacterEncoding(\PrintWriter out = response.getWriter();

out.println(\我的名字是:\out.println(\我的密码是:\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

注意:使用转发不能访问web应用外的url,forward发生在web服务器,所以在上面的ForwardCl和Ok这两个servlet用的是同一个request和response ? 1 sendRedirect和forward的区别是什么?

答:1 sendRedirect叫做重定向,forward叫做转发 2 他们发生的位置不一样sendRedirect发生在浏览器而forward发生在服务器 3 用法不一样

request.getRequestDispatcher(\

// response.sendRedirect(\名/路径\4 能够去的url不一样 重定向可以访问任何url而转发只能访问当前的web资源

Servlet的会话技术

? 什么是会话

指的是用户开一个浏览器,访问一个网站,只要不关闭该浏览器,不管用户点击了多少个超链接,访问了多少资源,知道该用户关闭了该浏览器,整个过程我们称为一个会话 比如:打电话

? 会话过程中要解决的一些问题?

每个用户在使用浏览器与服务器进行会话的过程中,不可避免的会产生各自的一些数据,服务器要想办法为每个用户保存这些数据。

例如:多个用户点击超链接通过一个servlet各自购买商品,服务器应该想办法将每个用户购买的商品保存到一个地方,以便用户的结账

Cookie

为什么需要cookie?

思考:

1 我们登陆一个网站,想要显示你上次登陆的时间,我们该怎么完成? 2 如何显示用户曾经浏览的商品 3 还有在我们等了网站的时候,发现用户名和密码已经给你填进去了,不用我们重新

填写

解决之道:cookie技术

Cookie是客户端技术,服务器把每个用户的数据以cookie的形式写给用户各自的浏览器,当用户再次使用浏览器访问服务器中的web资源的时候,就会带着各自的数据去,这样web资源处理的就是用户自己的数据了。 ? 创建cookie

package com.xueye.testCookie;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class TestCookie extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

response.setContentType(\request.setCharacterEncoding(\PrintWriter out = response.getWriter(); //创建我们的cookie

Cookie ck=new Cookie(\

ck.setMaxAge(3600);//设置生命周期为一个小时3600秒 //把我们创建的cookie返回给我们的浏览器 response.addCookie(ck); out.println(\已经发送\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

}

? 读取cookie

package com.xueye.testCookie;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class ReadCookie1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); //读取kookie信息

//读取所以的cookie信息再选中我们想要的cookie Cookie []ck=request.getCookies(); for (int i = 0; i < ck.length; i++) { Cookie ck1=ck[i];

out.println(\的名字是:\的值是:

\

} System.out.println(ck.length); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }

Cookie可以用来做什么?

1可以保存上次登陆的信息

2保存用户名和密码,不用我们再次输入

3记录用户访问网站的一些喜好(比如:网页背景色,有无背景音乐) 4网站的个性化(定制网站的服务,内容)

Cookie小总结:

1 cookie是服务器创建的 2 cookie保存在浏览器端

3 cookie的生命周期可以使用setMaxAge(3600)来设置,如果不设置,那么只要关闭浏览器,cookie就消失了

4 cookie可以被多个浏览器共享

5 cookie可以看成是一个表格

6 如果我们创建的cookie重名会怎么办? 如果重名了就会替换掉原来的cookie package com.xueye.testCookie;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class CreateCookie2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); Cookie ck=new Cookie(\ Cookie ck2=new Cookie(\ ck.setMaxAge(3600); ck2.setMaxAge(3600); response.addCookie(ck); response.addCookie(ck2); out.println(\第二个cookie已经创建\ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

this.doGet(request, response);

7 一个web应用可以保存多个cookie

Cookie实际运用

1 显示用户上次登陆的时间

package com.xueye.testCookie.yunyong;

import java.io.IOException; import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class Servlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); //获取cookie //假设我们保存的上次登陆的时间是:cookie \ //这里我们要考虑第一次登陆的情况,如果是第一次登陆的话,我们提示欢迎第一次登陆 Cookie[]ck=request.getCookies(); boolean b=false; if(ck!=null){ for (Cookie cookie : ck) { //取出名字 String name=cookie.getName(); if(\ //显示

}

}

out.println(\你上次登陆的时间是:\ //更新时间 SimpleDateFormat sdf=new SimpleDateFormat(\ String date=sdf.format(new java.util.Date()); cookie.setValue(date); cookie.setMaxAge(7*24*3600); response.addCookie(cookie); b=true; break; } } }

if(b==false){ }

//没有找到我们的cookie

SimpleDateFormat sdf=new SimpleDateFormat(\String date=sdf.format(new java.util.Date()); out.println(\你是第一次登陆...\

Cookie ck1=new Cookie(\ck1.setMaxAge(7*24*3600); response.addCookie(ck1);

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

2保存用户登陆的用户名和密码,在用户下次登陆的时候不用再次填写了 1Login

package com.xueye.testCookie;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); //从cookie中选中keep cookie String id=\ Cookie []ck=request.getCookies(); if(ck!=null){ for (Cookie cookie : ck) { if(cookie.getName().equals(\ id=cookie.getValue(); } } }

out.println(\用户登录\

out.println(\

out.println(\用户名:
\ out.println(\type='checkbox' value='keep' name='iskeep'>是否保存登陆信息
\ out.println(\type='checkbox' value='nokeep' name='iskeep'>不保存登陆信息
\ out.println(\登录'>\ out.println(\ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

}

2LoginCl

package com.xueye.testCookie;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class LoginCl extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); String val=request.getParameter(\ System.out.println(\ String id=request.getParameter(\ String pwd=request.getParameter(\ if(val!=null&&val.equals(\ //保存用户的信息的id Cookie ck=new Cookie(\ ck.setMaxAge(7*2*24*3600); //回写给浏览器 response.addCookie(ck); request.getRequestDispatcher(\ }else if(val!=null&&val.equals(\ //先得到cookie Cookie []ck=request.getCookies(); for (Cookie cookie : ck) { cookie.setMaxAge(0);

}

response.addCookie(cookie);//一定要加上这句话

}

}

request.getRequestDispatcher(\}else{ request.getRequestDispatcher(\}

//out.println(request.getParameter(\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

3Ok

package com.xueye.testCookie;

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 Ok extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); out.println(\返回重新登陆\ }

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

与cookie相关的函数

1 2 3 4 5

public cookie(String name,String value) setValue与getValue

setMaxAge与getMaxAge getName方法

response.addCookie

Session

? 思考:

1) 大家都在网上买过商品,张三和李四他们购买的商品是不一样的,他们的购物车中

显示的商品也是不一样的,这是怎么实现的? 2) 不用的用户登录网站后,不管该用户浏览该网站的哪个页面,都可显示登录人的名

字,同样可以随时查看自己购物车中的商品

解决之道-session技术

Session是服务端技术,利用这个技术,服务器在运行时可以为每一个用户的浏览器创建一个共享的session对象,由于session为用户浏览器独享,所以用户在访问服务器的web资源时,可以把各种的数据放到各自的session中,当用户再次访问服务器中的其他web资源的时候,其他web资源再从用户各自的session中取出数据为用户服务

Session的工作原理

Session可以用来做什么?

1 2 3 4

网上商城的购物车 保存登陆用户的信息

将某些信息放到Session中供同一用户的各个页面使用 防止非法登陆到某个页面….

如何理解session

Session的基本使用

如果同一个用户浏览器,向session设置一个属性的时候,如果名字相同了,会出现什么情况?

答:会替换原来的值!

1 得到一个session HttpSession hs=request.getSession()这里的括号中可以写true或者false

或者不写,写true的时候和不写是一样的,表示没有Session就会给我们创建一个Session,写成false的话,表示没有Session的话就算了,不自动创建 2 向session中添加属性 hs.setAttribute(“”,obj);

3 从session中得到某个属性String name=hs.getAttribute(); 4 从session中删除某个属性hs.removeAttribute(String name);

5

Session小结

1 2 3

session是存放在服务器的内存中 一个用户的浏览器独享一个session

Session中的属性的默认的生命周期是30分钟

我们也可以在我们的web.xml文件中进行配置 20 这里的20表示的是分钟 Session可以存放多个属性 Session可以存放对象

4 5 6

Session实际应用

防止非法用户登录到某个页面 Login

package com.xueyx.testSession.zuzhi;

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 Login extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); out.println(\用户登录\ out.println(\ out.println(\用户名:
\ out.println(\密 码:
\ out.println(\登录'>\ out.println(\ String info=(String) request.getAttribute(\ if(info!=null){ out.println(info); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

this.doGet(request, response);

LoginCl

package com.xueyx.testSession.zuzhi;

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; import javax.servlet.http.HttpSession;

public class LoginCl extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ PrintWriter out = response.getWriter(); request.setCharacterEncoding(\ String id=request.getParameter(\ String pwd=request.getParameter(\ System.out.println(id+pwd); if(\ HttpSession hs=request.getSession(); hs.setAttribute(\ hs.setAttribute(\ System.out.println(\ request.getRequestDispatcher(\ }else{ request.getRequestDispatcher(\ } }

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

Ok

package com.xueyx.testSession.zuzhi;

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; import javax.servlet.http.HttpSession;

public class Ok extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); HttpSession hs=request.getSession(); String id=(String) hs.getAttribute(\ String pwd=(String) hs.getAttribute(\ out.println(\ if(\ out.println(\欢迎你登陆了\ }else{ request.setAttribute(\请输入信息后登陆\ request.getRequestDispatcher(\ } }

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

对session销毁时间的讨论

要求:关掉浏览器后,再开浏览器,上次购买的商品还在,这就涉及到session的销毁时间 我们session的生命周期是30分钟的话,那么我们关闭浏览器的时候,session不会自动销毁,而会到30分钟过后被服务器销毁

Servlet1

package com.xueye.testsession2;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;

public class Servlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); HttpSession h1=request.getSession(); h1.setAttribute(\小张\ System.out.println(\ //在我们成功创建了Session后会自动创建一个session id,这是我们将这个id放入

到我们的cookie中 //但是一定要按照规范命名 Cookie ck=new Cookie(\ ck.setPath(\ ck.setMaxAge(1800); response.addCookie(ck); out.println(\创建成功并放入小张\ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }

Servlet2

package com.xueye.testsession2;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;

public class Servlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); HttpSession hs=request.getSession(); String name=(String) hs.getAttribute(\

}

}

System.out.println(\out.println(\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }

this.doGet(request, response);

ServletContext

问题:

1 在我们访问某个网站的时候,往往都会看到网站的首页面显示的你是第几位浏览者(网

站计数器)这是怎么实现的?

2 我们在访问某个bbs网站的时候,往往会显示有多少人在线,这是怎么实现的

解决之道ServletContext

? 什么是servletcontext:

要理解servletcontext就必须和cookie,session做一个对比,如图

你可以把ServletContext看成是一个公用的空间,可以被所有的客户访问

Web容器在启动的时候,他会为每个web应用程序创建一个对应的servletcontext对象,它代表当前的web应用 ? ServletContext

1 是在服务器端

2 是被所有的客户端共享

3 ServletContext是当web应用启动的时候自动创建

4 ServletContext对象当web应用关闭/tomcat关闭,web应用reload时会造成

ServletContext销毁

简单代码演示

? Servlet1

package com.xueye.testServletcontext;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class Servlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ PrintWriter out = response.getWriter(); request.setCharacterEncoding(\ //通过this获取对servletcontext对象 ServletContext servletcontext=this.getServletContext();

//通过Servletconfig获取 //ServletContext servletcontext2=this.getServletConfig().getServletContext(); servletcontext.setAttribute(\李四\ out.println(\中放入了李四\}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

}

? Servlet2

package com.xueye.testServletcontext;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class Servlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ PrintWriter out = response.getWriter(); request.setCharacterEncoding(\ //取出Servletcontext的某个属性 ServletContext servletcontext=this.getServletContext(); String name=(String)servletcontext.getAttribute(\ System.out.println(name); out.println(\得到的名字是=\ servletcontext.removeAttribute(\

}

用法小结

获取Servletcontext:

1 ServletContext servletcontext=this.getServletContext();

2 ServletContext servletcontext2=this.getServletConfig().getServletContext(); 添加属性: Servletcontext.setarrtibute(String ,obj) 取出属性 Servletcontext.getArrtibute(); 删除属性 ServletContext. removeAttribute(\

out.println(\删除了属性\

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

Servletcontext的应用

1 2

多个servlet可以通过ServletContext对象实现数据的共享 获取web应用的初始化参数

对于在servelt里面的init-param只能用this.getServletConfig().getInitParameter(\来获得里面的值;对于是context-param的要用this.getServletContext().getInitParameter(\来获得

3 实现servlet的转发

this.getServletContext().getRequestDispatcher(\

总之:如果涉及到不同的用户共享数据,而数据量又不大,同时又不希望写入到数据库中,我们可以考虑使用servletcontext

网站计数器:

在网站建设中,经常会统计某个网页被浏览的次数,我们把这个功能叫做计数器:那么这个网站计数器是怎么实现的?怎么才算是一个有效的点击?

1 只要访问过该网页,计算是一次点击,刷新一次也算的,当然这是最简单的

2 不同的ip访问该网页,算一次有效的点击,如果是同一个ip在一定的时间内,不管你

浏览了多少次都算一次

3 用户退出该网站时,在次访问也算一次

Login

package com.xueye.testServletcontext.wangzhanjishu;

import java.io.IOException; import java.io.PrintWrite

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\ request.setCharacterEncoding(\ PrintWriter out = response.getWriter(); }

}

out.println(\用户登录\

out.println(\out.println(\用户名:
\out.println(\密 码:
\out.println(\登录'>\out.println(\

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

LoginCl

package com.xueye.testServletcontext.wangzhanjishu;

import java.io.IOException; import java.io.PrintWriter;

import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class LoginCl extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

response.setContentType(\PrintWriter out = response.getWriter();

String name=request.getParameter(\String pwd=request.getParameter(\if(\

//1先取出来 2然后加一3再放回去 ServletContext sc=this.getServletContext(); String nums=(String)sc.getAttribute(\ if(nums==null){ sc.setAttribute(\ request.getRequestDispatcher(\ }else{ int a=Integer.parseInt(nums)+1; sc.setAttribute(\ request.getRequestDispatcher(\ } }else{ request.getRequestDispatcher(\ } }

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

Manager

package com.xueye.testServletcontext.wangzhanjishu;

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 Manage extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)

}

throws ServletException, IOException {

response.setContentType(\PrintWriter out = response.getWriter();

String nums=(String) this.getServletContext().getAttribute(\

out.println(\访问的次数是\}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

注意,当我们刷新的时候计数器会变,那门我们怎么防止这个问题 response.sendRedirect(\

本文来源:https://www.bwwdw.com/article/tlyo.html

Top