JSP+AJAX实现的验证用户名是否存在

2024-11-23 05:16:55
推荐回答(1个)
回答1:

jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>








用户名:


密  码:





servlet:

package classmate;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginAction extends HttpServlet {

public void init(ServletConfig config) throws ServletException {
}

/*
* 处理 请求方法.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收信息的字符集
request.setCharacterEncoding("UTF-8");
//接收浏览器端提交的信息
String uname = request.getParameter("uname");
String psw = request.getParameter("psw");
//设置输出信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
//创建输出流对象
PrintWriter out = response.getWriter();
//依据验证结果输出不同的数据信息
out.println("");
if(uname.equals("jenny") && psw.equals("hi")){
out.println("" + "热烈的欢迎您!" + "");
}else{
out.println("" + "对不起,登录失败!" + "");
}
out.println("
");
out.close();
}
/*
* 处理 请求方法.
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}