老师,出了点问题需要帮忙解决
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网页不显示课程信息,请老师帮忙解决一下。
18
收起
正在回答 回答被采纳积分+1
1回答
相似问题
登录后可查看更多问答,登录/注册
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧