老师请检查

来源:3-6 自由编程

慕工程8205364

2021-01-13 15:37:35

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<style type="text/css">

#div{

width:100%;

text-align:center;

}

input{

width:400px;

}

</style>

</head>

<body>

<div id="div">

<input type="button" id="lx" value="流行歌曲">

<input type="button" id="jd" value="经典歌曲">

<input type="button" id="yg" value="摇滚歌曲"><br>

<div id="div1"></div>

</div>

<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>

<script type="text/javascript">

$("input[id=lx]").click(function(){

$(function(){

$.ajax({

"url":"/Ajax/gq",

"type":"get",

"data":"gm=lx",

"dataType":"json",

"success":function(json){

console.log(json);

$("#div1").html("");

var html="";

for(var i=0;i<json.length;i++){

html=html+"<h1>"+json[i]+"</h1>";

}

$("#div1").html(html);

}

})

})

})

$("#jd").click(function(){

$.ajax({

"url":"/Ajax/gq",

"type":"get",

"data":"gm=jd",

"dataType":"json",

"success":function(json){

$("#div1").html("");

for(var i=0;i<json.length;i++){

$("#div1").append("<h1>"+json[i]+"</h1>");

}

}

})

})

$("#yg").click(function(){

$(function(){

$.ajax({

"url":"/Ajax/gq",

"type":"get",

"data":"gm=yg",

"dataType":"json",

"success":function(json){

$("#div1").html("");

for(var i=0;i<json.length;i++){

$("#div1").append("<h1>"+json[i]+"</h1>");

}

}

})

})

})

</script>

</body>

</html>



package com.imooc.zy;


import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


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 com.alibaba.fastjson.JSON;


/**

 * Servlet implementation class GqServlet

 */

@WebServlet("/gq")

public class GqServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public GqServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List<String> list = new ArrayList<>();

String gm = request.getParameter("gm");

if(gm.equals("lx")) {

list.add("稻香");

list.add("晴天");

list.add("告白气球");

}else if(gm.equals("jd")) {

list.add("好汉歌");

list.add("傻女");

list.add("七友");

}else if(gm.equals("yg")) {

list.add("一块红布");

list.add("假行绅");

list.add("热爱");

}

response.setContentType("text/html;charset=UTF-8");

String json = JSON.toJSONString(list);

response.getWriter().println(json);

}


}


写回答

1回答

好帮手慕阿慧

2021-01-13

同学你好,已完成练习,棒棒哒!继续加油!祝学习愉快!

0

0 学习 · 16556 问题

查看课程