前臺頁面與後臺servlet實現登入的三種實現方式
第一種:action獲取資料在servlet實現轉發處理實現登入
前臺頁面
<form action="Hasdf" method="get">
賬號:<input type="text" id="userId" name="userId"><br>
密碼:<input type="text" id="userPwd" name="userPwd"><br>
<input type="submit" value="提交">
</form>
後臺servlet:
String userid = request.getParameter("userId");
String userpwd = request.getParameter("userPwd");
if ("".equals(userid) || "".equals(userpwd)) {
response.sendRedirect("login.jsp");
}else if(null == userid && null == userpwd ) {
response.sendRedirect("login.jsp");
}else if("admin".equals(userid) && "123".equals(userpwd) ) {
response.sendRedirect("sucess.jsp");
}else {
response.sendRedirect("login.jsp");
}
第二種:獲取引數在Jspye頁面進行處理實現登入
後臺:
response.setContentType("text/html;charset=utf-8");//設定http輸出格式
response.setCharacterEncoding("utf-8");//設定字元編碼格式
HttpSession session = request.getSession();
String rec="";
String userName= request.getParameter("userName");
String userId= request.getParameter("userId");
if("".equals(userName)|"".equals(userId)|userName==null|userId==null) {
session.setAttribute("msg", "輸入錯誤");
response.sendRedirect("NewFile1.jsp");
}else {
rec="歡迎"+userName+"回來!";
//rec=new String(rec.getBytes("iso8859-1"),"utf-8");
response.getWriter().write(rec);
}
}
前臺
<body>
<%
String str=(String)session.getAttribute("msg");
if(str != null ){
out.write("<script type='text/javascript' >alert('"+str+"') </script>");
}
%>
<form action="Tser" method="post">
<input type="text" name="userName" id="userName" value="" />
<input type="password" name="userId" id="userId" value="" />
<input type="submit" value="提交"/>
</form>
</body>
第三種:使用ajax進行處理實現登入
前臺:
<title>Insert title here</title>
<script type="text/javascript">
function getUser(){
variable=new XMLHttpRequest();
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
} else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.status==200 ){
if(xmlhttp.readyState==4){
var str =xmlhttp.responseText;
document.getElementById("dimm").innerHTML=xmlhttp.responseText;
if(str=="OK"){
location.href="Login.jsp";
}
}
}
}
var vv="userName="+document.getElementById('userName').value+"&userPwd="+document.getElementById('userPwd').value
xmlhttp.open("POST","login?"+vv);
xmlhttp.send(null);
}
</script>
</head>
<body>
<form action="login" method="post">
userName<input type="text" name="userName" id="userName" /> <br>
userPwd<input type="password" name="userPwd" id="userPwd" /> <br>
<!-- <input type="submit" value="sumbmit"/> -->
<input type="button" id="errm" value="按鈕" onclick="getUser()" />
</form>
<div id="dimm"></div>
</body>
</html>
後臺:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
String userName =req.getParameter("userName");
String userPwd =req.getParameter("userPwd");
// HttpSession session = req.getSession();
if(userName==null||userPwd==null){
System.out.println("111111111");
//session.setAttribute("errMsg", "您輸入的姓名和密碼有誤");
//resp.sendRedirect("MyFirst.jsp");
resp.getWriter().write("賬號密碼不能為空!!!!");
}else if("".equals(userName)||"".equals(userPwd)) {
System.out.println("22222222");
//session.setAttribute("errMsg", "您輸入的姓名和密碼有誤");
//resp.sendRedirect("MyFirst.jsp");
resp.getWriter().write("賬號密碼不能為空!!!!");
}else if("aaa".equals(userName)&&"aaa".equals(userPwd)) {
resp.getWriter().write("OK");
}else {
System.out.println("33333333");
//session.setAttribute("errMsg", "您輸入的姓名和密碼有誤");
//resp.sendRedirect("MyFirst.jsp");
resp.getWriter().write("賬號密碼輸入錯誤!!!!!");
}
}