zoukankan      html  css  js  c++  java
  • jquery的Ajax框架乱码解决

    1.下面代码为服务器端获取请求信息Servlet,真正用的时候可能就是struts等

    package com.adu;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.URLDecoder;

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

     
    public class AjaxServer extends HttpServlet {

        private static final long serialVersionUID = -1391282338046351379L;

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
           
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");  
          
            PrintWriter out = response.getWriter();
            商账追收
            //获取页面jquery的url提交的数据,进行处理
    //        String userName = request.getParameter("name");
    //        String passWord = request.getParameter("password");
           
            String name = request.getParameter("name");
             String password = request.getParameter("password");
           
            //转换字符集编码,避免中文乱码问题
            String userName = URLDecoder.decode(name,"UTF-8");
            String passWord = URLDecoder.decode(password,"UTF-8");
               
           
            //进行逻辑判断,看输入是否合法
            if(userName.length() != 0 && userName != null && passWord.length() != 0 && passWord != null){
               
                if(userName.equals("mawanli") && passWord.equals("123")){
                   
                    //out.print("Welcome* "+userName+" *visit my web!");
                    out.print("欢迎* "+userName+" *访问!");
                    out.flush();
                    out.close();
                   
                }else{
                   
                    out.print("sorry please login again!");
    //                out.print("请输入正确的用户名和密码!");
                }   
            }
            else{
               
                out.print("please input right name and password!");
                //out.print("请重新登录!");
            }
             
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setContentType("text/html");
            this.doGet(req, resp);
        }

    }

    2.以下是页面测试的代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
       
        <title>测试ajax验证登录</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">   
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->

      <script type="text/javascript" src="js/jquery-1.6.min.js"></script></head>
      <script type="text/javascript">
     
      //添加时间戳,以防每次测试为不同的url,解决浏览器缓存问题
    function convertUrl(url) {  
         
          var timestamp = (new Date()).valueOf();  
          if (url.indexOf("?") >= 0) {  
           url = url + "&t=" + timestamp;  
          } else {   发型123
             url = url + "?t=" + timestamp;  
         }  
           return url; 
        
      }
      //用encodeURI可以解决页面传值时的中文乱码问题
      function validate(){
          var name = encodeURI(encodeURI($("#userName").val()));
          var password = encodeURI(encodeURI($("#passWord").val()));
          var url = "AjaxServer?name="+name+"&password="+password;
          url = convertUrl(url);
          
           $.get(url,null,function(data){
                                   $("#result").html(data);
                           });
      }
     
     
      </script>
       
      <body>
       <div align="center">
       <div >
       <form action="#" method="get">
       <table>
       <tr><td>用户名:</td><td><input type="text" name="userName" id="userName"/></td></tr>
       <tr><td>密&nbsp;&nbsp;&nbsp;&nbsp;码:</td><td><input type="password" id="passWord"/></td></tr>
       <tr align="right"><td colspan="2"><input type="button" value="登录" onclick="validate()"/>
       <input type="reset" value="取消"/></td></tr>
       </table>
       </form>
      
       <div id="result"></div>
       </div>
       <hr width="600px" align="center" size="3" color="green"/>
      
      
       </div>
        
      </body>
    </html>
    <pre name="code" class="java"></pre>[align=left][/align]

  • 相关阅读:
    云原生体系下 Serverless 弹性探索与实践
    PaddlePaddle:在 Serverless 架构上十几行代码实现 OCR 能力
    manjaro kde系统格式化U盘
    从B站看到的资源网站(确实是很不错,推荐!)
    ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysql 或 manjaro 安装 mariadb
    manjaro kde 我安装后使用到的命令
    思考了许久,博客在未来将会进行的改变(2021-10-03)
    linux 下配置 python源
    ModuleNotFoundError: No module named 'distutils.util'
    mysqlclient 安装失败
  • 原文地址:https://www.cnblogs.com/sky7034/p/2059518.html
Copyright © 2011-2022 走看看