JSP详解

1.JSP中9大内置对象:

*request

*response

*out~=request.getWriter();

*page=this  代表JSP页面所翻译成的Servlet对象自身

*application=ServletContext

*config=ServletConfig

*session

*exception

*pageContext  此对象很特殊,因为此对象里封装了其他8个内置对象。从pageContext中可以获取。

因为从jsp翻译成的servlet文件中可以看到,在进行out.writer("***");之间有以下几句代码:

 public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;
........

少一个Exception。以上8个都是在方法的开始就声明了,所以在jsp中可以引用这些对象。


2.在web.xml中除了可以注册servlet,还可以注册jsp(其实翻译后也是servlet),需要注意的是在jsp-file标签中的内容必须以/开头。

<servlet>
	<servlet-name>xxx</servlet-name>
  	<jsp-file>/index.jsp</jsp-file>
</servlet>
  
  
<servlet-mapping>
	<servlet-name>xxx</servlet-name>
	<url-pattern>/xxx.jsp</url-pattern>
</servlet-mapping>



3.为什么不能在JSP的<%%>中定义函数。

答:因为jsp最终会被翻译成java文件,在<%%>中写的代码会被放在以下方法中,所以这时会出现在方法中写方法。

public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {}
正确的做法是:<%! public void getName(){}%>


4.在jsp中输出<%或者%>

<\%
<%="<%" %>
%>
<%="%\>" %>
从上面会发现个问题:在<%%>之间的java代码写转义符号的时候不需要两个\


5.<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>

*pageEncoding:表示当前页面用什么编码显示,也表示jsp以什么编码翻译成servlet

*contentType:表示用什么码将此servlet输出给浏览器,response.setContentType("text/html;charset=utf-8");

注意:当pageEncoding和contentType只存在其中一个的话,存在的那个的会代替另一个完成功能(指的是编码)。如果都存在的话,那就各干各事。

例如:<%@ page contentType="text/html;charset=GBK"%>

表示当前页面以GBK来显示,并且servlet送给浏览器的数据是以GBK编码


6.错误页面的处理方式之一(出错后,反馈给tomcat容器,由tomcat进行错误页面的处理):jsp能抛出的异常只有三种 RuntimeException IOException  ServletException

jsp页面代码:

<%=request.getParameter("userName").equals("xxx") %>

Web.xml中的配置:表示tomcat接收到这个异常后,将会跳转到error.jsp这个页面

<error-page>
 	<exception-type>java.lang.NullPointerException</exception-type>
  	<location>/error.jsp</location>
</error-page>

或者:表示状态码为500的时候,页面跳转到error.jsp

<error-page>
  	<error-code>500</error-code>
  	<location>/error.jsp</location>
</error-page>


错误页面的处理方式之二:(出错后,servlet内部自己处理)

JSP:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page errorPage="/a.jsp" %><!-- 表示当前jsp出错后跳转到哪个页面上去 -->
<%
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="text/css" href="styles.css">
	-->
  </head>
  
  <body>
	<%=request.getParameter("name").equals("xxx") %>
  </body>
</html>


处理错误的页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %><!-- 这句话的意思是,表示当前页面是专门处理错误页面的,这时候九大内置对象的exception就能使用了 -->
<%
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 'a.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="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    aaaaaaaaaaaaaaaaaa
    <%=exception %>
  </body>
</html>


7.jsp中的form的action前面部分的路径不建议写死,因为可能后期会进行改动,为了到时候不用一个一个的jsp去改form里的action路径,我们可以这样来操作:

1.

<form action="<%=request.getContextPath() %>/servlet/CookieTest" method="post">

2.用EL表达式${pageContext.request.contextPath}   这句话也很好理解,因为pageContext对象里封装了jsp其他8个内置对象。


8.JSP中有一句<base href="<%=basePath%>">  这句话的作用是,如果下面代码中使用到了相对路径,那么都是在basePath路径下的相对路径。


9.pageContext  request  session  application四个作用域

用pageContext删除4个作用域里的设值(可全删,也可以指定作用域删除)

指定作用域:pageContext.removeAttribute("userName",pageContext.APPLICATION_SCOPE);

pageContext.setAttribute("userName", "xxc");
request.setAttribute("userName", "xxc");
session.setAttribute("userName", "xxc");
application.setAttribute("userName", "xxc");
pageContext.removeAttribute("userName");//这句话是把四个作用域中userName的值全部删除了
System.out.println("request--->"+request.getAttribute("userName"));//null
System.out.println("session--->"+session.getAttribute("userName"));//null
System.out.println("application--->"+application.getAttribute("userName"));//null
System.out.println("pageContext--->"+pageContext.getAttribute("userName"));//nul;


用pageContext设置4个作用域里的设值(只能指定4个作用域其中一个,不能一下设置四个)

添加值到指定作用域:pageContext.setAttribute("userName", "xxc",pageContext.SESSION_SCOPE);

pageContext.setAttribute("userName", "xxc");
System.out.println("request--->"+request.getAttribute("userName"));//null
System.out.println("session--->"+session.getAttribute("userName"));//null
System.out.println("application--->"+application.getAttribute("userName"));//null
System.out.println("pageContext--->"+pageContext.getAttribute("userName")); //xxc

10.EL表达式取值:

 ${userName }是从小到大在作用域里取值的。pageContext-->request-->session-->application

EL表达式就是    pageComtext.findAttribute("userName");   表示从小到大在作用域里取值


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值