我有一个注册用户页面,我可以在其中输入用户名和密码。单击注册按钮时,我需要验证用户是否已经在数据库中。
如果用户存在,我需要显示一个隐藏标签并留在屏幕上,或者导航到下一页。
JSP 代码
<%@ page import="pkg.Login.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<Script type = "text/javascript">
function ManFieldValidation()
{
String tempVal = document.forms["form1"]["txtUserID"].value;
UserExistingCheck strObj = new UserExistingCheck();
boolean iReturn = strObj.CheckIDExist(tempVal);
return iReturn
}
</Script>
<body>
<form name = "form1" onSubmit = "return ManFieldValidation()" action = "ServRegister" method="post">
<label id = "lblUserError" style="visibility:hidden;"><font color = "red">The User ID is already available</font></label><br></br>
<label>User ID</label>
<input type = "text"name = "txtUserID" size="8" maxlength="10"></input>
<label>Password</label>
<input type = "password" name = "txtPassword" size="8" maxlength="10"></input>
<input type = "submit" class = "tfbutton" name = "Register" value = "Register"></input>
</form>
</body>
</html>
JSP - “注册.jsp”
Servlet - “ServRegister.java”(如果用户不存在,将数据插入数据库)
因为我已经有一个 servlet;我尝试编写一个 java 方法来验证现有用户并返回真或假。下面是java代码。
Java 方法
package pkg.Login;
import java.sql.*;
public class UserExistingCheck
{
public void CheckIDExist(String tempVal)
{
try
{
String url = "jdbc:sqlserver://localhost:1433;"+
"databaseName=WebPortal;user=VRHAJAS;Password=Vignesh007";
Connection conn = null;
Statement select = null;
ResultSet result = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(url);
select = conn.createStatement();
String sQuery = "Select * from dbo.UserDetails where UserID = '" + tempVal + "'";
result=select.executeQuery(sQuery);
conn.close();
if(result.next())
{
return false;
}
else
{
return true;
}
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
}
当我单击注册按钮时,不会调用上述方法。而是执行“ServRegister.java”。也没有显示错误。我正在使用日食。任何人都可以提出解决方案吗?