zoukankan      html  css  js  c++  java
  • Servlet系列教材 (六)- 基础

    步骤1:获取中文的参数
    步骤2:返回中文的响应

    示例 1 : 获取中文的参数

    为了成功获取中文参数,需要做如下操作
    1. login.html中加上

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


    这句话的目的是告诉浏览器,等下发消息给服务器的时候,使用UTF-8编码

    2. login.html
    form的method修改为post

    3. 在servlet进行解码和编码

    byte[] bytes=  name.getBytes("ISO-8859-1");

    name = new String(bytes,"UTF-8");


    先根据ISO-8859-1解码,然后用UTF-8编码
    这样就可以得到正确的中文参数了

    这样需要对每一个提交的数据都进行编码和解码处理,如果觉得麻烦,也可以使用一句话代替:

    request.setCharacterEncoding("UTF-8"); 


    并且把这句话放在request.getParameter()之前

    以上是使用UTF-8的方式获取中文呢。 也可以使用GBK。把所有的UTF-8替换为GBK即可。 GB2312同理。

    获取中文的参数

    <!DOCTYPE html>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <form action="login" method="post">

        账号 : <input type="text" name="name"> <br> 密码: <input

            type="password" name="password"> <br> <input

            type="submit" value="登录">

    </form>

    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 {

        protected void service(HttpServletRequest request, HttpServletResponse response)

                throws ServletException, IOException {

            request.setCharacterEncoding("UTF-8");

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

            // byte[] bytes = name.getBytes("ISO-8859-1");

            // name = new String(bytes, "UTF-8");

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

            System.out.println("name:" + name);

        }

    }

    示例 2 : 返回中文的响应

    在Servlet中,加上

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

    返回中文的响应

    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 {

        protected void service(HttpServletRequest request, HttpServletResponse response)

                throws ServletException, IOException {

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

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

            String html = null;

            if ("admin".equals(name) && "123".equals(password))

                html = "<div style='color:green'>登录成功</div>";

            else

                html = "<div style='color:red'>登录失败</div>";

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

            PrintWriter pw = response.getWriter();

            pw.println(html);

        }

    }


    更多内容,点击了解: https://how2j.cn/k/servlet/servlet-gbk/554.html

  • 相关阅读:
    Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
    Spring Cloud学习笔记【一】Eureka服务注册与发现
    Springboot分布式限流实践
    Springboot分布式锁实践(redis)
    Springboot2本地锁实践
    Springboot集成mybatis通用Mapper与分页插件PageHelper
    Springboot多数据源配置
    redis主从集群搭建
    assert的基本用法
    spring中BeanPostProcessor笔记
  • 原文地址:https://www.cnblogs.com/Lanht/p/12615421.html
Copyright © 2011-2022 走看看