为什么没有获取到值
来源:1-1 初识EL表达式
薛定谔的猫迷
2019-08-23 10:49:15
package com.imooc.el;
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 StudentServlet
*/
@WebServlet("/info")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu= new Student();
stu.setName("张三");
stu.setMobile(null);
String grade="A";
request.setAttribute("grade",grade);
request.setAttribute("student",stu);
request.getRequestDispatcher("/info.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.imooc.el.Student"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Student stu=(Student)request.getAttribute("student");
String grade = (String)request.getAttribute("grade");
out.println("<h1>姓名:"+stu.getName()+"</h1>");
out.println("<h2>电话:"+stu.getMobile()+"</h2>");
out.println("<h2>评级:"+grade+"</h2>");
%>
</body>
</html>
package com.imooc.el;
public class Student {
String name;
String mobile;
// public Student(String name, String mobile) {
// this.name = name;
// this.mobile = mobile;
// }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}1回答
好帮手慕柯南
2019-08-23
同学的代码没有问题,在老师这里是可以显示的,同学是不是直接访问了页面呢?应该访问servlet ,比如:http://localhost:8080/el/info
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题