JSP内置对象request、response

内置对象(又叫隐含对象,有9个内置对象):不需要与现声明就可以在脚本代码和表达式中随意使用

只要我们知道他对应的变量的名称

作用域:pageContext、request、session、application

<%@ page language="java" contentType="text/html; charset=UTF-8" errorPage="error.jsp"
    pageEncoding="UTF-8" import="com.imooc.db.*,com.imooc.bean.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>  
	
	<!-- 获取账号以及密码,并且需要去调用DBUtil当中的方法来判断是否存在指定的信息 
		1、如果正确,DBUtil方法返回的值是true,显示成功页面
		2、如果错误,false,显示错误消息
		request:获取请求信息,包括请求信息。
		getParameter(String name):可以通过一个控件的name属性来获取控件的值
		
		out:输出流对象,输出指定的信息。
		println();
	-->
	<h3 align="center">响应的字符编码集:<%= response.getCharacterEncoding() %></h3>
	
	<% 
		String account = request.getParameter("account");
	
		String password = request.getParameter("password");
		
		Emp emp = new Emp(account,null,password,null);
		boolean flag = DBUtil.selectEmpByAccountAndPassword(emp);
		Map<String,Emp> map = DBUtil.map;
		if(flag == true){
			Object o = application.getAttribute("count");
			if(o == null){
				application.setAttribute("count", 1);
				
			}else{
				int count = Integer.parseInt(o.toString());
				application.setAttribute("count", count + 1);
				
			}
			session.setAttribute("account", account);
	%>
			<h3 align="right">访问量:<%= application.getAttribute("count") %></h3>
			<h3 align="right">登录账户:<%= session.getAttribute("account") %></h3>
			<h3 align="center">欢迎来到人事管理系统的首页</h3>
			<hr>
			<table align="center" border="1" width="500px">
				<tr>
					<td>
						账号
					</td>
					<td>
						员工姓名
					</td>
					<td>
						邮箱
					</td>
					<td>
						修改
					</td>
				</tr>
				<%
					for(String key : map.keySet()){4
						Emp e = map.get(key);
						%>
							<tr>
								<td>
									<%= e.getAccount() %>
								</td>
								<td>
									<%= e.getName() %>
								</td>
								<td>
									<%= e.getEmail() %>
								</td>
								<td>
									<!-- 
									相邻两个JSP页面传递数据的时候。通过URL参数的方式来传递数据。
									规则:
									资源?key=value&key=value
									 -->
									<a href="update.jsp?account=<%= e.getAccount() %>&name=<%= e.getName() %>&email=<%= e.getEmail() %>">修改</a>
								</td>
							</tr>
						<%
						
					}
				%>
			</table>
	<%
		}else{
			throw new Exception("账号和密码错误");
		}
	%>
</body>
</html>

这是一个登陆之后响应的完整的代码

这个发生在一个网页设置了request之后进行了请求的转发,运行这个网页之后会会直接跳转到request转发之后的网页

这也是request的共享

NewFile.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
  request.setAttribute("","");
  request.getRequestDispatcher("NewFile1.jsp").forward( request,response);
%>
   我是原网页
</body>
</html>

用forward传递请求响应的

NewFile1.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
       我是跳转的网页
</body>
</html>

这样之后我们运行第一个网页,原本是显示“我是原网页”,经过跳转,在本网页显示“我是跳转的网页”

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值