0

在我的应用程序中,我有一个带有文本框、复选按钮和密码的用户名。在文本框中输入用户名后,如果我单击检查按钮,它应该在 MySQL 数据库中搜索用户名,如果可用,它应该显示该消息,如果不应该显示其他消息。

在此处输入图像描述

我如何用 JSP 做到这一点?我尝试了以下代码:

    <form  method="post" name="frm_addUser" action="./adduserserver.jsp"><br><br>
<table width="500px;" border="0" cellpadding="5" cellspacing="1" bgcolor="#f8f8ff" bordercolor="#333366" align="center">


<tr>
<td bordercolor="Gainsboro"><font size="4">User ID</font></td>
<td bordercolor="Gainsboro"><input name="userid" style="WIDTH: 200px"></td></tr>

<tr>
<td bordercolor="Gainsboro"><font size="4">&nbsp;</font></td>

<!--<td><input value="Check availability" onclick="" class="btn_checkavail" type="button"></td></tr>-->
</td>
<td>
<input type="submit" value="check" name="check" 
onclick="" /></td></tr>     
<tr>
<td bordercolor="Gainsboro"><font size="4">Pass Word </font></td>
<td bordercolor="Gainsboro"><input name="password" type="password" style="WIDTH: 200px"></td></tr>

<tr>
<td bordercolor="Gainsboro"><font size="4">Confirm Password </font></td>
<td bordercolor="Gainsboro"><input name="confirmpassword" type="password" style="WIDTH: 200px"></td></tr>
<tr>
<%
try{
String username=request.getParameter("username");

    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","sumith");
    st=con.createStatement();

    sqlQuery="select distinct username from usernameexist where username='"+username+"'";
    rs=st.executeQuery(sqlQuery);
int count=0;
while(rs.next())
{
count++;
}
if(count>0)
{
            out.println("<html>");
                        out.println("<head>");
            out.println("<title>MeterDetailsPage</title>");
            out.println("</head>");
            out.println("<body>");
                        out.println("<table align='center' color='red'>");
                        out.println("<tr color='red'>");
                        out.println("<td ><font size=4 color=red >username  Already Exist</font></td>");
                        out.println("</tr>");
                        out.println("</table>");
                        out.println("</body>");
                        out.println("</html>");
}

else
{

if(username!=null )

{

if(!username.equals(""))

{

            //st.executeUpdate("insert into usernameexist(username) values('"+username+"')");
            out.println("<html>");
                        out.println("<head>");
            out.println("<title>username</title>");
            out.println("</head>");
            out.println("<body>");
                        out.println("<table align='center'>");
                        out.println("<tr>");
                        out.println("<td ><font size=4  color=green><b>available </b></font></td>");
            out.println("</table>");
                        out.println("</body>");
                        out.println("</html>");
}
}
}
st.close();
con.close();
}
catch(Exception e){}
%>



</table>
</form>
</body>
</html>
4

4 回答 4

1

试试这个它会工作,

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Index Page</title>

<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="js/app-ajax.js" type="text/javascript"></script>
</head>
<body>

        Enter Your Name: <input type="text" id="name" /><br>
        Enter our user name :<input type="text" id="userName"><br>
        Enter your Password :<input type="password" id="password"><br>
        <input type="button" value="Submit" onclick="ajaxCall();">        
    <br>

    <strong> Response</strong>:
    <div id="ajaxGetUserServletResponse"></div><br>

</body>
</html>

阿贾克斯文件

function ajaxCall(){
    var name = jQuery("#name").val(); 
    var userName = jQuery("#userName").val();
    var password= jQuery("#password").val();
    alert(name);
    alert(userName);
    alert(password);

    jQuery.ajax({
        url : "GetUserServlet", 
        method: "GET", 
        type : "JSON", 
        data : "name="+name+"&userName="+userName+"&password="+password,// query parameters 1st 
        success : function(response){
            $('#ajaxGetUserServletResponse').text(response);

        }
    });


}

小服务程序

import java.io.IOException;


import java.util.ArrayList;

import javax.management.relation.RelationSupportMBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GetUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

        userBean ub = new userBean();
        String name = request.getParameter("name").trim();
        String userName = request.getParameter("userName").trim();
        String password = request.getParameter("password").trim();

        System.out.println("name catched "+name);
        System.out.println("username catched"+userName);
        System.out.println("Password catched"+password);


        ArrayList<userBean> list = new ArrayList<userBean>();
         ub=new userBean();
         ub.setName(name);
         ub.setUserName(userName);
         ub.setPassword(password);
         list.add(ub);  

         response.setContentType("text/plain");
         response.getWriter().print(list);             
    }

}

Pojo类

public class userBean 
{
    private String name;
    private String userName;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

我的场景有点不同,您可以更改 index.jsp 中的代码并在“检查可用性”按钮上添加调用并从 servlet 获取响应。

于 2016-06-14T05:08:32.407 回答
0

你应该在下面做:

  1. 确保您的 jsp 将所有字段(包括选中字段)传递给 servlet。你可以做具有所有属性的表单帖子。

  2. 您的 servlet 应该读取所​​有参数,如果选中了复选框,那么您将适当的逻辑放入 servlet(例如查询您的 MySQL 数据库)

  3. 从您的 servlet 返回正确的响应,以便 jsp 可以显示适当的消息。您可以将消息保存在您的 jsp/js 中,并根据标志向用户显示适当的消息。

于 2012-12-06T08:48:52.077 回答
0

在查询中检查您的表名

select distinct username from usernameexist where username='"+username+"'";

确保您的表是用户名存在

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

这里的参数是用户 ID 不是用户名

所以用

String username=request.getParameter("userid");
于 2012-12-28T03:58:30.177 回答
0

你可以使用如下..在源代码中附加jquery文件

 <script src="jquery-1.9.1.js">

通知

 <input type="text" name="userName" id="userName" onBlur="checkId(this.value)">
 <span id="status"></span>

 <script type="text/javascript">
        function checkId(member_id)
        {
            $.ajax({
                type: "POST",
                url: "processAjax.jsp", /* The request will be processed in this file.. */
                beforeSend: function() {
                    $("#divIdStatus").html("geting name...");
                },
                data: "member_id=" + member_id ,
                success: function(msg) {
                    $("#divIdStatus").html(msg);
                }
            });
        }

在 processAjax.js

 String member_id = (request.getParameter("member_id")).trim();
        if (member_id.equals("")) {
            out.print("! This is Required");
        } else {


            ResultSet result = // check in database if any record with this user name;

            if (result.next()) {
                out.print("! Not Available");
            } else {
                out.print("OK..");
            }
        }
于 2014-03-20T15:07:41.850 回答