• request对象常用API 获取请求参数的值 request应用 MVC设计模式


    1 request对象常用API
      1)表示web浏览器向web服务端的请求
      2)url表示访问web应用的完整路径:http://localhost:8080/day06/Demo1
        uri表示访问web应用的资源路径:/day06/Demo1
        queryString表示?开头的参数;name=jack
        pathInfo:路径之外的信息,如果没有,返回null

    package cn.itcast.web.request;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /*
     * http://localhost:8080/day06/Demo1?name=jack回车
     */
    
    public class Demo1 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            /*
            String url = request.getRequestURL().toString();
            String uri = request.getRequestURI();
            String query = request.getQueryString();
            String pathInfo = request.getPathInfo();
            System.out.println("url=" + url);
            System.out.println("uri=" + uri);
            System.out.println("query=" + query);
            System.out.println("pathInfo=" + pathInfo);
            */
            
            String clientIP = request.getRemoteAddr();
            String clientHOST = request.getRemoteHost();
            int clientPORT = request.getRemotePort();
            
            String serverIP = request.getLocalAddr();
            String serverHOST = request.getLocalName();
            int serverPORT = request.getLocalPort();
            
            System.out.println("客户端:"+clientIP+":"+clientHOST+":"+clientPORT);
            System.out.println("服务端:"+serverIP+":"+serverHOST+":"+serverPORT);
        }
    }

    获得客户机信息

        •getRequestURL方法返回客户端发出请求时的完整URL。

        •getRequestURI方法返回请求行中的资源名部分。

        •getQueryString 方法返回请求行中的参数部分。

        •getPathInfo方法返回请求URL中的额外路径信息。额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容,它以“/”开头。

        •getRemoteAddr方法返回发出请求的客户机的IP地址。

        •getRemoteHost方法返回发出请求的客户机的完整主机名。

        •getRemotePort方法返回客户机所使用的网络端口号。

        •getLocalAddr方法返回WEB服务器的IP地址。

        •getLocalName方法返回WEB服务器的主机名。

    获得客户机请求头

        •getHeader(string name)方法:String

        •getHeaders(String name)方法:Enumeration

        •getHeaderNames()方法

    获得客户机请求参数(客户端提交的数据)

        •getParameter(String)方法

        •getParameterValues(String name)方法

        •getParameterNames方法+BeanUtils框架
    取得请求头相关的信息
         *a)request.getHeader(String):String
          b)request.getHeaders(String):Enumeration
          c)getHeaderNames():Enumeration
    *2 获取请求参数的值
       2)取得请求体相关的信息
          a)request.getParameter(String):String
          b)request.getParameterValues(String):String[]
            注意:在GET请求方式下,如果参数不一致,返回null,可能出现空指针异常
          c)BeanUtils框架能够自动将String参数设置到JavaBean对象中
            1)无需理会参数的类型,个数,但是参数名一定要和JavaBean中的字段一致
            2)String->8种基本类型自动转换,非8种基本类型可以注册转换器
            3)String或String[]类型的参数自动感知装配

    package cn.itcast.web.request;
    
    import java.io.IOException;
    import java.util.Enumeration;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Demo1 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            //一次性取得所有的请求头信息
            Enumeration<String> enums = request.getHeaderNames();
            while(enums.hasMoreElements()){
                //key表示请求头
                String key = enums.nextElement();
                Enumeration<String> enums2 = request.getHeaders(key);
                while(enums2.hasMoreElements()){
                    String value = enums2.nextElement();
                    response.getWriter().write(key+"<->"+value+"<br/>");
                }
            }
            
            /*
            String acceptLanguage = request.getHeader("Accept-Language");
            String userAgent = request.getHeader("User-Agent");
            String host = request.getHeader("Host");
            response.getWriter().write("Accept-Language=" + acceptLanguage + "<br/>");
            response.getWriter().write("User-Agent=" + userAgent + "<br/>");
            response.getWriter().write("Host=" + host + "<br/>");
            */
        }
    }
    package cn.itcast.web.request;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.lang.reflect.InvocationTargetException;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.beanutils.BeanUtils;
    
    import cn.itcast.web.domain.User;
    
    /*
     * http://localhost:8080/day07/Demo2?username=jack&age=30&salary=5000&likes=dance&likes=sing回车
     */
    
    public class Demo2 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            
            //分别取得客户端提交的信息
            //String strUsername = request.getParameter("username");
            //String strAge = request.getParameter("age");
            //String strSalary = request.getParameter("salary");
            //String[] strLikes = request.getParameterValues("likes");
            
            BeanUtils bu = new BeanUtils();
            User user = new User();
            Enumeration<String> enums = request.getParameterNames();
            while(enums.hasMoreElements()){
                String key = enums.nextElement();
                String[] values = request.getParameterValues(key);
                try {
                    bu.setProperty(user,key,values);
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            pw.write("用户名:" + user.getUsername() + "<br/>");
            pw.write("年龄:" + user.getAge() + "<br/>");
            pw.write("期望薪水:" + user.getSalary() + "<br/>");
            pw.write("爱好个数:" + user.getLikes().length + "<br/>");
            pw.flush();
            pw.close();
        }
    }

    *3 request应用
       1)防盗链
         判段某个页面来自于何方,即referer请求头

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>index.html</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      </head>
      <body>
          <a href="/day07/DownServlet">下载</a>
      </body>
    </html>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>index.html</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      </head>
      <body>
          这是广告页面
      </body>
    </html>
    package cn.itcast.web.request;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Demo4 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            
            //取得浏览器来自于何方
            String referer = request.getHeader("referer");
            
            //判段 
            if(referer!=null && referer.equals("http://localhost:8080/day07/index.html")){
                //转发到download.html页面
                this
                    .getServletContext()
                    .getRequestDispatcher("/download.html")
                    .forward(request,response);
            }else{
                //转发到ad.html页面
                this
                .getServletContext()
                .getRequestDispatcher("/ad.html")
                .forward(request,response);
            }
        }
    }

         2)收集表单数据
         a)项目中,用户请求的参数,最好做null或空串的二次判段

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>index.html</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      </head>
      <body>
          <form action="/day07/Demo5" method="post">
              <table border="1" align="center">
                  <caption>用户注册</caption>
                  <tr>
                      <th>用户名</th>
                      <td><input type="text" name="username"/></td>
                  </tr>
                  <tr>
                      <th>密码</th>
                      <td><input type="password" name="password"/></td>
                  </tr>
                  <tr>
                      <th>性别</th>
                      <td>
                          <input checked type="radio" name="gender" value="male"/><input type="radio" name="gender" value="female"/></td>
                  </tr>
                  <tr>
                      <th>爱好</th>
                      <td>
                          <input type="checkbox" name="likes" value="sing"/>唱歌
                          <input type="checkbox" name="likes" value="dance"/>跳舞
                          <input type="checkbox" name="likes" value="play"/>打球
                          <input type="checkbox" name="likes" value="net"/>上网
                      </td>
                  </tr>
                  <tr>
                      <th>上传文件</th>
                      <td><input type="file" name="upfile"/></td>
                  </tr>
                  <tr>
                      <th>城市</th>
                      <td>
                          <select name="city">
                              <option value="bj">北京</option>
                              <option value="sh">上海</option>
                              <option value="gz" selected>广州</option>
                              <option value="cq">重庆</option>
                          </select>
                      </td>
                  </tr>
                  <tr>
                      <th>留言</th>
                      <td>
                          <textarea name="message" rows="5" cols="20"></textarea>
                      </td>
                  </tr>
                  <tr>    
                      <td>
                          <input type="hidden" name="id" value="20111008"/>
                      </td>    
                  </tr>                
                  <tr>
                      <td colspan="2" align="center">
                          <input type="submit" value="提交"/>
                              &nbsp;&nbsp;&nbsp;&nbsp;
                          <input type="reset" value="重填"/>
                      </td>
                  </tr>    
              </table>
          </form>
      </body>
    </html>
    package cn.itcast.web.request;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Demo5 extends HttpServlet {
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            
            //将[请求体]的中文以UTF-8方式重新编码
            request.setCharacterEncoding("UTF-8");
    
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String gender = request.getParameter("gender");
            String[] likes = request.getParameterValues("likes");
            String upfile = request.getParameter("upfile");
            String city = request.getParameter("city");
            String message = request.getParameter("message");
            String id = request.getParameter("id");
            
            
            System.out.println("username="+username);
            System.out.println("password="+password);
            System.out.println("gender="+gender);
            System.out.println("likes="+likes.length);
            System.out.println("upfile="+upfile);
            System.out.println("city="+city);
            System.out.println("message="+message);
            System.out.println("id="+id);
        }
    }

         3)中文乱码
         a)POST方式下乱码产生的原因:浏览器发送的编码方式与Request对象的编码方式不一致
           解决方案:request.setCharacterEncoding("UTF-8),该方法只能解析请求体中的乱码方式,除此之外,无力解决
         b)如果用户是post方式提交,在转发情况下,依然是post请求,但重定向情况下,是get请求
         c)如果用户是get方式提交,在转发和重定向情况下,都是get请求
         d)405:本身请求是get请求,但未在servlet内提供对应的doGet()方法, post也是类似  

    针对请求头编码:

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ page import="java.net.*" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'get.jsp' starting page</title>
      </head>
      <body>
          <%-- 对中文编码 --%>
        <a href="/day07/Demo7?username=<%=URLEncoder.encode("小王","UTF-8")%>">
            get方式传中文
        </a>    
      </body>
    </html>
    package cn.itcast.web.request;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Demo7 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            
            String username = request.getParameter("username");
            
            //中文解码
            byte[] buf = username.getBytes("ISO8859-1");
            username = new String(buf,"UTF-8");
            
            if(username!=null || username.trim().length()>0){
                response.setContentType("text/html;charset=UTF-8");
                response.getWriter().write("用户名" + username);
            }
        }
    }

    //练习获取ip地址和所属地

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>用户登录</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      </head>
      <body>
          <form action="/day07/LoginServlet" method="post">
              <table border="1" align="center">
                  <caption>用户登录</caption>
                  <tr>
                      <th>用户名</th>
                      <td><input type="text" name="username"/></td>
                  </tr>
                  <tr>
                      <td colspan="2" align="center">
                          <input type="submit" value="提交"/>
                              &nbsp;&nbsp;&nbsp;&nbsp;
                          <input type="reset" value="重填"/>
                      </td>
                  </tr>    
              </table>
          </form>
      </body>
    </html>
    package cn.itcast.web.request;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import cn.itcast.web.domain.IpCheckBean;
    
    public class LoginServlet extends HttpServlet {
        //key为IP,value为归属地
        private Map<String,String> map = new HashMap<String,String>();
        public void init() throws ServletException {
            //在用户第一次访问前加载IP和归属地,不存在多线程安全问题
            map.put("192.168.2.222","北京");
            map.put("192.168.10.122","上海");
            map.put("192.168.10.50","深圳");
            map.put("192.168.10.33","佛山");
            map.put("192.168.10.44","珠海");
            map.put("127.0.0.1","广州");
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            //IP
            String ip = request.getRemoteAddr();
            IpCheckBean ipCheckBean = new IpCheckBean();
            //归属地
            String address = ipCheckBean.check(ip,map);
            //用户名
            String username = request.getParameter("username");
            //将信息绑定到ServletContext对象中
            ServletContext context = this.getServletContext();
            context.setAttribute("username",username);
            context.setAttribute("ip",ip);
            context.setAttribute("address",address);
            /*转发到ShowServlet
            context
                .getRequestDispatcher("/ShowServlet")
                .forward(request,response);
            */
            //重定向到ShowServlet
            response.sendRedirect("/day07/ShowServlet");
        }
    }
    package cn.itcast.web.request;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletExceppackage cn.itcast.web.request;
    
    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 ShowServlet extends HttpServlet {
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            pw.write("欢迎:"+context.getAttribute("username")+"光临<br/>");
            pw.write("你的IP是:"+context.getAttribute("ip")+"<br/>");
            pw.write("归属地:"+context.getAttribute("address")+"<br/>");
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            this.doPost(request,response);
        }
    }

    lrequest对象实现请求转发:请求转发指一个web资源收到客户端请求后,通知服务器去调用另外一个web资源进行处理。请求转发的应用场景:MVC设计模式

    *4 再谈MVC设计模式
       1)如果严格按照MVC思想的话,浏览器首页访问的是控制器,然后再由控制器转发到视图
       2)位于WEB-INF/目录下的资源,客户端无法直接访问,只能通过服务端转发进入
       3)request对象也可用于资源的转发
        request
            .getRequestDispatcher("/WEB-INF/success.html")
            .forward(request,response);
       4)通过正则表达式验证中文:[u4E00-uFA29]+
       5)对于重定向而言,前后的二个Request域对象不同,如果取值不到,返回"null",不会报错   
     

       6)request对象提供了一个getRequestDispatcher方法,该方法返回一个RequestDispatcher对象,调用这个对象的forward方法可以实现请求转发。

       7)request对象同时也是一个域对象,开发人员通过request对象在实现转发时,把数据通过request对象带给其它web资源处理。

        •setAttribute方法

        •getAttribute方法

        •removeAttribute方法

        •getAttributeNames方法

    package cn.itcast.web.request;
    
    import java.io.IOException;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import cn.itcast.web.domain.ModelBean;
    
    public class Demo8 extends HttpServlet {
        //NO1
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            //转发到view.html页面
            RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/view.html");
            rd.forward(request,response);
        }
        //NO2
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            String username = request.getParameter("username");
            ModelBean mb = new ModelBean();
            boolean flag = mb.validate(username);
            if(flag){
                //将用户名绑定到Request域对象中
                request.setAttribute("USERNAME",username);
                
                /*转发到ListServlet.java
                request
                    .getRequestDispatcher("/ListServlet")
                    .forward(request,response);
                */    
                
                //重定向到ListServlet.java
                response.sendRedirect("/day07/ListServlet");
                
            }else{
                request
                .getRequestDispatcher("/WEB-INF/fail.html")
                .forward(request,response);
            }
        }
    }
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>用户登录</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      </head>
      <body>
          <form action="/day07/Demo8" method="post">
              <table border="1" align="center">
                  <caption>用户登录</caption>
                  <tr>
                      <th>用户名</th>
                      <td><input type="text" name="username"/></td>
                  </tr>
                  <tr>
                      <td colspan="2" align="center">
                          <input type="submit" value="提交"/>
                              &nbsp;&nbsp;&nbsp;&nbsp;
                          <input type="reset" value="重填"/>
                      </td>
                  </tr>    
              </table>
          </form>
      </body>
    </html>
    package cn.itcast.web.request;
    
    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 ListServlet extends HttpServlet {
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            String username = (String) request.getAttribute("USERNAME");
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("欢迎"+username+"光临");
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            String username = (String) request.getAttribute("USERNAME");
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("欢迎"+username+"光临");        
        }
    }
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>用户登录</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      </head>
      <body>
        登录失败  
      </body>
    </html>
    package cn.itcast.web.domain;
    
    public class ModelBean {
        public boolean validate(String username){
            boolean flag = false;
            if(username!=null && username.matches("[u4E00-uFA29]+")){
                flag = true;
            }
            return flag;
        }
    }


    5 request的细节
       1)转发是通过RequestDispatcher对象的forward(request,response)方法完成的
       2)forward()方法之前的输出,如果是输出到缓冲区,再转发到其他Servlet,那么原缓冲区的数据忽略不计
       3)forward()方法之前的输出,如果真正输出到浏览器,再转发到其他Servlet,再转发到其他Servlet代码忽略不计
       4)forward()方法之前的输出,如果是对响应头的设置,会保留到转发的其他Servlet中

    请求转发的细节

        •forward方法用于将请求转发到RequestDispatcher对象封装的资源。

        •如果在调用forward方法之前,在Servlet程序中写入的部分内容已经被真正地传送到了客户端,forward方法将抛出IllegalStateException异常。

        •如果在调用forward方法之前向Servlet引擎的缓冲区(response)中写入了内容,只要写入到缓冲区中的内容还没有被真正输出到客户端,forward方法就可以被正常执

        •行,原来写入到输出缓冲区中的内容将被清空,但是,已写入到HttpServletResponse对象中的响应头字段信息保持有效

    package cn.itcast.web.request;
    
    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 Demo91 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            //response.setContentType("text/html;charset=UTF-8");
            //PrintWriter pw = response.getWriter();
            //pw.write("Demo91");
            //pw.flush();
            //pw.close();
            //request.getRequestDispatcher("/Demo92").forward(request,response);
            response.sendRedirect("/day06/success.html");
        }
    }
    package cn.itcast.web.request;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Demo92 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            response.getWriter().write("哈哈");
        }
    }

    *6 重定向和转发区别

        •一个web资源收到客户端请求后,通知服务器去调用另外一个web资源进行处理,称之为请求转发/307。

        •一个web资源收到客户端请求后,通知浏览器去访问另外一个web资源进行处理,称之为请求重定向/302。
       1)转发只能相对于当前web应用而言,不能转发到其它web应用
         重定向可以在当前web应用之外的其它web应用重写向资源
       2)转发/表示当前web应用
         重定向/表示web服务器的根目录,即webapps目录
       3)转发时,浏览器地址栏不变
         重定向时,浏览器地址栏改变
       4)转发是服务端的内部行为,浏览器不知
         重定向是浏览器和服务端的共同行为,浏览器必知
       5)转发时二个Servlet共享request域对象
         重定向时二个Servlet独享request域对象
       6)request具有二义性:
        a)请求对象
        b)域对   
    7 include包含
       1)需要使用Servlet包含相同的页面输出时,类似于函数调用

        •RequestDispatcher.include方法用于将RequestDispatcher对象封装的资源内容作为当前响应内容的一部分包含进来,从而实现可编程的服务器端包含功能。

        •被包含的Servlet程序不能改变响应消息的状态码和响应头,如果它里面存在这样的语句,这些语句的执行结果将被忽略

    package cn.itcast.web.request;
    
    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 Demo10 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            
            //取得客户端请求的类型
            String method = request.getMethod();
            System.out.println("method="+method);
            
            
            //包含head.html(类似于函数调用)
            request
                .getRequestDispatcher("/common/head.html")
                .include(request,response);
            response.getOutputStream().write("<hr/>".getBytes());
            
            //动态生成
            response.getOutputStream().write("login.html".getBytes());
            response.getOutputStream().write("<hr/>".getBytes());
            
            //包含foot.html
            request
                .getRequestDispatcher("/common/foot.html")
                .include(request,response);
            response.getOutputStream().write("<hr/>".getBytes());
        }
    }

    会话
       1)用户输出URL地址,有效访问某个网站,在该网站上的一系列有效操作,随后关闭浏览器的整个过程,叫一次会话
       2)会话主要解决服务端如何保存每个客户端对应的私有信息。
       3)会话主要有二种:
        a)Cookie技术[客户端技术]
        b)Session技术 
      
    Cookie

    image_thumb4_thumb
       1)Cookie是客户端的技术
       2)每次发送请求时,客户端都会带上各自已的不同Cookie到服务端,服务端一解析Cookie,就知道是哪个客户端发送过来的信息
       3)一个Cookie只能存储一种类型的信息
       4)更新某个名的Cookie,即向浏览器写一个相同名的Cookie
       5)Cookie一定要设置一个有效时间,如果不设置的话,默认该请求访问结束后,该Cookie自动销毁,用专业名词来讲,
         Cookie的默认有效期时一个有效会话结束

    package cn.itcast.web.cookie;
    
    import java.io.IOException;
    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 Demo1 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            /*
            //服务端产生Cookie
            Cookie cookie = new Cookie("username","haha");
            //设置Cookie存活时间
            cookie.setMaxAge(1*24*60*60);
            //服务端将Cookie写到客户端暂存
            response.addCookie(cookie);
            */
            
            //取得客户端所有的Cookie
            Cookie[] cookies = request.getCookies();
            Cookie usernameCookie = null;
            if(cookies!=null){
                for(Cookie c : cookies){
                    if(c.getName().equals("username")){
                        usernameCookie = c;
                        break;
                    }
                }
                //如果找到了Cookie
                if(usernameCookie!=null){
                    System.out.println("Cookie的名字:" + usernameCookie.getName());
                    System.out.println("Cookie的值:" + usernameCookie.getValue());
                    System.out.println("Cookie的生命:" + usernameCookie.getMaxAge());
                    System.out.println("Cookie的路径:" + usernameCookie.getPath());
                }
            }
        }
    }
    package cn.itcast.web.cookie;
    
    import java.io.IOException;
    import java.util.Date;
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /*
     * http://localhost:8080/day07/Demo2?name=jack回车
     */
    public class Demo2 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            String name = request.getParameter("name");
            if(name==null){
                name="default";
            }
            
            //取得客户端的所有Cookie的值
            Cookie[] cookies = request.getCookies();
            if(cookies==null){
                Cookie cookie = new Cookie("name_"+name,System.currentTimeMillis()+"");
                cookie.setMaxAge(1*24*60*60);
                response.addCookie(cookie);
                response.setContentType("text/html;charset=UTF-8");
                response.getWriter().write("欢迎"+name+"首次光临,访问时间:" + new Date().toLocaleString());
            }else{
                //查找名为name的Cookie
                Cookie nameCookie = null;
                for(Cookie c : cookies){
                    if(c.getName().equals("name_"+name)){
                        nameCookie = c;
                        break;
                    }
                }
                //找到了
                if(nameCookie!=null){
                    String strTime = nameCookie.getValue();
                    response.setContentType("text/html;charset=UTF-8");
                    long lastTime = Long.parseLong(strTime);
                    response.getWriter().write("欢迎"+name+"再次光临,你上次访问时间:" + new Date(lastTime).toLocaleString());
                    //更新时间
                    Cookie cookie = new Cookie("name_"+name,System.currentTimeMillis()+"");
                    cookie.setMaxAge(1*24*60*60);
                    response.addCookie(cookie);
                }
            }
        }
    }

    *0 带用验证码功能的用户登录
      1)验证码使用一个CheckCodeSerlvet产生,通过<img src=""/>发送请求,将验证码绑定到HttpSession中
      2)产生的验证码一定要在输出到浏览器之前绑定到HttpSession中
      3)设置验证码禁止缓存要在输出到浏览器之前 
      4)所有响应头的设置放置在最先

    package cn.itcast.web.session;
    
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.Random;
    import javax.imageio.ImageIO;
    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 CheckCodeServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            response.setHeader("expires","-1");
            response.setHeader("cache-control","no-cache");
            response.setHeader("pragma","no-cache");
            String token = Token.getNum();
            //将验证码绑定到域对象
            HttpSession session = request.getSession();
            session.setAttribute("checkCodeServer",token);
            BufferedImage image = new BufferedImage(50,25,BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();
            g.drawString(token,15,15);
            //加干扰线
            for(int i=0;i<10;i++){
                Random r = new Random();
                int x1 = r.nextInt(50);
                int y1 = r.nextInt(25);
                g.drawLine(x1, y1, x1+4,y1+4);
            }
            ImageIO.write(image,"JPG",response.getOutputStream());
        }
    }
    class Token{
        public static String getNum(){
            Random r = new Random();
            int num = r.nextInt(10000);
            return num + "";
        }
    }
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>login.html</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      </head>
      <body>
          <form action="/day09/LoginServlet" method="post">
              <table border="1" align="center">
                  <tr>
                      <th>用户名</th>
                      <td><input type="text" name="username"/></td>
                  </tr>
                  <tr>
                      <th>密码</th>
                      <td><input type="password" name="password"/></td>
                  </tr>
                  <tr>
                      <th>验证码</th>
                      <td>
                          <input type="text" name="checkCodeClient"/>
                      </td>
                      <td>
                          <img src="/day09/CheckCodeServlet"/>    
                      </td>
                  </tr>
                  <tr>
                      <td colspan="2" align="center">
                          <input type="submit" value="提交"/>
                      </td>
                  </tr>
              </table>
          </form>
      </body>
    </html>
    package cn.itcast.web.session;
    
    import java.io.IOException;
    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 doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            //收集客户端填入的验证码
            String checkCodeClient = request.getParameter("checkCodeClient");
            //取得服务端填入的验证码
            String checkCodeServer = (String) request.getSession().getAttribute("checkCodeServer");
            //判段
            response.setContentType("text/html;charset=UTF-8");
            if(checkCodeClient!=null && checkCodeClient.trim().length()>0 && checkCodeServer!=null && checkCodeServer.equals(checkCodeClient)){
                response.getWriter().write("验证码正确");
            }else{
                response.getWriter().write("验证码出错");
            }
        }
    }
  • 相关阅读:
    @PostConstruct和 @PreDestroy注解
    【JQuery】,ajax请求中,url出现[Object Object]
    筛法求素数
    母牛的故事
    将一个数拆分
    计算两个日期差
    用二分查找——查找比目标元素略大的索引
    反向输出字符串
    bootstrap table合并单元格(该版本是简单的应用)
    获取访问者IP
  • 原文地址:https://www.cnblogs.com/sunhan/p/3542141.html
Copyright © 2020-2023  润新知