ajax 使用者名稱密碼登陸驗證 java jsp
阿新 • • 發佈:2018-12-11
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <c:set var="prc" value="${pageContext.request.contextPath}"></c:set> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <script type="text/javascript" src="${prc}/js/jquery-3.2.1.min.js"></script> <script type="text/javascript" src="${prc}/js/testjs.js"></script> </head> <body> <form action=""> <table border="0"> <tr><td>使用者名稱:</td><td><input type="text" name="name"/></td></tr> <tr><td>密碼:</td><td><input type="password" name="pwd"/></td></tr> <tr><td></td><td><input type="button" id="btnSubmit" value="提交"/></td></tr> </table> </form> <div id="show"></div> <%-- JSP EL隱含物件 pageScope page 作用域 requestScope request 作用域 sessionScope session 作用域 applicationScope application 作用域 param Request 物件的引數,字串 paramValues Request物件的引數,字串集合 header HTTP 資訊頭,字串 headerValues HTTP 資訊頭,字串集合 initParam 上下文初始化引數 cookie Cookie值 pageContext 當前頁面的pageContext 您可以在表示式中使用這些物件,就像使用變數一樣。接下來會給出幾個例子來更好的理解這個概念。 pageContext物件 pageContext物件是JSP中pageContext物件的引用。通過pageContext物件,您可以訪問request物件。比如,訪問request物件傳入的查詢字串,就像這樣: ${pageContext.request.queryString} Scope物件 pageScope,requestScope,sessionScope,applicationScope變數用來訪問儲存在各個作用域層次的變數。 舉例來說,如果您需要顯式訪問在applicationScope層的box變數,可以這樣來訪問:applicationScope.box。 param和paramValues物件 param和paramValues物件用來訪問引數值,通過使用request.getParameter方法和request.getParameterValues方法。 舉例來說,訪問一個名為order的引數,可以這樣使用表示式:${param.order},或者${param["order"]}。 接下來的例子表明瞭如何訪問request中的username引數: --%> <%-- <%@ page import="java.io.*,java.util.*" %> <% String title = "Accessing Request Param"; %> --%> <%-- <html> <head> <title><% out.print(title); %></title> </head> <body> <center> <h1><% out.print(title); %></h1> </center> <div align="center"> <p>${param["username"]}</p> </div> </body> </html> --%> <%-- param物件返回單一的字串,而paramValues物件則返回一個字串陣列。 header和headerValues物件 header和headerValues物件用來訪問資訊頭,通過使用 request.getHeader方法和request.getHeaders方法。 舉例來說,要訪問一個名為user-agent的資訊頭,可以這樣使用表示式:${header.user-agent},或者${header["user-agent"]}。 接下來的例子表明瞭如何訪問user-agent資訊頭: --%> <%-- <%@ page import="java.io.*,java.util.*" %> <% String title = "User Agent Example"; %> --%> <%-- <html> <head> <title><% out.print(title); %></title> </head> <body> <center> <h1><% out.print(title); %></h1> </center> <div align="center"> <p>${header["user-agent"]}</p> </div> </body> </html> 執行結果如下: --%> <%-- <c:set var="aaa" value="${request.getContextPath() }"></c:set> <c:set var="prc" value="${pageContext.request.contextPath}"></c:set> --%> <%-- aaa:${aaa }<br/> --%> <!-- 沒有輸出 --> <%-- prc:${prc }<br/> --%> <!-- 輸出:/ajaxtest --> <%-- <% out.println(request.getContextPath()); %> --%> <!-- 輸出:/ajaxtest --> <%-- <%= request.getContextPath() %> --%> <!-- 輸出:/ajaxtest --> <%-- <c:out value="${request.getContextPath() }"></c:out><br/> --%> <!-- 沒有輸出 --> <%-- <c:out value="request.getContextPath()"></c:out><br/> --%> <!-- 輸出 request.getContextPath() --> <%-- <c:out value="${pageContext.request.contextPath}"></c:out> --%><!-- 輸出:/ajaxtest --> <%-- <c:out value="sss"></c:out><br/> --%> <!-- 輸出:sss --> <%-- <jsp:setProperty name="box" property="perimeter" value="100"/> ${expr} --%> <%-- <jsp:setProperty name="box" property="perimeter" value="${2*box.width+2*box.height}"/> --%> </body> </html>
Testservlet.java
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Administrator */ @WebServlet(name = "Testservlet", urlPatterns = {"/testservlet"}) public class Testservlet 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 */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet Testservlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet Testservlet at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } // <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 { processRequest(request, response); } /** * 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 { response.setContentType("application/text; charset=utf-8"); PrintWriter out = response.getWriter(); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); //System.out.println("name="+name+" pwd="+pwd); if(name.equals("admin") && pwd.equals("123456")){ try { out.write("true"); } catch (Exception e) { e.printStackTrace(); }finally{ out.close(); } }else{ try { out.write("false"); } catch (Exception e) { e.printStackTrace(); }finally{ out.close(); } } } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }
testjs.js
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ $(document).ready(function(){ //$("[value=GETsubmit]").click(function(){ $("#btnSubmit").click(function(){ $.ajax({ type: "POST", url: "testservlet?name="+$("[name=name]").val()+"&pwd="+$("[name=pwd]").val()+"&date="+new Date().getTime(), dataType: "text", success: function(data){ if(data=="true"){ $("#show").html("===ok==="+"<br/>"+"name="+$("[name=name]").val()+"<br/>"+"pwd="+$("[name=pwd]").val()); }else if(data=="false"){ $("#show").html("==資訊不符合,1秒後自動跳入登陸頁面!==") //window.onload("index.jsp"); //sendRedirect(index.jsp); /* window.onload(){ sendRedirect(index.jsp); }*/ //window.location.href="https://www.baidu.com"; //window.location.href="index.jsp"; function countDown(){ setTimeout("window.location.href='index.jsp'",1000); } //執行1秒後跳轉函式 countDown(); } }, error: function(){ $("#show").html("Error XMLHttpRequest"); } }); }); });