1.JSP中的内置对象有那些,是什么类型,有什么作用,有那些常用方法?
JSP中的内置对象就是服务器运行的时候预先创建好的对象,都不需要我们自己创建【new】.JSP就是一个Servlet程序。
JSP中的内置对象一共有9个。
名称 | 类型 | 作用 | 常用方法 |
request | javax.servlet.HttpServletRequest | 该对象代表了客户端的请求信息,主要用于接受通过HTTP协议传送到服务器的数据。(包括头信息、系统信息、请求方式以及请求参数等)。request对象的作用域为一次请求。 | 1.获取请求行信息的相关方法 2.获取请求消息头的相关方法 3.获取请求参数 4.通过 Request 对象传递数据 参考Servlet(3)中的HttpServletRequest 接口 |
out | javax.servlet.jsp. JspWriter | 用于在Web浏览器内输出信息 | write(“”)、println("");
|
response | javax.servlet.HttpServletResponse | response 代表的是对客户端的响应,主要是将JSP容器处理过的对象传回到客户端。response对象也具有作用域,它只在JSP页面内有效。 |
3.发送响应消息体相关的方法 参考Servlet(3)中的HttpServletResponse 接口 |
session | javax.servlet.http. HttpSession | 由服务器自动创建的与用户请求相关的对象。服务器为每个用户都生成一个session对象,用于保存该用户的信息,跟踪用户的操作状态。session对象内部使用Map类来保存数据,因此保存数据的格式为 “Key/value”。 session对象的value可以使复杂的对象类型,而不仅仅局限于字符串类型。 | 参考Servlet(4)中的HttpSession的常用方法 |
config | javax.servlet.ServletConfig | config 对象的主要作用是取得服务器的配置信息 | 参考Servlet(2)中的ServletConfig接口的常用方法 |
pageContext | javax.servlet.jsp.PageContext | pageContext 对象的作用是取得任何范围的参数,通过它可以获取 JSP页面的out、request、reponse、session、application 等对象。pageContext对象的创建和初始化都是由容器来完成的,在JSP页面中可以直接使用 pageContext对象。 | getRequest() getResponse() getSession() getOut() getPage() setAttribute(arg0, arg1, 范围) getAttribute(arg0, 范围) 范围 PageContext.APPLICATION_SCOPE PageContext.SESSION_SCOPE PageContext.REQUEST_SCOPE PageContext.PAGE_SCOPE |
page | Java.lang.Object | page 对象代表JSP本身,只有在JSP页面内才是合法的. page隐含对象本质上包含当前 Servlet接口引用的变量,类似于Java编程中的 this 指针。 | 参考Object类的方法 |
application | javax.servlet.ServletContext | application 对象可将信息保存在服务器中,直到服务器关闭,否则application对象中保存的信息会在整个应用中都有效。与session对象相比,application对象生命周期更长,类似于系统的“全局变量”。 | 参考Servlet(2)中的ServletContext接口的常用方法 |
exception | java.lang.Throwable | exception 对象的作用是显示异常信息,只有在包含 isErrorPage="true" 的页面中才可以被使用,在一般的JSP页面中使用该对象将无法编译JSP文件。在Java程序中,可以使用try/catch关键字来处理异常情况; 如果在JSP页面中出现没有捕获到的异常,就会生成 exception 对象,并把 exception 对象传送到在page指令中设定的错误页面中,然后在错误页面中处理相应的 exception 对象。
| 参考java.lang.Throwable/Exception类的常用方法 |
<%@ 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>Insert title here</title>
</head>
<body>
<center>
<table border="1px">
<tr align="center">
<td colspan="4"><h1>JSP的9大内置对象</h1></td>
</tr>
<tr align="center">
<td>名称</td>
<td>类型</td>
<td>作用</td>
<td>方法</td>
</tr>
<tr align="center">
<td>out</td>
<td>javax.servlet.jsp.JspWriter</td>
<td>用于在web浏览器内输出信息</td>
<td>write("")、println("");</td>
</tr>
<tr align="center">
<td>request</td>
<td>javax.servlet.HttpServletRequest</td>
<td>该对象代表了客户端的请求信息</td>
<td>
1.获取请求行信息的相关方法<br>
2.获取请求信息头的相关方法<br>
3.获取请求参数的相关方法<br>
4.同一个request对象传递数据的相关方法
</td>
</tr>
<tr align="center">
<td>response</td>
<td>javax.servlet.HttpServletResponse</td>
<td>代表的是对客户端的响应信息</td>
<td>
1.发送状态码相关的方法<br>
2.发送响应信息头的相关方法<br>
3.发送响应消息体的相关方法<br>
setCharacterEncoding(String charset)<br>
sendRedirect()
</td>
</tr>
<tr align="center">
<td>session</td>
<td>javax.servlet.HttpSession</td>
<td>代表的是一个会话对象【同一个用户的一组请求】</td>
<td>
1.获取HttpSession对象的基本信息相关的方法<br>
2.同一个用户的一组请求船体数据<br>
</td>
</tr>
<tr align="center">
<td>config</td>
<td>javax.servlet.ServletConfig</td>
<td>代表的服务器的配置信息</td>
<td>
getInitParameter(String name)<br>
ServletContext getServletContext()<br>
String getServletName()
</td>
</tr>
<tr align="center">
<td>application</td>
<td>javax.servlet.ServletContext</td>
<td>代表的本应用程序对象.</td>
<td>
String getInitParameter(String)<br>
InputStream getResourceAsStream(String path)
</td>
</tr>
<tr align="center">
<td>exception</td>
<td>java.lang.Throwable</td>
<td>代表的异常对象.只有在包含 isErrorPage="true" 的页面中才可以被使用</td>
<td>
java.lang.Throwable/Exception类的常用方法
</td>
</tr>
<tr align="center">
<td>page</td>
<td>java.lang.Object</td>
<td>page 对象代表JSP本身,只有在JSP页面内才是合法的.</td>
<td>
Object类的方法
</td>
</tr>
<tr align="center">
<td>pageContext</td>
<td>javax.servlet.jsp.PageContext</td>
<td>得到其他的内置对象【JSP页面的out、request、reponse、session、application 等对象】</td>
<td>
1.得到其他内置对象的方法<br>
2.设置/获取指定范围中的数据值<br>
范围<br>
1.PageContext.PAGE_SCOPE--page范围<br>
2.PageContext.REQUEST_SCOPE--request范围<br>
3.PageContext.SESSION_SCOPE--session范围<br>
4.PageContext.APPLICATION_SCOPE--application范围<br>
</td>
</tr>
</table>
</center>
</body>
</html>
1.out对象
<%@ 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>Insert title here</title>
</head>
<body>
<center>
<%!
//out.write("<h1>测试out对象</h1>");
//out.println("<h1>测试out对象</h1>");
String name = "zhangsan";
public String method(){
return "西安";
}
public class Student{
public String method2(){
return "北京";
}
}
%>
<h1>变量name==<%out.println(name); %></h1>
<h1>方法method==<%out.write(method()); %></h1>
<h1>类中的方法method2==<%out.write(new Student().method2()); %></h1>
</center>
</body>
</html>
2.request对象
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.Enumeration" %>
<!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>Insert title here</title>
</head>
<body>
<%
/*
//1.获取请求行信息的相关方法
out.println("<h2>获取请求行信息的相关方法</h2>");
out.println("<h2>getMethod : " + request.getMethod()+"</h2>");
out.println("<h2>getRequestURI:" + request.getRequestURL()+"</h2>");
out.println("<h2>getQueryString:" + request.getQueryString()+"</h2>");
out.println("<h2>getContextPath:" + request.getContextPath()+"</h2>");
out.println("<h2>getServletPath:" + request.getServletPath()+"</h2>");
out.println("<h2>getRemoteAddr : " + request.getRemoteAddr()+"</h2>");
out.println("<h2>getRemoteHost : " + request.getRemoteHost()+"</h2>");
out.println("<h2>getRemotePort : " + request.getRemotePort()+"</h2>");
out.println("<h2>getLocalAddr : " + request.getLocalAddr()+"</h2>");
out.println("<h2>getLocalName : " + request.getLocalName()+"</h2>");
out.println("<h2>getLocalPort : " + request.getLocalPort()+"</h2>");
out.println("<h2>getServerName : " + request.getServerName()+"</h2>");
out.println("<h2>getServerPort : " + request.getServerPort()+"</h2>");
out.println("<h2>getRequestURL : " + request.getRequestURL()+"</h2>");
//2.获取请求消息头的相关方法
out.println("<h2>获取请求消息头的相关方法</h2>");
//获取请求消息中的所有头字段
Enumeration headerNames = request.getHeaderNames();
//用循环遍历所有请求头,并通过 getHeader() 方法获取一个指定名称的头字段
while (headerNames.hasMoreElements()) {
//得到请求头的名称
String headerName = (String) headerNames.nextElement();
out.println("<h2>"+headerName + ":" + request.getHeader(headerName)+"</h2>");
}
*/
/*
//3.获取请求参数
out.println("<h2>获取请求参数</h2>");
String name=request.getParameter("username");
String pass=request.getParameter("password");
out.println("<h2>请求参数username=="+name+"</h2>");
out.println("<h2>请求参数password=="+pass+"</h2>");
*/
//4.同一个Request对象传递数据的相关方法
String name=request.getParameter("username");
String pass=request.getParameter("password");
request.setAttribute("name", name);
request.setAttribute("pass", pass);
request.getRequestDispatcher("/other.jsp").forward(request, response);
%>
</body>
</html>
3.response对象
<%@ 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>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("username");
String pass=request.getParameter("password");
request.setAttribute("name", name);
request.setAttribute("pass", pass);
response.sendRedirect("other.jsp");
%>
</body>
</html>
4.session对象
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
/*
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
//1.获取HttpSession对象的基本信息相关的方法
out.println("<h2>获取HttpSession对象的基本信息相关的方法</h2>");
out.println("<h2>sessionID:"+session.getId()+"</h2>");
out.println("<h2>创建时间:"+sdf.format(new Date(session.getCreationTime()))+"</h2>");
out.println("<h2>最后访问时间:"+sdf.format(new Date(session.getLastAccessedTime()))+"</h2>");
out.println("<h2>最大不活动时间:"+session.getMaxInactiveInterval()+"</h2>");
out.println("<h2>是否是一个新的:"+session.isNew()+"</h2>");
*/
//2.同一个用户的一组请求传递数据
String name=request.getParameter("username");
String pass=request.getParameter("password");
session.setAttribute("name", name);
session.setAttribute("pass", pass);
response.sendRedirect("other.jsp");
%>
</body>
</html>
5.pageContext对象
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.Enumeration" %>
<!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>Insert title here</title>
</head>
<body>
<%
//1.得到其他内置对象的方法
//JspWriter out1=pageContext.getOut();
//ServletRequest req=pageContext.getRequest();
//ServletResponse resp=pageContext.getResponse();
//Object page1=pageContext.getPage();
//Exception exception1=pageContext.getException();
//......
//设置/获取指定范围中的数据值
pageContext.setAttribute("name", "zhangsan", pageContext.PAGE_SCOPE);
pageContext.setAttribute("myname", "lisi", pageContext.REQUEST_SCOPE);
pageContext.setAttribute("testname", "wangwu", pageContext.SESSION_SCOPE);
pageContext.setAttribute("doname", "网星软件", pageContext.APPLICATION_SCOPE);
request.getRequestDispatcher("other.jsp").forward(request,response);
//response.sendRedirect("other.jsp");
%>
<h1>得到page范围中的数据值==<%= pageContext.getAttribute("name", pageContext.PAGE_SCOPE) %></h1>
<h1>得到request范围中的数据值==<%= pageContext.getAttribute("myname", pageContext.REQUEST_SCOPE) %></h1>
<h1>得到session范围中的数据值==<%= pageContext.getAttribute("testname", pageContext.SESSION_SCOPE) %></h1>
<h1>得到application范围中的数据值==<%= pageContext.getAttribute("doname", pageContext.APPLICATION_SCOPE) %></h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.util.Enumeration" %>
<!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>Insert title here</title>
</head>
<body>
<h1>得到来自pagecontext.jsp中保存在page范围中的数据===<%= pageContext.getAttribute("name", pageContext.PAGE_SCOPE) %></h1>
<h1>得到来自pagecontext.jsp中保存在request范围中的数据===<%= pageContext.getAttribute("myname", pageContext.REQUEST_SCOPE) %></h1>
<h1>得到来自pagecontext.jsp中保存在session范围中的数据===<%= pageContext.getAttribute("testname", pageContext.SESSION_SCOPE) %></h1>
<h1>得到来自pagecontext.jsp中保存在application范围中的数据===<%= pageContext.getAttribute("doname", pageContext.APPLICATION_SCOPE) %></h1>
</body>
</html>
2.JSTL[JSP的标准标签库]
2.1.EL表达式
格式:${表达式/变量/方法}
帮助我们去计算表达式的结果,并且直接将运算结果输出
注意:1.如果我们要在jsp页面中使用EL表达式语言,那么要在page指令中设置启用EL 表达式【isELIgnored="false"】
2.通过EL表达式来访问变量中保存的数据,需要将这个变量保存到request/session/application/pageContext对象中。
例如:
package com.wangxing.student.bean;
public class StudentBean {
private int stuid;
private String stuname;
private int stuage;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
}
package com.wangxing.student.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangxing.student.bean.StudentBean;
public class StudentServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = "大唐芙蓉园";
StudentBean studentBean = new StudentBean();
studentBean.setStuname("曲江池");
studentBean.setStuid(1001);
studentBean.setStuage(23);
req.setAttribute("name", name);
req.setAttribute("studentBean", studentBean);
req.getRequestDispatcher("el.jsp").forward(req, resp);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.wangxing.student.servlet.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
<%@page import="jdk.nashorn.internal.ir.RuntimeNode.Request"%>
<%@ 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>Insert title here</title>
</head>
<body>
<h1>格式:${表达式/变量/方法}</h1>
<h1>作用:帮助我们去计算表达式的结果,并且直接将运算结果输出</h1>
<h1>注意:如果我们要在jsp页面中使用EL表达式语言,那么要在page指令中设置启用EL表达式启用【isELIgnored="false"】</h1>
<h2>算术123+234 = ${123+234}</h2>
<h2>关系123>234 = ${123>234}</h2>
<h2>逻辑(123>234) && (123<234) = ${(123>234) && (123<234)}</h2>
<h1>注意:通过EL表达式来访问变量中保存的数据,需要将这个变量保存到request/session/application/pageContext对象中。</h1>
<%
String username = "诗人李白";
request.setAttribute("username", username);
%>
<h1>访问来自jsp页面中java代码块中的变量==${username }</h1>
<h1>访问来自servlet中的变量==${name }</h1>
<h1>访问来自servlet中对象的变量stuid==${studentBean.stuid }</h1>
<h1>访问来自servlet中对象的变量stuname==${studentBean.stuname}</h1>
<h1>访问来自servlet中对象的变量stuage==${studentBean.stuage }</h1>
</body>
</html>
2.2JSTL--JSP标准标签库
将一些基本的java操作代码封装成一个标签,在jsp页面中使用的时候,就可以像使用html标签一样.[简单]
1.需要添加jstl.jar和standard.jar
jstl.jar下载地址:http://repo2.maven.org/maven2/javax/servlet/jstl/
standard.jar下载地址:http://repo2.maven.org/maven2/taglibs/standard/
2.导入依赖
3.在jsp页面中通过taglib指令导入jstl标签
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
uri--标签库路径【不同的路径表示使用不同的标签库】
core---核心标签库【经常使用】
fmt---格式化标签库
functions---函数标签库
sql---sql标签库
xml---xml的标签库
prefix---是为使用标签库中的标签时定义的前缀名称,通过前缀名称来判断使用的标签库; prefix="c" 前缀名称就是c,c就是core标签库。
2.2.1<c:if> 标签.
判断表达式的值,如果表达式的值为 true 则执行其主体内容。
格式:
<c:if test="判断表达式" >
主体内容
</c:if>
package com.wangxing.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name="wangwu";
req.setAttribute("myname", name);
req.getRequestDispatcher("/test1.jsp").forward(req, resp);
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<%--
<%
String name="lisi";
if(name==""){
out.write("<h2>name变量的值为空</h2>");
}
if(name!=""){
out.write("<h2>name变量的值为"+name+"</h2>");
}
%>
--%>
<%--
<%
String name="zhangsan";
request.setAttribute("myname", name);
%>
<c:if test="${myname==''}">
<h2>name变量的值为空</h2>
</c:if>
<c:if test="${myname!=''}">
<h2>name变量的值为${myname}</h2>
</c:if>
--%>
<h2>通过jstl的便签和EL访问变量</h2>
<c:if test="${myname==''}">
<h2>name变量的值为空</h2>
</c:if>
<c:if test="${myname!=''}">
<h2>name变量的值为${myname}</h2>
</c:if>
</body>
</html>
2.2.2<c:forEach>标签
遍历集合【它迭代一个集合中的对象】
items | 要被循环的信息【集合/数组】 |
var | 代表当前条目的变量名称 |
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>Insert title here</title>
</head>
<body>
<h1><c:forEach>标签</h1>
<h1>遍历集合/数组【它迭代一个集合中的对象】</h1>
<h1>格式:</h1>
<h2><c:forEach items="${mystulist}" var="stu"></h2>
<h2>主体内容</h2>
<h2></c:forEach></h2>
<h2>items--要被循环的信息【集合/数组】</h2>
<h2>var--代表当前条目的变量名称</h2>
<%
String names[]={"javaSE","javaEE","javaME"};
request.setAttribute("names", names);
%>
<c:forEach items="${names}" var="name">
<h1>${name}</h1>
</c:forEach>
</body>
</html>
综合使用<c:if> 标签 和 <c:forEach>标签:
package com.wangxing.bean;
public class StudentBean {
private int stuid;
private String stuname;
private int stuage;
private String stuaddress;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
}
package com.wangxing.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.wangxing.bean.StudentBean;
public class StudentServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StudentBean stu1 = new StudentBean();
stu1.setStuid(1001);
stu1.setStuname("李白");
stu1.setStuage(21);
stu1.setStuaddress("杭州");
StudentBean stu2 = new StudentBean();
stu2.setStuid(1002);
stu2.setStuname("王安石");
stu2.setStuage(22);
stu2.setStuaddress("广州");
StudentBean stu3 = new StudentBean();
stu3.setStuid(1003);
stu3.setStuname("杜甫");
stu3.setStuage(23);
stu3.setStuaddress("山东");
List<StudentBean> studentlist = new ArrayList<>();
studentlist.add(stu1);
studentlist.add(stu2);
studentlist.add(stu3);
req.setAttribute("list", studentlist);
req.getRequestDispatcher("el.jsp").forward(req, resp);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.wangxing.servlet.StudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@
taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"
%>
<!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>Insert title here</title>
</head>
<body>
<table align="center" border="1px" width="300px">
<tr align="center">
<td colspan="6">学生信息总表</td>
</tr>
<tr align="center">
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>住址</td>
<td colspan="2">相关操作</td>
</tr>
<c:if test="${list.size()==0}">
<tr align="center">
<td colspan="6">没有记录</td>
</tr>
</c:if>
<c:if test="${list.size()!=0}">
<c:forEach items="${list }" var="stu">
<tr align="center">
<td>${stu.stuid }</td>
<td>${stu.stuname }</td>
<td>${stu.stuage }</td>
<td>${stu.stuaddress }</td>
<td>修改</td>
<td>删除</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>
网址:http://localhost:8080/JSP4/servlet