我有这个jsp文档(如下)。基本上,当用户在文本框中键入时,如果用户名存在于数据库/长度< 5等中,我想显示错误
我希望同时显示这些错误,而无需通过jQuery / AJAX进行任何刷新。我做了这个,但它似乎没有工作。这里,CheckAvailability和Success是servlet,CheckAVailability检查数据库中是否存在。
JSP文件:
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.11.3.js"></script>
<script>
$(document).ready(function() {
$('#username').keyup(function() {
var name = $('#username').val();
$.get('CheckAvailability?username='+name,function(responseText){
$('#status').text(responseText);});});
</script>
</head>
<body>
<form id="login_form" ><input type="text" placeholder="username" name="username" class="style-4" required="required" action="Success"/>
<div id="status"> </div>
CheckAvailability Servlet
public class CheckAvailability extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection conn=null;
Statement s=null;
ResultSet rs=null;
PreparedStatement ps;
try {
//make connection
String userid = request.getParameter("username");
String arr;
Class.forName("oracle.jdbc.OracleDriver");
if (userid.equals("")) {
arr = "Error: User name cannot be empty";
} else if(userid.length()<5){
arr="Error: Username cannot be less than 5 characters.";
}
else
{
String table="user1.app_users";
String p = "alpha";//database password
String query = "select userid from " + table + " where userid='" + userid + "'";
String url = "jdbc:oracle:thin:system/" + p + "@localhost:1521:XE";
conn = DriverManager.getConnection(url);
s = conn.createStatement();
ps = conn.prepareStatement(query);
rs = ps.executeQuery();
if (!rs.next()) {
arr="UserID <b>" + userid + "</b> is available.";
} else {
arr= "Error: UserID <b>" + userid + "</b> is already in use.";
}
}
response.setContentType("text/plain");
response.getWriter().write(arr);
}catch (SQLException se) {
out.println("Error ->" + se.getMessage());
} catch(ClassNotFoundException ce)
{
out.println("Error ->" + ce.getMessage());
}finally {
out.close();
}
}
}
但是当我在文本框中输入时,这并没有显示任何内容。 servlet在提交时点击了。没有发生的是文本没有显示在旁边。代码执行,我的IDE中没有错误。我不能专门运行servlet,它给出错误:'此方法不支持HTTP方法GET',即当我使用参数运行它时。我把输入内部输入,如果用户名=“user12”,那也没有运行。任何人都可以指出我的错误吗?我是jQuery / AJAX的新手。
答案 0 :(得分:1)
这对我有用:
$(document).ready(function() {
$('#userid').keyup(function(event) {
var user=$('#userid').val();
$.get('CheckValidity',{username:user},function(responseText) {
$('#status').text(responseText);
});
});
});
必须使用不同的JQuery。
答案 1 :(得分:0)
覆盖doGet
,因为javax.servlet.http.HttpServlet
没有任何processRequest
方法。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.print("hi " + req.getParameter("username"));
}
无论如何..你是如何声明你的servlet的?通过注释?在web.xml中?什么是URL模式?
如果使用web.xml:
<servlet>
<servlet-name>CheckAvailability Servlet</servlet-name>
<servlet-class>your.package.CheckAvailability</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckAvailability Servlet</servlet-name>
<url-pattern>/CheckAvailability</url-pattern>
</servlet-mapping>
如果使用注释:
@WebServlet("/CheckAvailability")
public class Serv extends HttpServlet {
// ...
}