百度下拉框

来源:互联网 发布:windows7 软件中文乱码 编辑:程序博客网 时间:2024/06/03 04:02

jsp

<%@ 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>My JSP 'index.jsp' starting page</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 src="js/jquery-1.8.3.js" type="text/javascript"></script>    <script type="text/javascript">        $(function() {            $("#name").keyup(function(){            document.getElementById("mess").style.display="block";            //$("#mess").attr("display","block");            //alert("1234")                $.ajax({                    url : "ajaxServlet", //要提交的URL路径                    type : "post",      //发送请求的方式                    data : {ser:$("#name").val()}, //要发送到服务器的数据                    dataType : "text",    //指定传输的数据格式                    success : function(result) {//请求成功后要执行的代码                        var arr = result.split(",");                        //alert("1");                        $("#mess").html("");                        for(var i=0;i<arr.length;i++){                            $("#mess").append("<div class='mouse' onmouseover='mouseover(this);' onmouseout='mouseout(this);' onclick='click1(this);'>"+arr[i]+"</div>");                        }                    },                    error : function() { //请求失败后要执行的代码                        alert("验证失败!!");                    }                });            });        });    function mouseover(v){        v.style.backgroundColor="#F4F2F4";    }    function mouseout(v){        v.style.backgroundColor="white";    }       function click1(v){        var value = v.innerHTML;        $("#name").val(value);        $("#mess").html("");    }    </script>  <style type="text/css">        body{width: 1024; }        table{margin-top: 50px; width: 70%}        tr{height: 50px}        .t1{ background-color:#DFFFFF }        #name{ width: 400px;}        #mess{position: absolute; top: 87px;left: 390px;display: none}    </style>  </head>  <body>   <form action="">    <table align="center">        <tr>        <td class="t1" width="25%">承租人名称</td>        <td class="t1" width="45%">        <input id="name" type="text"  >         </td>        </tr>        <tr>        <td class="t1" width="25%">友情提示</td>        <td class="t1" width="45%"> </td>        </tr>        <tr>        <td class="t1" width="25px">是否锁定</td>        <td class="t1" width="45%"> </td>        </tr>    </table>    <div id="mess" style="width: 400px; float: left;"></div>   </form>  </body></html>

servlet

package com.lq.servlet;import java.io.IOException;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.lq.utils.JdbcUtil;public class AjaxServlet extends HttpServlet {    /**     * The doGet method of the servlet. <br>     *     * This method is called when a form has its tag value method equals to get.     *      * @param request the request send by the client to the server     * @param response the response send by the server to the client     * @throws ServletException if an error occurred     * @throws IOException if an error occurred     */    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        this.doPost(request, response);    }    /**     * The doPost method of the servlet. <br>     *     * This method is called when a form has its tag value method equals to post.     *      * @param request the request send by the client to the server     * @param response the response send by the server to the client     * @throws ServletException if an error occurred     * @throws IOException if an error occurred     */    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        response.setCharacterEncoding("UTF-8");        String ser= request.getParameter("ser");        System.out.println(ser);        JdbcUtil ju=new JdbcUtil();        String sql="select name,card from t_users t where name like '%"+ser+"%'";        System.out.println(sql);        //List list=new ArrayList();        String s="";        try {            ResultSet rs=ju.querry(sql, null);            while(rs.next()){                System.out.println(rs.getString(1)+"_"+rs.getString(2)+",");                s=s+(rs.getString(1)+"_"+rs.getString(2)+",");            }        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        response.getWriter().print(s);    }}