程序员

JSP语法学习基础版,附带对应小例子程序

2018-06-06  本文已影响18人  Allen的光影天地

directive

其中import contentType两种天天见。
这里重点说一下 errorPage 和 isErrorPage

<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="index.jsp" %>
//在第一个jsp文件中,我们指定跳转到errorPage
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
//在第二个文件中我们将isErrorPage设置为true,这样在第一个页面出错的时候,会友好的跳转到第二个页面
- 第一个jsp用于显示
<%--
  Created by IntelliJ IDEA.
  User: allenhsu
  Date: 2018/6/5
  Time: 下午5:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  %>
<html>
  <head>
    <title>compute</title>
  </head>
  <body bgcolor="#f5f5dc">
  <div align="center">
    <form method="post" action="Compute.jsp">
      <p>选择要做的计算
        <input type="radio" name="compute" value="division"  checked>
        除法
        <input type="radio" name="compute" value="multiplication">
        乘法
      </p>
      <p>被除数(被乘数)
        <input type="text" name="value1">
        除数(乘数)
        <input type="text" name="value2">
      </p>
      <p>
        <input type="submit" name="Submit" value="计算结果">
      </p>
    </form>
  </div>
  </body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: allenhsu
  Date: 2018/6/11
  Time: 下午7:07
这个例子主要说明两种include手段
第一种是:include    动态编译,会传参
第二种是:@ include 原封不动拿来
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //完善写法:检验是否为空值
    String value1 = request.getParameter("value1");
    String value2 = request.getParameter("value2");
%>

<%
    if (request.getParameter("compute").equalsIgnoreCase("division"))
    { %>
<jsp:include page="divide.jsp" flush="true">
    <jsp:param name="v1" value="<%=value1%>"/>
    <jsp:param name="v2" value="<%=value2%>"/>
</jsp:include>
<%
    } else {
%>
<!--这种方式无法传参数 -->
    <%@ include file="multiply.jsp" %>
<% } %>
<%--
  Created by IntelliJ IDEA.
  User: allenhsu
  Date: 2018/6/11
  Time: 下午7:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>multiply</title>
</head>
<body bgcolor="#7fffd4">
<center>
    <h1>
        <%
            try {
                float dividend = Float.parseFloat(request.getParameter("value1"));
                float dividsor = Float.parseFloat(request.getParameter("value2"));
                double result = dividend * dividsor;
        %>
        <%=result%>
        <%
            }
            catch (Exception e){
                out.println("不合法的被乘数或除数");
            }
        %>
    </h1>
</center>
<%--
  Created by IntelliJ IDEA.
  User: allenhsu
  Date: 2018/6/11
  Time: 下午7:18
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Divide</title>
</head>
<body bgcolor="#7fffd4">
<center>
    <h1>
        <%
            try {
                float dividend =Float.parseFloat(request.getParameter("v1"));
                float dividsor = Float.parseFloat(request.getParameter("v2"));
                float result = dividend/dividsor;
                %>
        <%=result%>
        <%
            }
            catch (Exception e){
                out.println("不合法的被乘数或除数");
            }
        %>
    </h1>
</center>
</body>
</html>
  1. forward
    1. 用途:当你输入用户名密码之后,自动跳转到对应的forward界面
    2. 带参数param的forword.jsp程序参数同样被传递
    3. 注意 :与response.sendRedirect做区别:
    • forward是服务器端在跳转,客户端不知道服务器端在背后做什么,所以显示在客户端的依旧是跳转前的URL,两个页面中拿到的request都是客户端的request,没有差异
    • response.sendRedirect有一个往返的过程,显示返回到客户端,再从客户端重新跳转到第二个页面,前后两个页面使用的是不同的request
forward相关语法
  1. jsp: useBean
    注意 在jsp中使用javabean要将java代码写入包里,不能裸着出现在src目录下
    bean相关语法
    jsp使用bean时各项参数具体含义

看个使用bean的小例子

package bean;

import java.io.Serializable;

public class CounterBean implements Serializable {
    int count;

    public CounterBean() {
    }

    public int getCount(){
        count++;
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" import="bean.*" %>
<html>
<head>
    <title>testBean</title>
</head>
<%
    CounterBean cb = new CounterBean();
%>
<body>

<div align="center">
    <font color="#dc143c" size="5">
        <%=cb.getCount()%>
    </font>
</div>
</body>
</html>
<jsp:useBean id="bg" class="bean.CounterBean"> //实例化对应的类
</jsp:useBean>

<jsp:setProperty name="bg" property="count" value="55"></jsp:setProperty>
//使用set方法
<jsp:getProperty name="bg" property="count"></jsp:getProperty>
//使用get方法

注意以下:

jsp内置对象

出个面试题大家回答一下:
请写出jsp常用内置对象以及它的常用方法:


这三个不常用

jsp和servlet之间的跳转

package Servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class servletToJsp extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
//这里的req和response是将Servlet传递过去的
            resp.setContentType("text/html;charset=utf-8");
            req.setAttribute("servletName", "ServletToJsp");
            PrintWriter out = resp.getWriter();
            out.println("this is knowledge from servlet!!");
            getServletConfig().getServletContext().getRequestDispatcher(
                    "/ServletUseJsp.jsp"
            ).forward(req, resp);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ServletUseJsp</title>
</head>
<body>
<h2>从Servlet跳转过来的</h2>
<jsp:forward page="/servlet/servletToJsp"></jsp:forward>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读