JSP基础语法之一:Scriptlet使用、简单的JSP获取表单数据再输出表格


Scriptlet的三种形式:

Ps: <% %>在注释中必须转义, <%=%>不能嵌套在 <% %>中

<html>
	<!-- HTML注释(客户端可见) -->
	<%-- JSP注释(客户端不可见) --%>
	
	
<%-- 一:Scriptlet的<%!%>用法 --%>
<%! 
	/*1:是定义全局变量和常量
	x++后会变化,如果定义在/</%/%/>中,刷新则无变化*/
	int x = 0; //静态
%>
<%! 
	/*2:是定义方法和类
	如果放在/</%/%/>中,则编译会出错*/
	public int add(int x,int y)
	{
		return (x+y);
	}
%>
<%! 
	public class Person
	{
		private String name = "rt";
		
		public String toString()
		{
			return this.name;
		}
	} 
%>


<%-- 二:用Scriptlet的 /</%/=/%/> 语法,直接输出 --%>
<h3>自增前 x=> <%=x%> </h3>


<% 
	out.println("<h3>new Person()=>"+ new Person() +"</h3>");
	out.println("<h3>add(1,2)=>"+ add(1,2) +"</h3>");
	
	out.println("<h3>x++=>"+ (++x) +"</h3>");
%>

<!-- /</%/%/>在注释中必须转义,   /</%/=/%/>不能嵌套在 /</% /%/>中-->
	
</html>









html输入数据,提交到jsp做显示

<html>
	<head>
		<title> 标题 </title>
	</head>

	<body>
		<form action="print_table.jsp" method="post">
			<table border="1" width="100%">
				<tr>
					<td>输入要显示的行数</td>
					<td><input type="text" name="rows"></td>
				</tr>
				
				<tr>
					<td>输入要显示的列数</td>
					<td><input type="text" name="cols"></td>
				</tr>
				
				<tr>
				<td colspan="2">
					<input type="submit" value="提交至JSP">
					<input type="reset" value="重置">
				<td>
				<tr>
					
			</table>
		</form>
	
	</body>
	
</html>



Scriptlet:

<html>

<body>
<%
	int rows = 0;
	int cols = 0;
	try{
		rows = Integer.parseInt(request.getParameter("rows"));
		cols = Integer.parseInt(request.getParameter("cols"));
	}catch (Exception e)
	{
		out.println("<h2>输入有误</h2>");
	}
	
%>

	<table border="1" width="70%">
		<%
			for(int i=1;i<=rows;i++)
			{
		%>
			<tr>
		<%
				for(int j=1;j<=cols;j++)
				{
		%>
				<td> <%=(i*j)%> </td>
		<%
				}
		%>
			</tr>
		<%
			}
		%>
		
	</table>


</body>	































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值