没有值的输出......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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( "/el_info.jsp" ).forward(request, response); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <%-- Created by IntelliJ IDEA. User: mhr04 Date: 19.11.13 Time: 15:34 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 > < h1 >姓名:${requestScope.student.name }</ h1 > < h2 >手机:${requestScope.student.mobile }</ h2 > < h2 >评价:${requestScope.grade }</ h2 > </ body > </ html > |
1 2 3 4 5 6 7 8 9 | <? 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 >el_info.jsp</ welcome-file > </ welcome-file-list > </ web-app > |
上一个提问是为什么带.jsp不能显示,这次确实不带.jsp不能显示。。。为什么啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
10
收起
正在回答 回答被采纳积分+1
3回答
好帮手慕酷酷
2019-11-13 17:27:26
同学你好,可能同学没有明白整个案例的执行过程,这个案例中需要首先要通过访问Servlet的映射路径进行给Student对象设置值,然后通过页面转发跳转,将数据传递给el_info.jsp,最后jsp中通过el表达式进行展示数据。
所以直接访问jsp是无法获取到值的,而在第二张图片中出现404,则代表路径访问错误,这里需要先访问Servlet的映射路径,也就是如下:
综上所述,路径应该是:
1 | http: //localhost:8080/el/info |
而如果先访问jsp,则代表先从jsp中传递数据给servlet。比如将页面的表单输入的值进行传递给某个servlet。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题
登录后可查看更多问答,登录/注册
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧