JSP指令标记和动作标记基础实践

847 阅读6分钟
原文链接: bingyishow.top

前言

使用<jsp:include/>动作标记在JSP页面中动态插入一个文件、使用<jsp:forward/>动作标记在JSP页面中实现跳转。

实验内容1

include动作标记:编写三个JSP页面,main.jsp,ladder.jsp和circle.jsp页面,将三个JSP页面保存在同一个WEB服务目录中,main.jsp页面中使用include动作标记动态插入ladder.jsp和circle.jsp页面分别计算梯形和圆形的面积。并且ladder.jsp和circle.jsp页面运行时分别获取include动作标记的param子标记传递过来的上底、下底和高以及圆半径的值。

代码

  • main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head>
        <title>计算页面</title>
    </head>

    <body>
        <% double r=2,a=3,b=5,h=4;  %>
            <BR>加载circle.jsp半径为
            <Font color=blue size=4>
                <%=r%>
            </Font>
            的圆的面积:
            <jsp:include page="circle.jsp">
                <jsp:param name="circle" value="<%=r%>" /> </jsp:include>
            <BR>加载ladder.jsp各参数为
            <Font color=blue size=4>
                <%=a%>,
                    <%=b%>,
                        <%=h%>
            </Font>
            的梯形的面积:
            <jsp:include page="ladder.jsp">
                <jsp:param name="SDi" value="<%=a%>" />
                <jsp:param name="XDi" value="<%=b%>" />
                <jsp:param name="height" value="<%=h%>" /> </jsp:include>
    </body>

    </html>
  • ladder.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head>
        <title>梯形面积</title>
    </head>

    <body>

        <%! public String getArea(double a,double b,double h)       
                    {               
                        double area=(a+b)*h/2;              
                        return ""+area;       
                    }  %>
            <%   String SDi=request.getParameter("SDi");        
                        String XDi=request.getParameter("XDi");        
                        String height=request.getParameter("height"); 
            double a=Double.parseDouble(SDi);        
            double b=Double.parseDouble(XDi);        
            double h=Double.parseDouble(height);  
            %>
                <Font color=blue size=4>

                    <%= getArea(a,b,h)%>
                </Font>

    </body>

    </html>
  • circle.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head>
        <title>圆形面积</title>
    </head>

    <body>
        <%! public String getArea(double r){
            double n=3.14*r*r;
            return ""+n;

        }
        %>

            <%   String circle=request.getParameter("circle"); 
            double r= Double.parseDouble(circle);

            %>

                <Font color=blue size=4>
                    <%=getArea(r)%>
                </Font>
    </body>

    </html>

实验内容2

这个实验内容要求太多了。总结起来要实现的功能就是:在one.jsp页面判断,用户在表单输入1~100之间的整数,提交给页面。判断如果在1~50就转向two.jsp;51~100就转向three.jsp;不合要求转向error.jsp(每个页面都包含一个head.txt的导航页)

代码

  • one.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head>
        <title>include标记</title>
        <jsp:include page="head.txt" />
    </head>

    <body bgcolor=yellow>
        <form action="" method="GET" name="form">
            请输入1~100之间的整数:<input type="text" name="number">
            <br/> <input type="submit" value="送出" name="submit">
        </form>
        <% String num=request.getParameter("number");
        if(num==null)
        num="0";
        try{
            int n=Integer.parseInt(num);
            if(n>=1 && n<=50){
        %>
            <jsp:forward page="two.jsp">
                <jsp:param name="number" value="<%=n%>" />
            </jsp:forward>
            <%    }
        else if(n>50 && n<= 100){
            %>
                <jsp:forward page="three.jsp">
                    <jsp:param name="number" value="<%=n%>" />
                </jsp:forward>
                <% }
       else if(n>100){
           %>
                    <jsp:forward page="error.jsp">
                        <jsp:param name="mess" value="<%=n%>" />
                    </jsp:forward>
                    <%  }
        }
        catch(Exception e){
            %>
                        <jsp:forward page="error.jsp">
                            <jsp:param name="mess" value="<%=e.toString()%>" />
                        </jsp:forward>

                        <% }
        %>

    </body>

    </html>
  • two.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head>
        <title>two</title>
        <jsp:include page="head.txt" />
    </head>

    <body bgcolor=yellow>
        <p>
            <font size="3" color=blue>this is two.jsp</font>
            <font size=3>
                <% String s=request.getParameter("number");
                out.println("<br/>传过来的值是" + s);
                %>
                    <br/><img src="a.jpg" width="<%=s%>" height="<%=s%>"></img>
            </font>

        </p>

    </body>

    </html>
  • three.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head>
        <title>three</title>
        <jsp:include page="head.txt" />
    </head>

    <body>

        <body bgcolor=yellow>
            <p>
                <font size="3" color=red>this is three.jsp</font>
                <font size=3>
                    <% String s=request.getParameter("number");
                            out.println("<br/>传过来的值是" + s);
                            %>
                        <br/><img src="b.jpg" width="<%=s%>" height="<%=s%>"></img>
                </font>

            </p>
        </body>

    </html>
  • error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head>
        <title>error</title>
        <jsp:include page="head.txt" />
    </head>

    <body>

        <body bgcolor=yellow>
            <p>
                <font size="5" color=red>this is error.jsp</font>
                <font size=3>
                    <% String s=request.getParameter("mess");
                                    out.println("<br/>传过来的错误信息是" + s);
                                    %>
                        <br/><img src="error.jpg" width="120" height="120"></img>
                </font>

            </p>
        </body>

    </html>
  • head.txt
    <table cellSpacing="1" cellPadding="1" width="60%" align="center" border="0">
        <tr valign="bottom">
            <td><a href="one.jsp"><font size=3>one.jsp</font></a></td>
            <td><a href="two.jsp"><font size=3>two.jsp</font></a></td>
            <td><a href="error.jsp"><font size=3>error.jsp</font></a></td>
        </tr>
    </table>

实验内容3

创建一个login.jsp文件,提交表单页面至当前页面,判断用户选择的用户类型,如果是管理员则使用forward动作标记跳转至admins.jsp页面,在admins.jsp页面判断如果管理员用户名是admin并且密码也是admin,则输出管理员登录成功!判断用户选择的用户类型,如果是普通用户则使用forward动作标记跳转至users.jsp页面,在users.jsp页面判断如果普通用户用户名是web并且密码是jsp,则输出会员登录成功!

代码

  • ligin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

    <html>

    <head>

        <title>用户登录</title>

    </head>

    <body>
        <br/>
        <form action="">
            <table border="0" width="500px">

                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>口令:</td>
                    <td> <input type="password" name="password" /></td>
                </tr>
                <tr>
                    <td>用户类型:</td>
                    <td> <input type="radio" name="a" value="admin">管理员
                    </td>
                    <td> <input type="radio" name="a" value="user">普通用户</td>

                </tr>

                <tr>
                    <td><input type="submit" value="登录" /></td>
                    <td><input type="reset" value="重置" /></td>
                </tr>
            </table>
        </form>

        <%
        request.setCharacterEncoding("UTF-8");
        String str = request.getParameter("a");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(str);
        System.out.println(username);
        System.out.println(password);
        if(str!=null&&username!=null&&password!=null){
            if(str.equals("admin")){
                %>
            <jsp:forward page="admins.jsp">
                <jsp:param value="<%= username %>" name="username" />
                <jsp:param value="<%= password %>" name="password" />
            </jsp:forward>
            <%
            }else{
                %>
                <jsp:forward page="user.jsp">
                    <jsp:param value="<%= username %>" name="username" />
                    <jsp:param value="<%= password %>" name="password" />
                </jsp:forward>
                <%
            }
        }
    %>

    </body>

    </html>
  • admins.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

    <html>

    <head>
        <title>管理界面</title>
    </head>

    <body>
        <h2>
            <%
            request.setCharacterEncoding("UTF-8");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            if(username.equals("admin")&&password.equals("admin")){
                out.write("管理员登录成功");
            }else{
                out.write("管理员登录失败");
            }
        %>
        </h2>

    </body>

    </html>
  • user.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <html>

    <head></head>

    <body>
        <h2>
            <%
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username.equals("web")&&password.equals("jsp")){
            out.write("普通用户登录成功");
        }else{
            out.write("普通用户登录失败");
        }
        %>
        </h2>

    </body>

    </html>