Ajax
JSONArray使用說明及示例
定義實體Goods類:
3.創建List將Goods存入list,將list存入JSONArray中,並將數組傳遞迴頁面
4.頁面顯示,通過Ajax接受數據並完成展示
$("#cont").html(content);是將content存入<tbody id="cont">的標籤內。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登錄</title>
<style type="text/css">
input{
width:250px;
height:25px;
}
#login{
width:255px;
height:35px;
background-color:#FF2611;
border:0px;
cursor:pointer;
color:white
}
.c1{
font-size:24px;
color:black;
font-weight:bolder
}
.c2{
font-size:14px;
color:#666;
}
.c3{
font-size:14px;
color:red;
}
</style>
<script type="text/javascript" src="resources/js/jquery-1.4.2.js"></script>
</head>
<body style="text-align:center;">
<%-- <form action="<%=basePath%>/LoginServlet" method="post"> --%>
<table>
<tr>
<td>
<span class="c1">歡迎登錄</span>
<span class="c2">沒有帳號?</span>
<span class="c3">立即註冊</span>
</td>
</tr>
<tr>
<td><input type="text" name="username" placeholder="請輸入登錄郵箱/手機號"><span class="tip" style="color:red;font-size:12px"></span></td>
</tr>
<tr>
<td><input type="password" name="password" placeholder="6-16位密碼,區分大小寫,不能空格"></td>
</tr>
<tr>
<td>
<!-- <input type="submit" value="登錄" id="login"> -->
<input type="button" value="登錄" id="login">
</td>
</tr>
</table>
<!-- </form> -->
</body>
<script type="text/javascript">
$("#login").click(function(){
//單擊登錄按鈕的時候觸發ajax事件
$.ajax({
url:"<%=basePath%>/LoginServlet",
type:"post",
data:{
username:$("input[name=username]").val(),
password:$("input[name=password]").val(),
},
dataType:"json",
success:function(result){
var flag = result.flag;
if(flag==true){
//如果登錄成功
window.location.href="<%=basePath%>/pages/front/success.jsp";
}else{
//跳回到Index.jsp登錄頁面,同時在登錄頁面給用戶一個提示
$(".tip").text("你輸入的用戶名或密碼不正確");
}
}
});
});
</script>
</html>
package com.imooc.servlet;
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 org.json.JSONObject;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//首先獲取jsp頁面傳遞過來的參數
String username= request.getParameter("username");
String password = request.getParameter("password");
//如果username="admin",password="123"則登錄成功
JSONObject jsonObject =null;
if("admin".equals(username) && "123".equals(password)) {
jsonObject = new JSONObject("{flag:true}");
}else{
jsonObject = new JSONObject("{flag:false}");
}
response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>js調用ajax</title>
<style type="text/css">
input{
width:250px;
height:25px;
}
#login{
width:255px;
height:35px;
background-color:#FF2611;
border:0px;
cursor:pointer;
color:white
}
.c1{
font-size:24px;
color:black;
font-weight:bolder
}
.c2{
font-size:14px;
color:#666;
}
.c3{
font-size:14px;
color:red;
}
</style>
</head>
<body style="text-align:center;">
<input type="button" value="查看java課程" flag="1" οnclick="showJava()">
<input type="button" value="查看C課程" flag="2" οnclick="showC()">
<div style="width:400px,height:300px" id="div1">
</div>
</body>
<script type="text/javascript">
function showJava(){
//1、創建一個 xmlhttpRequest對象
var xmlhttp = new XMLHttpRequest();
//2、規定請求的類型、URL 以及是否異步處理請求。
xmlhttp.open("GET","<%=basePath%>/ListCouseServlet?flag=1",true);
//3、將請求發送到服務器。
xmlhttp.send();
//4、接收服務器端的響應(readyState=4表示請求已完成且響應已就緒 status=200表示請求響應一切正常)
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//responseText:表示的是服務器返回給ajax的數據
document.getElementById("div1").innerHTML=xmlhttp.responseText;
//alert(xmlhttp.responseText);
}
}
}
function showC(){
//1、創建一個 xmlhttpRequest對象
var xmlhttp = new XMLHttpRequest();
//2、規定請求的類型、URL 以及是否異步處理請求。
xmlhttp.open("GET","<%=basePath%>/ListCouseServlet?flag=2",true);
//3、將請求發送到服務器。
xmlhttp.send();
//4、接收服務器端的響應(readyState=4表示請求已完成且響應已就緒 status=200表示請求響應一切正常)
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//responseText:表示的是服務器返回給ajax的數據
document.getElementById("div1").innerHTML=xmlhttp.responseText;
//alert(xmlhttp.responseText);
}
}
}
</script>
</html>
package cn.java.servlet;
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 org.json.JSONObject;
@WebServlet("/ListCouseServlet")
public class ListCouseServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、獲取ajax傳遞過來的參數信息
String flag = request.getParameter("flag");
//2、需要返回的數據信息
String data = "";
if("1".equals(flag)){//Java課程
data = "Java從入門到精通<br>java框架";
}else if("2".equals(flag)){//C課程
data = "C程序設計<br>C項目實戰";
}
//3、將數據信息回寫給ajax
response.getOutputStream().write(data.getBytes("utf-8"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}