JSP内置对象
1.request对象
获取表单请求参数
方法举例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head><body>
<form>
<hr action="requstPage.jsp" method="post" >
用户名: <br/>
<input type="text" name="name"><hr/>
男 <input type="radio" name="gender" value="男">
女 <input type="radio" name="gender" value="女"><hr/>
喜欢的颜色:<br/>
紫:<input type="checkbox" name="color" value="紫">
绿:<input type="checkbox" name="color" value="绿">
蓝:<input type="checkbox" name="color" value="蓝"><hr/>
来自的地区:<br/>
<select name="country">
<option value="内蒙古">内蒙古</option>
<option value="吉林">吉林</option>
<option value="黑龙江">黑龙江</option>
</select></hr>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
2.response对象
实现页面重定向
方法举例
<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charst=utf-8">
<title>response</title>
</head>
<body>
<center>
跳转页面到百度主页
<%//response.sendRedirect("https://www.baidu.com");%>
</center>
</body>
</html>
3.page对象
方法举例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>page对象</title>
</head>
<body>
<center>
<%
int code=page.hashCode();
String str=page.toString();
out.println("page对象的hash码"+code+" </br>");
out.println("page对象的值:"+str);
%>
</>center>
</body>
</html>
4.exception对象
方法举例
<%@ page contentType="textml;charset=UTF-8" language="java" errorPage="error.jsp" %>
<html>
<head>
<title>exception</title>
</head>
<body>
<%
int a=8;
int b=0;
int c=a/b;
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>exception对象</title>
</head>
<body>
<hr>
异常类型:<%=exception.getClass()%><br/><br/>
异常信息:<%=exception.getMessage()%>
<hr/>
</body>
</html>
5.out对象
方法举例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>out对象</title>
</head>
<body>
<center>
<hr>
<h4>表格如下</h4>
<%
out.println("<table border='2'>");
out.println("<tr>");
out.println("<td width='60'>"+"姓名"+"</td>");
out.println("<td width='40'>"+"性别"+"</td>");
out.println("<td width='80'>"+"姓出生年月"+"</td>");
out.println("<td width='60'>"+"城市"+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='60'>"+"zst"+"</td>");
out.println("<td width='60'>"+"famale"+"</td>");
out.println("<td width='60'>"+"1999.4.4"+"</td>");
out.println("<td width='60'>"+"China"+"</td>");
out.println("</tr>");
out.println("</table>");
%>
</center>
</body>
</html>
6.application对象
方法举例
<%@ page contentType="textml;charset=UTF-8" language="java" %>
<%
Integer count=(Integer) application.getAttribute("count");
if(count==null)
{count=1;}
else{count++;}
application.setAttribute("count",count);
%>
<html>
<head>
<title>application</title>
</head>
<body>
<center>
欢迎访问本网站,您是第<%=count%>位访问客户!
</center>
</body>
</html>
7.config对象
方法举例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>config对象</title>
</head>
<body>
<%
String url = config.getInitParameter("url");
String str = config.toString();
out.print("page对象的initParameter方法:"+url+"</br>");
out.print("page对象的toString方法:"+str);
%>
</body>
</html>
8.session对象
方法举例
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="texts" href="css/style.css">
</head>
<body>
<form id="form1" name="form1" method="post" action="session2.jsp">
<div align="center">
<table width="40%" border="0">
<tr>
<td width="36%"><div align="center">您的名字是:</div></td>
<td width="64%">
<label>
<div align="center">
<input type="text" name="name" />
</div>
</label>
</td>
</tr>
<tr>
<td colspan="2">
<label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'session.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="texts" href="css/style.css">
</head>
<body>
<%
String name = request.getParameter("name"); //获取用户填写的用户名
session.setAttribute("name",name); //将用户名保存在session对象中
%>
<div align="center">
<form id="form1" name="form1" method="post" action="sessionShow.jsp">
<table width="28%" border="0">
<tr>
<td>您的名字是:</td>
<td><%=name%></td>
</tr>
<tr>
<td>您最喜欢去的地方是:</td>
<td><label>
<input type="text" name="address" />
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label></td>
</tr>
</table>
</form>
<p> </p>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: hp
Date: 2020/9/16
Time: 20:20
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="texts" href="css/style.css">
</head>
<body>
<div align="center">
<%
Cookie[] ck=request.getCookies();
for(Cookie coo:ck){
String ID=coo.getValue();
System.out.println("ID:"+ID);
}
String sessionID=request.getSession(true).getId();
String name = (String)session.getAttribute("name"); //获取保存在session范围内的对象
String solution = request.getParameter("address"); //获取用户输入的最想去的地方
%>
<form id="form1" name="form1" method="post" action="">
<table width="28%" border="0">
<tr>
<td colspan="2"><div align="center"><strong>显示答案<></div> </td>
</tr>
<tr>
<td width="49%"><div align="left">您的名字是:</div></td>
<td width="51%"><label>
<div align="left"><%=name%></div> <!-- 将用户输入的用户名在页面中显示 -->
</label></td>
</tr>
<tr>
<td><label>
<div align="left">您最喜欢去的地方是:</div>
</label></td>
<td><div align="left"><%=solution%></div></td> <!-- 将用户输入的最想去的地方在页面中显示 -->
</tr>
<td><label>
<div align="left">sessionID:</div>
</label></td>
<td><div align="left"><%=sessionID%></div></td>
</tr>
</table>
</form>
<p> </p>
</div>
</body>
</html>