重生day03

web组件之间的跳转:forward、redirect、请求包含(include),Servlet的三大作用域对象(request,session,application)


forward跳转的特点:

1.浏览器地址不会发生改变

2.请求转发只发送一个请求

3.共享同一个请求中的数据

4.最终响应给浏览器的由s2决定

5.请求转发不能跨域名(www.baidu.com),只能在当前应用中的资源

6.请求转发可以访问WEB-INF中的资源

package forward;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/forward/s1")
public class S1Servlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		System.out.println("s1");
		request.getRequestDispatcher("/forward/s2").forward(request, response);
	}
	
}
package forward;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/forward/s2")
public class S2Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		out.println("<h1>");
		out.println(username);
		out.println("</h1>");
		
		System.out.println("s2");
	}
}



URL重定向 response.sendRedirect(url);

就一句代码就不贴代码了

特点:

1.浏览器地址会发生改变,s2

2.二个请求

3.不共享同一个请求中的数据

4.最终响应给浏览器的由s2决定(把目标地址的资源名称拷到地址栏后敲回车)

5.可以访问外部的资源比如说response.sendRedirect("http://baidu.com");

6.不可以访问WEB-INF中的资源



请求转发 request.getRequestDispatcher("/s2").include(request,repsonse);

在一个页面中包含s2的内容



面对几种跳转该如何选择

必须使用请求转发的

1:需要共享请求中的数据

2:需要访问WEB-INF中的内容

如果需要跨域访问,避免表单的重复提交,只能使用url重定向

其他情况,任选



Servlet三大作用域,主要目的是共享数据的

request                     对应的对象类型               HttpServletRequest                 每一次请求都是新的

session                                                                HttpSession                              每一次会话都是新的,不关闭浏览器累加

application       ServletContext           跟随服务器

三大作用域中的添加、得到、删除

对象作用域.setAttribute(String name,Object value);

对象作用域.getAttribute(String name);//得到的是一个object的value

对象作用域.removeAttribute(String name);



ServletContext接口和常用方法

ServletContext是随着tomcat一起启动的,只有一个,可以在多个会话中共享数据

如果获取ServletContext对象呢

Serlvet类中,super.getServletContext();

在request中,request.getServletContext();//该方法是从tomcat7 开始的

在session对象中,request.getSession().getServletContext();

常用方法:

String getRealPath(String path):根据一个资源的相对web路径,来获取绝对路径

ServletContext servletContext = request.getServletContext();
		String realPath = servletContext.getRealPath("/forward/s2");
		System.out.println(realPath);
String getContextPath();返回当前的响应的上下文路径就是设置tomcat的path
全局的初始化参数:在xml是web-app包含的,而不是Servlet包含的

String getInitParameter(String name):获取指定名称的全局初始化参数.
    Enumeration<String> getInitParameterNames():获取所有全局的初始化参数的名字

注意:
ServletConfig接口中的获取初始化参数的方法,只能获取当前Servlet自身的初始化参数,不能获取全局的初始化参数.
ServletContext接口中的获取初始化参数的方法,只能获取全局的初始化参数,不能获取局部的初始化参数.


JSP的基本语法

1、<%-- 这是jsp注释:不会翻译到Servlet --%>

2、<% 此处可以直接写java代码 %>

<%= %> //直接输出在html

<%! 这里书写的代码是定义成员变量或者方法的 %>



page指令

1:并不向客户端产生任何输出;
 2:指令在JSP整个文件范围内有效; 
 3:为翻译阶段提供了全局信息;
每一个JSP都得有page指令,一般放在JSP的第一行.

JSP三大指令 page include taglib

<%@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>
<%= new Date().toLocaleString() %>
</body>
</html>
异常页面处理 isErrorPage和errorPage 两个属性也是在page中的

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="error.jsp"%>
<!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>
<% int i = 1/0; %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<%
	String msg = exception.getMessage();
%>
出現信息:<%=msg%>
全局错误信息,用于404(页面找不到)和500(页面代码错误)的错误代码页面跳转

<?xml version="1.0" encoding="UTF-8"?>


<web-app 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_3_0.xsd"
  version="3.0"
  metadata-complete="false" >
<error-page>
	<error-code>404</error-code>
	<location>/404.jsp</location>
</error-page>
<error-page>
	<error-code>500</error-code>
	<location>/500.jsp</location>
</error-page>


</web-app>



JSP的九大内置对象和三大作用域

内置对象名称          内置对象的类型                       描述
--------------------------------------------------------------------------------------
request                 HttpServletRequest                      请求对象        
response                HttpServletResponse                     响应对象
pageContext             PageContext                             当前JSP作用域对象
session                 HttpSession                             会话对象(page指令:session="true")
exception               Throwable                               异常类型,封装当前JSP的异常信息(page指令:isErrorPage="true")
application             ServletContext                          当前Web应用对象
config                  ServletConfig                           当前Servlet的信息配置对象
out                     JspWriter                               字符输出流对象
page                    Object                                  当前Servlet对象

作用域对象名称         作用域对象的类型                       描述
--------------------------------------------------------------------------------------
pageContext(page)      PageContext                            当前JSP的作用域对象
request                HttpServletRequest                     当前请求的作用域
session                HttpSession                            当前会话的作用域
application            ServletContext                         当前Web应用的作用域



jsp的常用动作元素

<jsp:forward page="/for.jsp">
	<jsp:param  name="age" value="12"/>
</jsp:forward>
<% 
	String age = request.getParameter("age");
%>
<%=age %>
静态包含和动态包含

----------------------------------------------------------------
静态包含:使用JSP的include指令:
         <%@include file="被包含的页面文件" %>
特点:
     在翻译阶段就已经把多个JSP,合并在一起.
动态包含:使用JSP的动作指令.
         <jsp:include page="被包含的页面文件"/>
特点:
     把每一个JSP都翻译成Servlet类,在运行时期,动态的合并在一起.
----------------------------------------------------------------
什么时候使用静态包含,什么时候使用动态包含?
    如果在包含的时候,需要传递数据,此时只能使用动态包含.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值