老师,出了点问题需要帮忙解决

老师,出了点问题需要帮忙解决

Course类:

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
package com.andreas.servlet;
 
public class Course {
    private String id; //课程编号
    private String name;//课程名
    private String category;//所属方向
    public Course() {
         
    }
     
    public Course(String id, String name, String category) {
        super();
        this.id = id;
        this.name = name;
        this.category = category;
    }
 
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    @Override
    public String toString() {
        return "课程信息: [课程编号:=" + id + ", 课程名:=" + name + ", 所属方向:" + category + "]";
    }
     
     
     
}

Elservlet :

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
package com.andreas.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;
 
/**
 * Servlet implementation class Elservlet
 */
@WebServlet("/Elservlet")
public class Elservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Elservlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      String level = request.getParameter("level");
        Course cou = new Course();
        cou.setId("001");
        cou.setName("人工智能");
        cou.setCategory("计算机科学");
        request.setAttribute("course", cou);
        request.getRequestDispatcher("/Eljsp.jsp").forward(request, response);
    }
 
}

Eljsp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.andreas.servlet.Course"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Eljsp</title>
</head>
<body>
<%-- <%
    Course co = (Course)request.getAttribute("course");
    out.println("<h1>" + co.getId() + "</h1>");
    out.println("<h1>" + co.getName() + "</h1>");
%> --%>
    <p>课程编号:${requestScope.course.id}</p>
    <p>课程名:${requestScope.course.name}</p>
    <p>所属方向:${requestScope.course.category}</p>
    <p>${course}</p>
</body>
</html>

urljsp:

1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>urljsp</title>
</head>
<body>
<h1>${param.level}</h1>
</body>
</html>

使用以前的request获取数据会空指针异常,使用El网页不显示课程信息,请老师帮忙解决一下。

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

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

1回答
好帮手慕小脸 2020-03-20 17:12:44

同学你好,测试同学的代码是正确的。并且在el浏览器上也是正确展示的。

http://img1.sycdn.imooc.com//climg/5e74884509fe0a4405500255.jpg

http://img1.sycdn.imooc.com//climg/5e74888e09b73da606230263.jpg

如果不是上述老师所阐述的问题,还请同学贴出自己的路径访问

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

  • 提问者 孬帮手慕小菜 #1
    课程信息出不来啊,不是这个
    2020-03-21 14:28:09
  • 同学你好,不是老师上述演示的话,麻烦同学把自己访问的路径截图以及运行效果。方便老师及时解决问题。 祝学习愉快~
    2020-03-21 16:04:19
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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