美文网首页
JavaEE-动态网页数据传输

JavaEE-动态网页数据传输

作者: yyq唯心不易 | 来源:发表于2017-07-03 20:44 被阅读0次

    1.  表单数据传送到后台

    String username = request.getParameter("username");

    通过该段代码,我们可以用key(name)取到value。所以搞清楚html文件标签的name和value很重要。

    2.  乱码问题

    2.1 doGet的解决办法

    response.setContentType("text/html;charset=UTF-8");

    String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

    2.2 doPost的解决办法

    request.setCharacterEncoding("UTF-8");

    response.setContentType("text/html;charset=UTF-8");

    3.  页面跳转

    设置跳转:response.setHeader("refresh", "3;url=login.html");//在3秒后跳转到login.html

    4.  3个作用域

    ps:能用作用域小的尽量用小的

    4.1 ServletContext

    全局都可以获取的到,所以基本用处不大。感觉用这个还不如用全局变量。

    4.2 Session

    作用在一次会话中(打开浏览器访问,应该就算一次会话)

    //把验证码发到登录处理器去验证

    HttpSession session = request.getSession();

    session.setAttribute("code", code);

    HttpSession session = request.getSession();

    //获取通过Session传来的验证码

    String code = (String) session.getAttribute("code");

    ps:在jsp中,不用定义就有session对象;

    4.3 Request

    一般来说是在一个网页界面中传递

    //发送

    request.setAttribute("code", code);

    //接受

    String code = (String) request.getAttribute("code");

    5.  重定向与转发

    重定向或是转发过后,页面都会实现跳转

    5.1 重定向

    response.sendRedirect("index.html");

    5.2 转发

    request.getRequestDispatcher("login.jsp").forward(request, response);

    关于request:

    关于session:

    6.  验证码

    6.1 步骤

    (1)获取画笔

    int width = 100;

    int height = 25;

    //设置图像                                                      宽      高                图像类型

    BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

    //通过图像获取画笔

    Graphics graphics = image.getGraphics();

    (2)填充区域

    //                        位置        宽高

    graphics.fillRect(0, 0, width, height);

    (3)写字

    //设置字体样式                      name    字体样式  大小

    graphics.setFont(new Font("宋体", Font.BOLD, 20));

    //画字

    graphics.drawString(string , x , y);

    //画线

    graphics.drawLine(x1, y1, x2, y2);

    (4)输出

    ImageIO.write(image, "jpg", response.getOutputStream());

    6.2  点击更换验证码

    <img alt="验证码" src="Verification" onclick="this.src = 'Verification?'+new Date().getTime()">

    相关文章

      网友评论

          本文标题:JavaEE-动态网页数据传输

          本文链接:https://www.haomeiwen.com/subject/dfmzcxtx.html