Jsp语法简介

1.JSP指令

  jsp指令用来设置整个JSP网页想关闭的属性,如网页的编码和脚本语言等。常用的3种指令为page,include和taglib.

2.JSP声明

  jsp声明用于声明JSP代表的Servlet类的成员变量和方法,语法:<%! %>

  例如:

  <%! int i=0;%>

<%! int a,b,c;%>

<%!

public String f(int i){

  if(i<3)

    return "i<3";

  else

    return "i>=3";

}

%>

每个JSP声明只在当前JSP页面有效。

3.Java程序片段

在JSP文件中,可以在<% 和%>标记间直接嵌入任何有效的JAVA语言代码。如何在page指令中method属性,则生成的代码默认为service方法的主体。

例如:

<%String gender="female"; if(gender.equals("female")){%>

  She is a girl.

<%}else{%>

  He is a boy.

<%}%>

以上代码等价于以下Servlet的service方法:

public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

  PrintWriter out=response.getWriter();

  String gender="female";

  if(gender.equals("female"))

    out.println("She is a girl.");

  else

    out.println("He is a boy");

}

4.jsp表达式

Jsp表达式标记为<%=和%>。该表达式的值会显示在网页上。int或者float类型的值都会自动转换成字符串加以显示。

<html>

  <head>

    <title></title>

  </head>

  <body>

    <H1>You hit the page:

    <%!int hitcount=1;%>//变量声明,相当于成员变量

    <%int count=0;

    hitcount=count++;%>//java脚本,count相当于局部变量

    <%=hitcount++%>//表达式,没有分号;

    times

    </H1>

  </body>

</html>

以上代码相当于

public class hitCounterServlet extends HttpServlet{

  private int hitcount=1;

  public void init()throws ServletException{

  }

  public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

    int count=0;

    hitcount=count++;

    PrintWriter out=response.getWriter();

  out.println("html");

  out.println("<head><title>Welcome Page</title></head>");

  out.println("<body>");

  out.println("<H1>You hit the page:"+(hitcount++)+"times</H1>");

  out.println("</body></hmtl>");

  }

  public void destroy(){

  }

}

5.jsp隐含对象(request,response,pageContext,application,out,config,page,session,exception)

6.转发JSP请求

<jsp:forward>标签用于将客户请求重定向到其它html,jsp或者servlet文件。

语法:<jsp:forward page="重新定向的文件"/>

<jsp:forward>标签从一个JSP文件向另一个文件传递包含用户请求的request对象。

如果JSP文件中包含<jsp:forward>标签,那么这个JSP文件中的所有输出数据都不会被发送到客户端,并且<jsp:forward>标签以下的代码不会被执行。

 

转载于:https://www.cnblogs.com/jishushijie/p/6111224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值