URL处不带.jsp显示失败
来源:1-1 初识EL表达式
mixiaofan
2019-11-13 15:30:24



第一张图是IDEA运行项目后自动开启的
第二张图URL不带.jsp
第三张图带.jsp失败
package com.imooc.el;
public class Student {
private String name;
private String 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;
}
}package com.imooc.el;
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 java.io.IOException;
@WebServlet("/info")
public class StudentServlet extends HttpServlet {
public StudentServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu = new Student();
stu.setName("子");
stu.setMobile(null);
String grade = "A";
request.setAttribute("student", stu);
request.setAttribute("grade", grade);
request.getRequestDispatcher("/info.jsp").forward(request, response);
}
}<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <welcome-file-list> <welcome-file>info.jsp</welcome-file> </welcome-file-list> </web-app>
<%@ page import="com.imooc.el.Student" %><%--
Created by IntelliJ IDEA.
User: mhr04
Date: 19.11.13
Time: 14:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Student</title>
</head>
<body>
<%-- Jsp方式输出--%>
<%
Student stu = (Student) request.getAttribute("student");
String grade = (String) request.getAttribute("grade");
out.println("<h1>"+stu.getName()+"</h1>");
out.println("<h1>"+stu.getMobile()+"</h1>");
out.println("<h1>"+grade+"</h1>");
%>
</body>
</html>1回答
好帮手慕柯南
2019-11-13
同学你好!
1.你贴出来的第二张和第三张图都是直接访问这个jsp页面,由于没有经过Servlet,所以此时的request并没有student和grade这两个属性,因此在页面获取到的stu和grade为null

2.stu和grade为null,那么通过这两个对象去获取它们的属性,肯定会报空指针异常

3.所以这里不能直接访问页面
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题