我正在尝试向JEE / jquery / datatables应用程序添加身份验证例程

时间:2018-03-21 17:12:06

标签: jquery ajax jsp

我有一个正常运行的JQuery / DataTables appilcation。我想在index.jsp的加载(实际上在加载之前)添加一个例程,它检查当前用户是否有权使用该应用程序。

我的问题是我没有运气使Authenticate类运行。这是我的代码:

的index.jsp:

  <%@page contentType="text/html" pageEncoding="UTF-8"%>
  <%@page import="member.Authenticate" %>
 <!DOCTYPE html>
 <html>
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <link rel="stylesheet" type="text/css" href="css/datatables.min.css"/>
    <link rel ="stylesheet" type="txt/css" href="css/dataTables.jqueryui.min.css"/>

    <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="js/datatables.min.js"></script>
    <script type="text/javascript" src="js/datatables.jqueryui.min.js"> 
</script>

    <script type="text/javascript">


         $.get ( { 
             url: "${pageContext.request.contextPath}/Authenticate",
             async: "false" 
         });  <!-- Authenticate does not get called -->


         var ok = '<%= session.getAttribute("OK") %>';


        $(document).ready(function () {






            var memberTable = $('#memberList').DataTable({
                "jqueryui": true,
                "scrollY": "150px",
                "paging": false,
                "select": "single"

            });

            var txnTable = $('#txnList').DataTable({
                "jqueryui": true,
                "scrollY": "520px",
                "paging": false,
                "select": "single"
            });

            $("#btnFind").click(function () {
                getSubsFromDB();
            });

            $("#txtSearchCriteria").keypress(function (event) {
                if (event.which === 13) {
                    event.preventDefault();
                    getSubsFromDB();
                }
            });



            function getSubsFromDB() {
                searchCriteria = $("#txtSearchCriteria").val().trim();
                subsOnly = "N";
                currentOnly = "N";

                if ($('#chkSubsOnly').is(":checked")) {
                    subsOnly = "Y";
                }
                if ($('#chkCurrentOnly').is(":checked"))
                {
                    currentOnly = "Y";

                }
                memberTable.search( ' ' );

 memberTable.ajax.url("${pageContext.request.contextPath}/SubSearch"
                        .concat("?SearchCriteria=", searchCriteria)
                        .concat("&SubscribersOnly=", subsOnly)
                        .concat("&CurrentOnly=", currentOnly)).load();
                <!-- SubSearch Does get called -->

 txnTable.ajax.url("${pageContext.request.contextPath}/TXNList"
                        .concat("?subscriber=9999999999999")).load();
            }




            memberTable.on('select', function (e, dt, type, indexes) {
                var subscriber = memberTable.rows(indexes[0]).data()[0][0];
                var npsGroup = memberTable.rows(indexes[0]).data()[0][6];
                //alert (subscriber);

 txnTable.ajax.url("${pageContext.request.contextPath}/TXNList"
                        .concat("?subscriber=",subscriber)).load();
            });

            document.getElementById("chkCurrentOnly").checked = false;
        });

    </script>
    <title>MEMBER TXN</title>

</head>
<body>

    <h1>Member TXN Display</h1>
    <div>


        <form>
            (Enter: last name or last name,first name)
            <input type="text" id="txtSearchCriteria" style="width:222px"/>
            <input id="btnFind" type="button" value="Find" />

            <input id="chkSubsOnly" type="checkbox"  checked="true" />
            <label for="chkSubsOnly">Subscribers Only</label>

            <input id="chkCurrentOnly" type ="checkbox" checked="false"/>
            <label for="chdCurrentOnly">Current Contracts Only </label>
        </form>

        <table id="memberList" class="display" cellspacing="0" width="70%" >
            <thead>
                <tr>
                    <th style="text-align: left">Subscriber</th>
                    <th style="text-align: left">Last Name</th>
                    <th style="text-align: left">First Name</th>


                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    <div>
        <table id="txnList" class="display" cellspacing="0">
            <thead>
                <tr>

                    <th style="text-align: left">Transaction</th>
                    <th style="text-align: left">Transaction Description</th>
                    <th style="text-align: left">Effective Date</th>

                </tr>
            </thead>
        </table>

    </div>
    </body>
 </html>

我的问题似乎是没有调用Authenticate。

这里是Authenticate Routine(IDE:NetBeans)---似乎在独立工作     包成员;

 import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import javax.servlet.http.HttpSession;
 import oracle.jdbc.pool.OracleDataSource;

 /**
  *
  * @author Ainsworth
  */
 @WebServlet(name = "Authenticate", urlPatterns = {"/Authenticate"})
 public class Authenticate extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 * @throws java.sql.SQLException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    HttpSession session = request.getSession(true);
    try {



        String userName = 
      System.getProperty("user.name").trim().toUpperCase();
        String userName2  = new 
   com.sun.security.auth.module.NTSystem().getName().trim().toUpperCase();


        String oracleConnectionString
                = "jdbc:oracle:thin:@(description=(address=(host=oraclehost)(port=1521)(protocol=tcp))(connect_data=(sid=testsid)))";

        OracleDataSource ods = new OracleDataSource();
        ods.setURL(oracleConnectionString);
        ods.setUser("oracleUser");
        ods.setPassword("oraclePassword");
        Connection ora = ods.getConnection();

        String strGetAuthRecordCount
                = "SELECT COUNT(*) "
                + "FROM APP_USERS "
                + "WHERE UPPER(USER_ID) = ? AND APP_NAME = 'MEMBER_TXN' ";


        PreparedStatement stmtAuthRecordCount;

        stmtAuthRecordCount = ora.prepareStatement(strGetAuthRecordCount);
        stmtAuthRecordCount.setString(1,userName);




        ResultSet rsAuth = stmtAuthRecordCount.executeQuery();
        rsAuth.next();
        if (rsAuth.getInt(1) > 0) {
            session.setAttribute("OK","true");

        } else {


        session.setAttribute("OK","false");
                }


    } catch (Exception ex) {
        session.setAttribute("OK","error");
    }
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (SQLException ex) {
    }
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (SQLException ex) {
    }
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

1 个答案:

答案 0 :(得分:0)

我将javascript的第一部分更改为以下内容:

<script type="text/javascript">

        $.ajax({
            url: "${pageContext.request.contextPath}/Authenticate",
            type: "get",
            async: "false",
            cache: "false"


        });

并执行Authenticate例程。关键是添加缓存:&#34; false&#34; 。