JSP标准标签

目录

JSP标准标签

jsp:include:包含 

jsp:param:传参 

jsp:forward:转发 

 jsp:useBean、jsp:setProperty、jsp:getProperty:实例化类和设置&取

JSP标准标签

常用jsp标签(自带):本质是java代码

<jsp:include page=“页面”> </jsp:include >:包含
<jsp:param name=“name” value=“va”></jsp:param > :传参
一般和<jsp:include >一起使用
<jsp:include page=“页面”><jsp:param value=“va” name=“name”></jsp:param ></jsp:include >
<jsp:forward page=“页面”> </jsp:forward >:转发
网页会改变,但是网页地址不变,并且会将请求带过去
<jsp:useBean ></jsp:useBean >:相当于实例化类
用法:<jsp:useBean id=“” beanName=“” type=“” class=“” scope=“”>
id:对象名
class:类 创建对象时,完全限定名(包名+类名)
type:类型 调用对象时 * (可以用抽象父类或者接口)
scope:作用域 (page * request session application)
<jsp:setProperty ></jsp:setProperty >:给useBean属性设置值
用法:<jsp:setProperty name=“” property=“” value=“”>
name:useBean 的id
property:属性名(要注意必须跟实体类中的属性名保持一致)
value:属性值
<jsp:getProperty ></jsp:getProperty >:取值
和设置值差不多一样的使用方法

jsp:include:包含 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录</h1>
<jsp:include page="index.jsp"></jsp:include>
</body>
</html>

 效果如下:

 

jsp:param:传参 

前端: 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<h1>登录</h1>
<jsp:include page="index.jsp">
	<jsp:param value="1" name="type"/>
</jsp:include>
<jsp:include page="index.jsp">
	<jsp:param value="2" name="type"/>
</jsp:include>
<jsp:include page="index.jsp">
	<jsp:param value="3" name="type"/>
</jsp:include>
</body>
</html>

 后端:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
	.h1{
		border:10px solid red;
		height:100px
	}
</style>
</head>
<body>
<%
	String type=request.getParameter("type");
	//根据别人携带的参数进行判断
	String data="";
	if("1".equals(type)){
		data="英雄联盟";
	}
	if("2".equals(type)){
		data="地下城与勇士";
	}
	if("3".equals(type)){
		data="APEX英雄";
	}
%>
<h1 class="h1"><%=data %></h1>
</body>
</html>

 效果如下:

 

jsp:forward:转发 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<h1>登录</h1>
<jsp:include page="index.jsp">
	<jsp:param value="1" name="type"/>
</jsp:include>
<jsp:include page="index.jsp">
	<jsp:param value="2" name="type"/>
</jsp:include>
<jsp:include page="index.jsp">
	<jsp:param value="3" name="type"/>
</jsp:include>
<jsp:forward page="index.jsp"></jsp:forward>
</body>
</html>

 效果如下:页面跳转到了另外一个界面

 jsp:useBean、jsp:setProperty、jsp:getProperty:实例化类和设置&取值

实体类: 

package com.test.pojo;

public class User {

	private Integer userId;
	private String userName;
	private String userPwd;
	
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	
	public User() {
		// TODO Auto-generated constructor stub
	}
	
	public User(Integer userId, String userName, String userPwd) {
		super();
		this.userId = userId;
		this.userName = userName;
		this.userPwd = userPwd;
	}
    @Override
	public String toString() {
		return "User [userId=" + userId + ", userName=" + userName + ", userPwd=" + userPwd + "]";
	}
}

 text.jsp:测试

<%@page import="com.test.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 相当于java中的User user=new User(); -->
	<jsp:useBean id="user" class="com.test.pojo.User"></jsp:useBean>
<!-- 给useBean设置值,相当于user.setUserName("suibian") -->
	<jsp:setProperty property="userName" name="user" value="suibian"/>
	<jsp:setProperty property="userPwd" name="user" value="suibian"/>
<!-- 取useBean中的值,相当于user.getUserName -->
	<jsp:getProperty property="userName" name="user"/>
	<%
		out.print(user);
	%>

 效果如下(页面放大200%):

 

以上就是Java JSP标准标签的一些内容!!

 感 谢 阅 读 ……

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值