JSP

一,JSP概述

定义

由Sun公司开发的,一种用于简化Servlet开发的服务器端动态页面技术的组件规范。

优点

使用jsp的好处是可以实现减少代码冗余,代码更加直观,可以做到前后端分离,是可以动态实时加载的。

二,输出编码格式

1,out输出

1,-out.print();

<body>
    两个数字相加的和:
    <%
    int a=10;
    int b=12;
    out.print(a+b);
    %>
</body>

2,-<%%>

<body>
    两个数字相加的和:
    <%
    int a=10;
    int b=12;
    %>
    <%=a+b %>
    
</body>

2,include页面包含指令

<body>
    我是第一个文件
    <br/>
    <%@include file="第二个网页的名字.jsp" %>
</body>

三,内置对象

 1,request

request是封装了客户端请求信息通过HTTP协议传送到服务器的JSP内置对象。
只在一次请求中好用,一次请求之后内容就清空了。
 
1.1  request.getParamater(String name);
返回指定请求参数的值,如果该参数不存在,则返回null
<!--第一个页面-->
<form action="my_panduan.jsp" method="post">
    姓名:<input type="text" name="username" size=12 />
    密码:<input type="password" name="pwd" size=12 />
    <input type="submit" value="注册" >
</form>

<!--第二个页面-->
<body>
<%
    String name=request.getParameter("username");
    String pwd=request.getParameter("pwd");
    if("admin".equals(name)&&"123".equals(pwd))
    {
    out.print("<h3>成功</h3>");
    }else{
    out.print("<h3>失败</h3>");
    }
%>
</body>

1.2  setAttribute(String key,Object value)

向Request域中设置属性
       getAttribute(String key)
从Request域中获取属性
<%
    request.setAttribute("username", "admin");
    request.setAttribute("pwd", "123");
    
    if(request.getAttribute("username").equals("admin")&&request.getAttribute("pwd").equals("123")){
        out.print("成功");
    }else{
        out.print("失败");
    }
%>

1.3  getRequestDispatcher(String filename).forward(request,response)

转发请求,并且将值通过request传递给另一个页面

<%
    request.setAttribute("username", "admin");
    request.setAttribute("pwd", "123");
    request.getRequestDispatcher("index.jsp").forward(request, response);
%>

2,Response

response对象是用于动态响应客户端请求,并将结果返回到浏览器中的JSP内置对象。
作用: 响应客户请求并向客户端进行输出。
2.1  getWriter()
获取用于向客户端输出数据的字符输出流

2.2  getOutputStream()

获取用于向客户端输出数据的字节输出流
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter pw=response.getWriter();
        pw.flush();
        pw.close();
}        

3,Out

<%
        out.print("异常信息:" + exception.getMessage());
        out.print("异常信息:" + exception.toString());
    %>

4,exception

out对象是通过缓冲区向客户端页面输出数据的JSP内置对象
作用:向客户端输出文本数据。
第一步:出现异常时,跳转至处理错误页面,在会出现异常的页面的第一行代码中加额外设置好的异常处理页面
errorPage="1.jsp"
第二步:声明该页面为异常处理页面,在异常处理页面第一行加
isErrorPage="true"
第三步:调用exception方法处理异常
 
<!--产生异常的类-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="1.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 a=(8/0);
%>
</body>
</html>

<!--处理异常的类-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!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>
    <h1>1234567</h1>
    <%
        out.print("异常信息:" + exception.getMessage());
        out.print("异常信息:" + exception.toString());
    %>
</body>
</html>

5,application

application是在服务器启动时产生,服务器关闭时销毁的,可以被所有用户间进行信息共享的JSP内置对象
作用:
  1.在Web应用的多个JSP、Servlet之间共享数据。
  2.访问Web应用的全局配置参数。
 
5.1  application.getContextPath()
获取项目名称
<body> 
     <%=application.getContextPath() %> 
 </body>

5.2  对象创建使用

<!--controller文件-->
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        
        <!--新建对象,存储键值对-->
        ServletContext cxt=request.getServletContext();
        cxt.setAttribute("username", "admin");
        
}

<!--jsp文件使用application调用-->
<%
if(application.getAttribute("username")!=null){
    out.print("姓名<input type="text" name="username" value=""+username+"" size=12/>");
}
else{
    out.print("no");
}
%>

四,请求转发与重定向

请求转发地址栏不变,可以携带数据

重定向地址栏发生改变就是一个跳转,不能携带数据!

 请求转发用request做的,只能响应一次就不保存数据了,重定向是用response做的

        //请求转发
        request.getRequestDispatcher("index.jsp").forward(request, response);
    
        //重定向
        response.sendRedirect("index.jsp");

五,会话请求-session

会话:是从打开浏览器访问页面、经历不中断的请求和响应、到关闭浏览器的过程。

定义:session是存放在服务器端,用于保存整个会话过程中会话数据的对象。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        
        //创建session对象
        HttpSession session=request.getSession();
        
        //向session中保存数据
        session.setAttribute("username", "张三");
        
        //从session中获取数据
        session.getAttribute("username");
        
        //从session中删除数据
        session.removeAttribute("username");
        
        PrintWriter pw=response.getWriter();
        pw.print(session.getAttribute("username"));
        
        pw.flush();
        pw.close();
    }
原文地址:https://www.cnblogs.com/lumc5/p/15268593.html