课题打卡~请老师检查

课题打卡~请老师检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <div style="width: 620px;">
        <form action="ajax/emp" method="get">
            <input style="width: 200px;" type="button" id="popular"
                name="popular" value="流行歌曲"> <input style="width: 200px;"
                type="button" id="classic" name="classic" value="经典歌曲"> <input
                style="width: 200px;" type="button" id="rock" name="rock"
                value="摇滚歌曲">
        </form>
    </div>
    <div style="width: 600px; text-align: center;">
        <div id="content"></div>
    </div>
    <script type="text/javascript" src="js/jquery-3.3.1.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#popular").click(function() {
                initAjax("ajax/song","get","flag=popular","json");
            });
            $("#classic").click(function() {
                initAjax("ajax/song","get","flag=classic","json");
            });
            $("#rock").click(function() {
                initAjax("ajax/song","get","flag=rock","json");
            });
        });
        function initAjax(url,type,data,dataType){
            $.ajax({
                "url" : url,
                "type" : type,
                "data" : data,
                "dataType" : dataType,
                "success" : function(json) {
                    $("#content").text("");
                    console.log(json.length);
                    for(var i=0;i<json.length;i++){
                        $("#content").append("<h2><b>"+json[i].name+"</b></h2>");
                    }                                           
                },
                "error" : function(xmlhttp, errorText) {
                    console.log(xmlhttp);
                    console.log(errorText);
                    if(xmlhttp.status == 400){
                        alert("无效的请求");
                    }else if(xmlhttp.status == 404){
                        alert("无法找到文件");
                    }else if(xmlhttp.status == 500){
                        alert("内部服务器错误");
                    }
                }
            });
        }
    </script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.imooc.ajax;
 
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 SongServlet
 */
@WebServlet("/ajax/song")
public class SongServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SongServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Song> song = new ArrayList<Song>();
        String flag = request.getParameter("flag");
        if (flag.equals("popular")) {
            song.add(new Song(1, "稻香"));
            song.add(new Song(2, "晴天"));
            song.add(new Song(3, "告白气球"));
        } else if (flag.equals("classic")) {
            song.add(new Song(1, "千千阙歌"));
            song.add(new Song(2, "傻女"));
            song.add(new Song(3, "七友"));
        } else if (flag.equals("rock")) {
            song.add(new Song(1, "一块红布"));
            song.add(new Song(2, "假行憎"));
            song.add(new Song(3, "新长征路上的摇滚"));
        }
        String json = JSON.toJSONString(song);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(json);
    }
 
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小尤 2020-07-09 19:03:55

同学你好,已完成练习,不过有一个小建议,建议同学下次反馈代码时,将使用到的代码全部反馈到问答区,(如:此练习中的Song类)便于老师测试代码。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 慕仙4530950 #1
    好哒,我担心代码太长,不便于老师查看呢
    2020-07-09 19:04:49
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码