jquery和ajax实战教程_ajax可以跨域吗

jquery和ajax实战教程_ajax可以跨域吗下面介绍使用jQuery来实现一个简单的ajax实例主要的效果是使用ajax来实现书籍的价格随着书籍的数量变化,有一个增加按钮和一个减少按钮jsp页面代码<%@pagelanguage=”java”import=”java.util.*”pageEncoding=”UTF-8″%><%Stringpath=request.getContextPath();…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

下面介绍使用jQuery来实现一个简单的ajax实例

主要的效果是使用ajax来实现书籍的价格随着书籍的数量变化,有一个增加按钮和一个减少按钮

jsp页面代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>ajax测试页面5</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<style type="text/css">

#title{
   
   color: #6495ED;
   font-size: x-large;
   font-weight: bold;
   height: 40px;

}

</style>

<script type="text/javascript" src = "JS/jquery-3.3.1.js"></script>
<script type="text/javascript">
   
$(function(){

      function book(flag){
      
         var url = "AddToCarServlet";
         
         //购买书籍的数量
         var number = $("#number").text();
         
         //书籍的单价
         var price = $("#price").text();
         var args = {"number":number,"price":price,"flag":flag};
         
         $.getJSON(url,args,function(data){
         
            $("#number").empty().text(data.number);
            $("#result").empty().text(data.result);
         
         });
      
      }
      
      //减少书籍的数量
      $("#reduce").click(function(){
         
         var number = $("#number").text();  
         if(number == 0){
            alert("购买书籍的本书不能为负数!!");
            return;
         }
         
         var flag = "reduce";
         
         book(flag);
      
      });
      
       //增加书籍的数量
      $("#add").click(function(){
       
         var flag = "add";
         book(flag);
      });
      
   });


</script>
</head>
  
<body topmargin="40px">
  
<form action="" method="get">
  
  <table width="80%" align="center">
      <caption id = "title">编  程  书  籍  清  单</caption>
      <tr bgcolor="#6495ED" height="34px">
          <th>BOOK NAMA</th>
		  <th>BOOK PRICE</th>
		  <th>总价格</th>
		  <th>加入购物车</th>
	  </tr>
	  <tr bgcolor="#D1EEEE" align="center" height="34px">
	      <td>JAVA编程思想</td>
		  <td><span id = "price">60</span>&nbsp;RMB</td>
		  <td><span id = "result">0</span>&nbsp;RMB</td>
		  <td><input type="button" value="+" id = "add"><font id = "number">0</font><input type="button" value="-" id ="reduce"></td>
	  </tr>
  </table>
  
</form>
    
</body>
</html>

这是页面的初始效果图
在这里插入图片描述

处理ajax的servlet代码

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import beans.Book;

public class AddToCarServlet extends HttpServlet {

    
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//flag为add即为增加书籍,flag为reduce即为减少书籍
		String flag = request.getParameter("flag");
		
		//购买的书籍的数量
		int number = Integer.parseInt(request.getParameter("number"));
        
        //购买的书籍的单价
		int price = Integer.parseInt(request.getParameter("price"));
		
		Book book = new Book();
		
		if(flag.equals("add")){
				
			book.setNumber(number + 1);
			book.setResult(book.getNumber() * price);
		}
		else{
			
			book.setNumber(number - 1);
			book.setResult(book.getNumber() * price);
		}
		
		ObjectMapper mapper = new ObjectMapper();
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/javascript");
		
    	response.getWriter().print(mapper.writeValueAsString(book));
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}
}

与servlet对应的bean

package beans;

/**
 * 购买的书籍对应的bean
 * @author Administrator
 *
 */
public class Book {
	
	//书籍的数量
	private int number;
	
	//书籍的单价
	private int price;
	
	//书籍的总价
	private int result;

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}
	
}

最终实现的效果

在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170678.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • STL之涉及到的算法

    STL之涉及到的算法

    2021年12月9日
    41
  • Windows XP虚拟机安装全过程(VMware)「建议收藏」

    Windows XP虚拟机安装全过程(VMware)「建议收藏」​1.准备工作VMware没有装上的,可以参考一下之前装win2000的前半部分:博客链接然后电脑要安装一个迅雷,下载链接:迅雷下载链接;最后,就是大名鼎鼎的网站NextItellyou(原msdn我告诉你)的账号啦,链接:网站链接所有这些准备工作都做好之后,就可以开始下一步了~2.下载WindowsXP镜像(非百度网盘)打开NextItellyou官方网站,然后点击WindowsXP;然后点黄色箭头指向的“复制”;然后打开迅雷,它应该就会自动跳出下

    2022年8月16日
    11
  • 电信dns服务器哪个稳定,电信宽带dns设置哪个最快? dns设置哪个最好最快「建议收藏」

    电信dns服务器哪个稳定,电信宽带dns设置哪个最快? dns设置哪个最好最快「建议收藏」中国电信广州用户(包括番禺、增城、从化等区电信用户)“首选DNS服务器”为:61.144.56.101“备用DNS服务器”为:61.144.56.100这个经过测试确实是目前最快最有效的DNS服务器。2中国电信深圳用户“首选DNS服务器”为:202.96.128.86“备用DNS服务器”设置为:202.96.128.1663中国电信广东省其他地区用户(包括佛山、中山、江门、珠海、汕头等地区电信…

    2022年7月11日
    97
  • deb 安装_Ubuntu下安装软件的几种方法

    deb 安装_Ubuntu下安装软件的几种方法1,使用UbuntuSoftware进行软件安装打开UbuntuSoftware应用程序,查找要安装的软件并进行安装,方法与使用软件管家在Windows下安装软件的过程一致。2,使用APT(软件包管理系统)进行软件安装apt是一款安装包管理工具,在Ubuntu下,我们可以使用apt命令进行软件包的安装、删除等操作。例如使用apt-get安装git软件,打开终端,输入sudoapt-…

    2022年5月31日
    38
  • datagrip在线激活码[免费获取]

    (datagrip在线激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    282
  • 数据库的设计原则有哪些_数据库三原则

    数据库的设计原则有哪些_数据库三原则数据库设计是程序开发的核心部分,标准的数据库设计原则和步骤能有效提高开发进度和效率。数据库设计(DatabaseDesign)是指对于一个给定的应用环境,构造最优的数据库模式,建立数据库及其应用系统,使之能够有效地存储数据,满足各种用户的应用需求(信息要求和处理要求)。  在数据库领域内,常常把使用数据库的各类系统统称为数据库应用系统。一、数据库和信息系统  (1)数据库是信息系统的核

    2025年6月9日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号