欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

啊啊啊

程序员文章站 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> 

 

相关标签: 建议