Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $.ajax({
- url: "LoginServlet",
- type: "post",
- data: {
- "username": username,
- "password": password
- },
- cache: false,
- success: function(data) {
- var xhr = data.msg;
- if (xhr === "true") {
- alert(xhr);
- window.location.href = "success.jsp";
- } else {
- alert(xhr);
- }
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
- LoginDAO dao = new LoginDAO();
- String username=request.getParameter("username");
- String password=request.getParameter("passsword");
- String msg = "false";
- Login login = dao.validateUser(username, password);
- String username1 = login.getUsername();
- String password1 = login.getPassword();
- if(LoginDAO.isEqual(username, password1) && LoginDAO.isEqual(password, password1)){
- msg = "true";
- }
- response.setContentType("text/plain");
- response.getWriter().write(msg);
- }
- <script>
- $(document).ready(function() {
- $("button").click(function() {
- var username = "admin";
- var password = "admin";
- $.ajax({
- url: "LoginServlet",
- type: "post",
- data: {
- "username": username,
- "password": password
- },
- cache: false,
- success: function(data) {
- console.log("data: " + data);
- var xhr = data;
- console.log("xhr: " + xhr);
- if (data == "true") {
- alert(xhr);
- window.location.href = "success.jsp";
- } else {
- alert(xhr);
- }
- }
- });
- });
- });
- </script>
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- response.setContentType("text/plain");
- response.getWriter().write(msg);
- $.ajax({
- url: "LoginServlet",
- type: "post",
- data: {
- "username": username,
- "password": password
- },
- dataType: "text", // To match with the actual response type: «text/plain».
- cache: false,
- success: function (data) {
- var xhr = data; // Because «data» contains true or false.
- if (xhr === "true") {
- alert(xhr);
- window.location.href = "success.jsp";
- } else {
- alert(xhr);
- }
- }
- });
Add Comment
Please, Sign In to add comment