啊啊啊
程序员文章站
2022-06-07 16:02:15
...
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserLoginServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
if("Mike".equals(username) && "123".equals(userpwd)) {
response.sendRedirect("loginOK.jsp");
}else {
request.setAttribute("msg", "用户登录失败");
request.getRequestDispatcher("loginNO.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
this.doGet(request, response);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#loginBtn").click(function(){
var username =$("#username").val();
var userpwd = $("#userpwd").val();
var formURL = $("form:eq(0)").attr("action");
$.ajax({
'url':formURL,
'data':{'username':username,'userpwd':userpwd},
'type':'post',
'success':function(data){
$("#showInfo").html(data);
}
});
});
});
</script>
</head>
<body>
<form action="userLoginServlet" method="post">
用户名:<input type="text" id="username" name="username"/><br/>
用户密码:<input type="password" id="userpwd" name="userpwd"/><br/>
<input type="button" id="loginBtn" value="用户登录"/>
</form>
<div id="showInfo"></div>
</body>
</html>
上一篇: RAC环境下的阻塞(blockingblocked)
下一篇: WML处理---H_wml.php