jsp九大内置对象
1.request对象
1.使用getParammeter()获取请求参数
2.获取网页请求参数
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>requset</title>
</head>
<body>
<center>
郑雅妮
<%--获取name和address请求参数--%>
<%
String name=request.getParameter("name");
String address=request.getParameter("address");
%>
<p>
欢迎<%=name%>来到 <%=address%>
</p>
</center>
</body>
</html>
地址栏输入:http://localhost:8080/web_war_exploded/
2.response对象
1.reponse对象实现页面重定向
<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="utf-8"%>
<html>
<head>
<title>response</title>
</head>
<body>
<center>
跳转页面到百度主页
<%
response.sendRedirect("https://www.baidu.com");
%>
</center>
</body>
</html>
3.application对象
1.使用application保存网站访问人数,使得多个JSP,Servlet共享数据
2.代码如下
<%@ page contentType="text/html;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>
3.地址栏输入:http://localhost:8080/web_war_exploded/
4.out对象
1.输出表格
2.代码如下
<%@ 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'>"+"zyn"+"</td>");
out.println("<td width='60'>"+"famale"+"</td>");
out.println("<td width='60'>"+"1984.8"+"</td>");
out.println("<td width='60'>"+"China"+"</td>");
out.println("</tr>");
out.println("</table>");
%>
</center>
</body>
</html>
3.地址栏输入:http://localhost:8080/web_war_exploded/index2.jsp
4.
5.config对象
1.代码如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>
jspconfigdemo
</servlet-name>
<jsp-file>/index.jsp</jsp-file>
<init-param>
<param-name>url</param-name>
<param-value>http://www.baidu.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>
jspconfigdemo
</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
</web-app>
----------
<%@ 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>
2.
6.page对象
1.代码如下
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" %>
<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>
2.地址栏输入:http://localhost:8080/web_war_exploded/page.jsp
7.exception对象
1.代码如下:
第一个jsp文件,命名为exception.jsp。
<%@ page contentType="text/html;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>
第二个jsp文件,命名为error.jsp文件。
<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>异常处理页面</title>
</head>
<body>
<hr>
异常类型:<%=exception.getClass()%><br/><br/>
异常信息:<%=exception.getMessage()%>
</hr>
</body>
</html>
2.
8.session对象
1.session1.jsp文件代码如下
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>session1</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="text/css" 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>
2.session2.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>session2</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="text/css" href="css/style.css">
</head>
<body>
<%
String name = request.getParameter("name");
session.setAttribute("name",name);
%>
<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>
3.sessionShow.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>session3</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="text/css" 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");
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>显示答案</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>