////////////////////////////////////////后台//////////////////////////////////////////////////////////////////
public class LoginCheckServlet extends HttpServlet {
PublicSystem sys = PublicSystem.getInstance();
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/json;charset=utf-8");
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Access-Control-Max-Age", "3600");
PrintWriter out = response.getWriter();
String uri=request.getRequestURI();
System.out.println(uri);
String action=request.getParameter("action");
System.out.println(action);
String username=request.getParameter("username");
System.out.println(username);
if(action.equals("logincheck")){
if ("admin".equals(username)) {
System.out.println("111");
try {
sys.executeSql("insert into sss (id,sss) values ('2','"+username+"')");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write("checkOK");
}
else {
System.out.println("222");
out.write("checkError");
}
}
out.close();
}
}
////////////////////////////////////////////////////////////////HTML////////////////////////////////////////////////////////////
<!DOCTYPE html>
<html class="ui-page-login">
<head>
<meta charset="utf-8">
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta http-equiv="Access-Control-Max-Age" content="3600000">
<meta http-equiv="content-security-policy">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>测试</title>
<link href="css/mui.min.css" rel="stylesheet" />
<script src="js/mui.min.js"></script>
</head>
<body>
<div class="mui-bar mui-bar-tab" style="height:20px;line-height:20px;font-size:10px;text-align:center;">
这是底部信息
</div>
<div class="mui-content-padded" style="margin:5px">
<form class="mui-input-group">
<div class="mui-input-row">
<label>用户名</label>
<input type="text" id="username" placeholder="用户名" />
</div>
<div class="mui-input-row">
<label>密码</label>
<input type="password" id="userpassword" placeholder="密码" />
</div>
</form>
</div>
<div style="margin-top:20px;text-align: center;">
<button class="mui-btn mui-but-primary" id="loginBtn">登录</button>
<button class="mui-btn mui-but-primary" id="regBtn">注册</button>
</div>
<script>
function postData(url, data, callback) {
mui.ajax(url,{
data:data,
dataType:'text',
type:'GET',
crossDomain:true,
contentType:"application/x-www-form-urlencoded; charset=utf-8",
timeout:3600,
success:callback,
error:function(xhr,type,errorThrown){
mui.alert("<网络连接失败,请重新尝试一下>", "错误", "OK", null);
}
});
}
document.getElementById("loginBtn").addEventListener('tap',function(){
var data={"username":document.getElementById("username").value};
postData("http://127.0.0.1:8080/APPtest/loginCheck.do?action=logincheck", //服务端的URL
data, //json数据
function(text){
if(text=="checkOK"){
mui.alert("登录成功","登录成功啦","关闭");
}
else{
mui.alert("用户名或密码错误","登录错误","关闭");
}
}
);
});
</script>
</body>
</html>
///////////////////////////////////////////////////////////////////
sql语句会被执行两次,插入两条相同的数据