JSP页面中获取并显示从后端得到的值(不使用EL标签)

输出一表格,表格中有5个学生的信息,表格包含学号,姓名,年龄,性别,地址的信息。这里的学生对象及集合对象,在Servlet中创建,转发给jsp显示。(注意:Servlet使用的是Request域传值,所以跳转方式为转发形式,JSP使用的是"原生态"语法,没有使用EL表达式,直接与Html拼接而成)
后端代码:

package com.atguigu.servlet;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.atguigu.bean.Student;public class StudetServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {List<Student> list = new ArrayList<>();list.add(new Student(1, "章登", 22, "男", "河北保定"));list.add(new Student(2, "朱一龙", 23, "男", "河南郑州"));list.add(new Student(3, "孙悟空", 500, "公", "花果山"));list.add(new Student(4, "王靖雯", 23, "女", "哈尔滨"));list.add(new Student(5, "猪八戒", 1500, "公", "高老庄"));request.setAttribute("list", list);request.getRequestDispatcher("Student.jsp").forward(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}

JSP代码:

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="com.atguigu.bean.Student" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
<style type="text/css">table{width: 500px;border: 1px solid red;border-collapse: collapse;}th , td{border: 1px solid red;}
style>
head>
<body><h1>request域取值h1><% List<Student> list = (List)request.getAttribute("list");%><table><tr><th>学号th><th>姓名th><th>年龄th><th>性别th><th>地址th>tr><% for(Student student:list){ %><%="<tr>" %><%="<td>"+student.getStuNo()+"td>"%><%="<td>"+student.getStuName()+"td>"%><%="<td>"+student.getStuAge()+"td>"%><%="<td>"+student.getStuSex()+"td>"%><%="<td>"+student.getAddress()+"td>"%><%="tr>" %><% } %>table>
body>
html>

结果:
在这里插入图片描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部