• 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]

  • 相关阅读:
    Java并发之CAS与AQS简介
    关系型数据库三范式
    分库分表使用场景及设计方式
    项目部署到tomcat出错(tomcat运行时的JDK版本)
    手写一个简化版Tomcat
    java并发之并发工具
    java并发之停止线程
    class中static总结-静态成员函数和静态成员变量
    45 孩子们的游戏(圆圈中最后剩下的数) + list操作总结+ for_each多记忆容易忘记
    C++ split分割字符串函数
  • 原文地址:https://www.cnblogs.com/sky7034/p/2059518.html
Copyright © 2020-2023  润新知