导入
- jsp为简化页面的开发提供了一些内部对象。
- jsp的编写者可以直接引用这些内置对象,不需要由jsp的编写者显式声明,也不需要实例化。它们由jsp容器实现和管理,在所有jsp页面中都能使用内部对象。
- 内部对象只对Java程序片和Java表达式有用,在声明中不能使用。
一.request对象
- HTTP通信协议是用户与服务器之间一种提交(请求)信息与响应信息(request/response)的通信协议。
- 在JSP中,内置对象request封装了用户提交的信息,那么该对象调用相应的方法可以获取封装的信息,即使用该对象可以获取用户提交的信息。
- 内置对象request对象是实现了ServletRequest接口类的一个实例,可以在Tomcat服务器的webapps\tomcat-docs\servletapi中查找ServletRequest接口的方法。
1.1获取用户提交信息
1.1.1 用户通常使用HTML的form表单:
-
get 方法
get方法将请求的编码信息添加在网址后面,网址与编码信息通过"?"号分隔。get方法是浏览器默认传递参数的方法。
-
post 方法
一些敏感信息,如密码等我们可以通过post方法传递,post提交数据是隐式的。
JSP使用getParameter()来获得传递的参数,getInputStream()方法用来处理客户端的二进制数据流的请求。
例如,
demo01.1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action= "demo01.2.jsp" method= " post " >
<input type="text" name="boy" value="ok" />
<input type="submit" name="submit" value="提交" />
</form>
</body>
</html>
demo01.2jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
显示在网页
<%= request.getParameter("boy")%>
显示在控制台
<% String boy=request.getParameter("boy");
System.out.print(boy);
%>
</body>
</html>
1.2处理中文信息
- 对信息重新编码:request将获取的信息重新编码,即用ISO-8859-1进行编码,并将编码存放到一个字节数组中,然后再将这个数组转化为字符串。
如下所示:
String str=request.getParameter(“message”); byte
b[]=str.getBytes(“ISO-8859-1”); str=new String(b);
- request设置编码:request在获取信息之前使用setCharacterEncoding方法设置自己的编码为gb2312:
request.setCharacterEncoding("gb2312");
例如,utf-8
demo02.1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action= "demo02.2.jsp" method= " post " >
<input type="text" name="boy" value="可以" />
<input type="submit" name="submit" value="提交" />
</form>
</body>
</html>
demo02.2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% request.setCharacterEncoding("utf-8"); %>
<%= request.getParameter("boy")%>
</body>
</html>
1.3获取其他信息
例如,
demo03.1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action= "demo03.2.jsp" method= " post " >
<input type="text" name="boy" value="可以" />
<input type="submit" name="submit" value="提交" />
</form>
</body>
</html>
demo03.2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% String a=request.getServletPath();%>
<%=a %>
</body>
</html>
1.4处理HTML标记(不区分大小写)
1.4.1form 标记(form表单)
1.4.2input标记
request通过name指定的名字来获取GUI对象中提交的数据.
request对象需调用getParameterValues方法获取被选中的多个checkbox中value属性指定的值。
checked如果取值是一个非空的字符串,那么该单选框的初始状态就是选中状态。
用户单击form表单中的submit提交键,那么form表单所请求的页面的内置对象request调用getParameter方法将获取由value指定的值hello。
1.4.3select、option标记(下拉列表或滚动列表)
1.4.4textArea标记
1.4.5style样式标记
其它html标记可以让其id属性值是样式名称来使用这个样式
如,style标记给出样式:
1.4.6table标记(主要用于显示数据,不能提交数据)
1.4.7 image标记
不能用于提交数据,用于显示图像。
1.4.8 embed标记
不能用于提交数据。使用embed标记可以播放音乐和视频,当浏览器执行该标记时,会把浏览器所在机器上的默认播放器嵌入到浏览器中,以便播放音乐或视频文件。
或者,
例如,
demo04.1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="demo04.2.jsp" method=post>
背景图片<br>
<select id=tom name="back" size=1>
<option value="back1.png">快发1</option>
<option value="back1.png">快发2</option>
<option value="back1.png">快发3</option>
</select><br>
音乐:
<input type="radio" name="music" value="music.mp3" algin="top" checked="ok"/>打开
<input type="radio" name="music" algin="top" />关闭<br>
<h1>个人信息调查表(*为必填项目)</h1>
<br>
姓名:<input type="text" name="name" value="" />
<font color=red>*</font><br>
学历:
<select id=xl name="edu" size=1>
<option value="小学">小学</option>
<option value="初中">初中</option>
<option value="高中">高中</option>
<option value="大学及以上">大学及以上</option>
</select><br>
爱好:<br>
<input type="checkbox" name="hobby" value="游泳"/>游泳
<input type="checkbox" name="hobby" value="旅游"/>旅游
<input type="checkbox" name="hobby" value="服装"/>服装<br>
<input type="checkbox" name="hobby" value="阅读"/>阅读
<input type="checkbox" name="hobby" value="音乐"/>音乐
<input type="checkbox" name="hobby" value="购物"/>购物<br>
对本站的评价:<br>
<input type="radio" name="pj" value="好" checked="ok"/>好
<input type="radio" name="pj" value="一般" />一般
<input type="radio" name="pj" value="差" />差<br>
其他信息:
<br>
<textarea name="qtxx" id="qtxx" rows=5 cols=38></textarea>
<p>
<input type="submit" value="提交">
<input type="reset" value="重置">
</p>
</form>
</body>
</html>
demo04.2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String pic = request.getParameter("back");
String music = request.getParameter("music");
String name = request.getParameter("name");
String edu = request.getParameter("edu");
String []hobby = request.getParameterValues("hobby");
String pj = request.getParameter("pj");
String qtxx = request.getParameter("qtxx");
%>
<%
if( (name.isEmpty()) ) {
response.sendRedirect("research.jsp");
return;
}else{
%>
<table border="1" style="background-image: url('/csdn04/image/<%= pic %>'); background-repeat: no-repeat; background-size: cover;">
<tr><th>注册信息如下:</th></tr>
<tr>
<th>姓名</th>
<th><%= name %></th>
</tr> <br>
<tr>
<th>学历</th>
<th><%= edu %></th>
</tr> <br>
<tr>
<th>爱好:</th>
<th><%= (hobby != null) ? String.join(", ", hobby) : "无" %></td></th>
</tr> <br>
<tr>
<th>评价:</th>
<th><%= pj %></th>
</tr> <br>
<tr>
<th>其他信息</th>
<th><%= qtxx %></th>
</tr> <br>
<embed src="/csdn04/image/<%= music %>"></embed>
</table>
<%
}
%>
</body>
</html>
1.5 处理超链接
- 用户单击超链接标记的文字说明,可以访问超链接给出的链接页面。
- 使用超链接标记时还可以增加参数,以便向所链接的页面传递值
使用request调用getParameter(“参数”)获得超链接的参数传递过来的参数的值,即字符串。
需要注意的是,超链接标记向所链接的页面传递的参数的值,即字符串中不允许含有非ASCII字符(例如汉字等)。
1.6
二.response对象
response对象对客户的请求做出响应,向客户端发送数据。
Response方法可分为三类:
- 设定响应状态码的方法
- 设定表头的方法
- 用来URL重写的方法
2.1动态设置MIME类型
用户可以使用response对象的setContentType(String s)方法来改变contentType的属性值。
提交,另存为excel
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="demo03.2.jsp" method="post">
<button type="submit">另存为Excel</button>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% response.setContentType("application/vnd.ms-excel"); %>
</body>
</html>
2.2response的HTTP头
利用response对象的void setHeader(String name, String value)方法设置HTTP头,实现页面的自动刷新。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.time.LocalTime" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>这个页面将在5秒后自动刷新。</p>
<br>
现在的时间:
<br>
<%
out.println(""+LocalTime.now());
response.setHeader("Refresh","5");
%>
</body>
</html>
2.3response重定向
可以使用response的sendRedirect(URL url)方法实现用户的重定向。
2.4response的状态行
当服务器对用户请求进行响应时,发送的首行称为状态行。
一.二.小总结
- 在JSP中,内置对象request封装了用户提交的信息; response对象对客户的请求做出响应,向客户端发送数据
- 通过Request对象可以获得用户提交的信息、处理中文信息、以及获得其他信息
- Response对象可以动态改变contentType的值决定浏览器使用什么样的方式解释执行所收到的信息,也可以动态设置响应头、甚至重定向到新的页面。
三.session对象
3.1session对象的id
- 在Web应用中,当一个客户首次访问服务器上的某个JSP页面时,JSP引擎(比如Tomcat)将为这个客户创建一个session对象用于记录有关连接的信息。
- 这个session对象被分配了一个String类型的ID号,这个ID号存于客户端浏览器的cookie中,当客户关闭浏览器离开之后,session对象被注销。
- 换浏览器,关闭cookie,session的id都会改变。简而言之,同一个浏览器的不同页面,id不变。
利用session对象String getId() 方法获取当前session对象的ID号。
利用session对象Boolean isNew() 方法获判断是否为新的session对象。
例如,
demo05.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="servlet01" >
学号:<input type="text" name="id"/><br>
姓名:<input type="text" name="name"/><br>
年龄:<input type="text" name="age"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
servlet01.servlet
package Servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
public class servlet01 extends HttpServlet {
private static final long serialVersionUID = 1L;
public servlet01() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* 输出中文 */
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
PrintWriter out=response.getWriter();
HttpSession session = request.getSession();
//取出用户名
Object username = session.getId();
out.println(username);
out.println(id);
out.println(name);
out.println(age);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0">
<display-name>csdn04</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>servlet01</display-name>
<servlet-name>servlet01</servlet-name>
<servlet-class>Servlet.servlet01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet01</servlet-name>
<url-pattern>/servlet01</url-pattern>
</servlet-mapping>
</web-app>
3.2session对象与URL重写
response调用encodeURL()或encodeRedirectURL()方法实现URL重写。
3.3session存储数据
同一个用户在访问Web服务器期间,可以利用session对象来保存登录信息,购物车中的数据等。
- public void setAttribute(String key, Object obj)
- public Object getAttribute(String key)
- public Enumeration getAttributeNames()
- public void removeAttribute(String name)
3.4session对象的生命周期
- 客户是否关闭浏览器或服务器是否关闭。
- session对象是否调用invalidate()方法使session无效。
- session对象是否达到设置的最长“发呆”时间。
打开Tomcat安装目录中conf文件下的配置文件web.xml,找到
<session-config>
<session-timeout>30</session-timeout>
</session-config>
利用session对象的session.setMaxInactiveInterval(1)方法设置session的最大“发呆时间”。
小结
-
内置对象session由Tomcat服务器负责创建,session是实现了HttpSession接口类的一个实例 。
-
session对象被分配了一个String类型的ID,Tomcat服务器将ID发送到客户端,存放在客户的Cookie中。
-
同一用户在同一Web服务目录中各个页面的session是相同的 不同用户的session对象互不相同,具有不同的ID。
-
Session生存周期:关闭浏览器;调用invalidate()方法;超过最长的“发呆”时间;关闭服务器。
四.application对象
- 不同的客户拥有不同的session对象,application对象由多个客户端用户共享。
- application对象一直保持至服务器关闭。
4.1application对象的常用方法
Application对象由服务器负责创建,application对象被访问该Web服务目录的所有的用户共享,但不同Web服务目录下的application互不相同。
- public void setAttribute(String key, Object obj)
- public Object getAttibue(String key)
- public Enumeration getAttributeNames()
- public void removeAttribue(String key)
4.2application对象
存储在application对象中的属性可以被该Web应用程序中的所有servlet程序访问,而不管访问来自哪个客户端。
4.3 out对象
out对象是一个输出流,用来向用户端输出数据。通过out对象直接向用户端写一个由程序动态生成的html文件。
小结
- out对象是一个输出流,用来向用户端输出数据。
- 不同的客户拥有不同的session对象。与session对象不同的是application对象由多个客户端用户共享 。
- application对象一直保持至服务器关闭
- 每个Web服务目录下的application对象被访问该服务目录的所有的用户共享,但不同Web服务目录下的application互不相同。
- http是用户与服务器之间一种提交(请求)信息与响应信息(request/response)的通信协议。在JSP中,内置对象request封装了用户提交的信息,request对象获取用户提交信息的最常用的方法是getParameter(String
s);内置对象response对象对用户的请求作出动态响应,向用户端发送数据。 - http协议是一种无状态协议。一个用户向服务器发出请求(request),然后服务器返回响应(response),但不记忆连接的有关信息。所以,Tomcat服务器必须使用内置session对象(会话)记录有关连接的信息。同一个用户在某个Web服务目录中的session是相同的;同一个用户在不同的Web服务目录中的session是互不相同的;不同用户的session是互不相同的。
- 一个用户在某个Web服务目录的session对象的生存期限依赖于用户是否关闭浏览器、session对象是否调用invalidate()方法使得session无效或session对象是否达到了设置的最长的“发呆” 状态时间。
- 内置对象application由服务器负责创建,每个Web服务目录下的application对象被访问该服务目录的所有用户共享;不同Web服务目录下的application互不相同。