JavaWeb实验 JSP 基本语法(续

实验目的

  1. 掌握JSP基本语法;
  2. 掌握JSP常见用法。

实验内容

1】创建index.jsp、first.jsp和second.jsp三个jsp文件,页面的内容分别显示“This is my JSP page of index.jsp.”、“This is my JSP page of first.jsp.”和“This is my JSP page of second.jsp.”。然后在index.jsp中,通过include方法引进first.jsp和second.jsp两个文件,最终,访问index.jsp显示为下图的内容。

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>test</title>
</head>
<body>
	<%@ include file="./first.jsp"%> <br>
	<%@ include file="./second.jsp"%><br>
	This is my JSP page of index.jsp.<br>
</body>
</html>




【2】利用JSP的传值机制,在URL输入两个参数的值,并计算两值之间所有数字的和。

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta charset=UTF-8">
<title></title>
<%
	String x=request.getParameter("a");
	String y=request.getParameter("b");
	int xint=Integer.parseInt(x);
	int yint=Integer.parseInt(y);
		int c=xint+yint;
	out.print("a+b="+c);
%>
</head>
<body>

</body>
</html>








综合实践】结合理论课所学的内容,实现一个简单的聊天室。
 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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">
<%
	String allchat=(String)application.getAttribute("allchat");
	if(allchat==null){
		allchat="暂无消息!<br>";
	}else{
		out.println(allchat);
	}
	String sesid=session.getId();
	String chat=request.getParameter("chat");
	if(chat!=null){
		allchat=allchat+sesid+"say:"+chat+"<br>";
		out.println(chat);
		application.setAttribute("allchat",allchat);
	}
	if(allchat!=null){
		out.println(allchat);
	}
	response.setHeader("refresh","6");
%>
<title>Insert title here</title>
</head>
<body>

</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8">
<title>Chat room</title>
</head>
<body>
<iframe src="./chat.jsp" style="width:500px;height:500px,"></iframe>
<form action="" method="post"><br>
	<input type="text" style="width:200px;" name="chat"/><br>
	<input type="submit" style="width:200px;" value="发送"/>
</form>
</body>
</html>







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值