Bootstrap远程验证与java无法正常工作

时间:2016-03-17 18:19:39

标签: twitter-bootstrap

当chcek用户名可用时,它总是给出mesg'用户名不可用'。它如何解决这个问题。请帮我 提前感谢您的建议或解决方案

注册页面

**Reg.jsp**   

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title> ON-DB   &nbsp;  &nbsp;  &nbsp;Registration</title>
    <link rel="shortcut icon" href="images/online-chart-sold-products-by-region.png">
    <link rel="stylesheet" href="css/bootstrap.css"/>
        <link rel="stylesheet" href="css/bootstrapValidator.css"/>

        <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <script type="text/javascript" src="js/bootstrapValidator.js"></script>
    </head>
    <body>

    <div class="main">
    <%@include file="Header.jsp" %>
    <div class="container">

        <div class="row">
          <section>
            <div class="col-lg-8 col-lg-offset-2">

                <div class="page-header">
                    <h2>Registration</h2>
                </div>

                <form action="RegistrationSevlet" id="defaultForm" method="post" class="form-horizontal" >
                    <div class="form-group" id="CheckUsername">
                        <label class="col-lg-3 control-label">Username</label>
                        <div class="col-lg-5">
                            <input type="text" class="form-control" name="username" /><span class="status"></span>
                        </div>
                         <div class="progress" id="progressBar" style="margin: 5px 0 0 0; display: none;">
                             <div class="progress-bar progress-bar-success progress-bar-striped active" style="width: 100%"></div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">First Name </label>
                        <div class="col-lg-5">
                            <input type="text" class="form-control" name="firstName" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">Last Name </label>
                        <div class="col-lg-5">
                            <input type="text" class="form-control" name="lastName" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-lg-3 control-label">Email </label>
                        <div class="col-lg-5">
                            <input type="text" class="form-control" name="email" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-lg-3 control-label">Password</label>
                        <div class="col-lg-5">
                            <input type="password" class="form-control" name="password" />
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-lg-9 col-lg-offset-3">
                            <input type="submit" class="btn btn-default" value="SignUp"/>
                        </div>
                    </div>
                </form>
            </div>
            </section>s
        </div>
    </div>
    </div>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#defaultForm')

            .bootstrapValidator({
                framework: 'bootstrap',
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    username: {
                       threshold: 6,
                       message: 'The username is not valid',
                        validators: {

                            notEmpty: {
                                message: 'The username is required and can\'t be empty',
                            },
                            stringLength: {
                                min: 6,
                                max: 20,
                                message: 'The username must be more than 6 and less than 20 characters long',
                            },

                            regexp: {
                                regexp: /^[a-zA-Z0-9_\.]+$/,
                                message: 'The username can only consist of alphabetical, number, dot and underscore',
                            },
                                remote: {
                                type: "POST",
                                url: 'Check1',
                                delay:1000,

                                message: 'The username is not available', 

                            }, 


                        }
                    },
                    firstName: {
                        validators: {
                            notEmpty: {
                                message: 'The first name is required and cannot be empty'
                            }
                        }
                    },
                    lastName: {
                        validators: {
                            notEmpty: {
                                message: 'The last name is required and cannot be empty'
                            }
                        }
                    },

                    email: {
                        validators: {
                            notEmpty: {
                                message: 'The email is required and can\'t be empty'
                            },
                            emailAddress:{
                                message: 'The input is not a valid '
                            }
                        }
                    },
                    password: {
                        validators: {
                            notEmpty: {
                                message: 'The password is required and can\'t be empty'
                            }
                        }
                    }
                }
            })

    });
    </script>

    </body>
    </html>

Servlet

Check.java

公共类Check1扩展了HttpServlet {     private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{

     String user_db=null;
      PrintWriter out = response.getWriter();

      String uname=request.getParameter("username");
      System.out.println(uname);
      String SQL="SELECT username FROM userinfo WHERE username='"+uname+"'";   
      Connection con=DBConnection.getConnection(user_db);

      Statement st;
      ResultSet rs;

      boolean status=false;

     try 
     {
      st=con.createStatement();
       rs=st.executeQuery(SQL);
      System.out.println(SQL);

      if(!rs.next())
      { 
          status=false;
         out.print(status);
          System.out.println("false");
       }
      else
      { 
         status=true;
        out.print(status);

          System.out.println("true");
        }
     }
     catch(Exception ex)
     {
         ex.printStackTrace();
     }

} }

1 个答案:

答案 0 :(得分:0)

您发送回bootstrap验证器的数据不是正确的json格式。将其更改为正确的json格式,我会工作。

这对我有用。根据您的要求进行更改

<input type="button" value="Button" onClick="someFunction()"/>

function someFunction(){
var params = 'line to add parameter'
window.location.href="<s:url action='my-action'/>"+params;
}