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" onclick="showJava()">
<input type="button" value="查看C课程" flag="2" onclick="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);
}
}