JSP 标准标签库(JSTL)的使用方法

本文介绍了一个Java Servlet与JSP结合使用的示例,展示了如何通过Servlet设置属性并在JSP页面中使用JSTL标签库进行处理。示例中,Servlet将金钱属性设置为5000,并转发至JSP页面;JSP页面则通过JSTL的set标签设置会话作用域的工资属性,并与请求转发过来的金钱属性进行比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码:

package com.java.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Request extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Request() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("money", 5000);
		
		//转发到页面
		request.getRequestDispatcher("HTML/index.jsp").forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

 

JSP代码: 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 引入C标签库 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>JSP 标准标签库(JSTL)</title>
	</head>
	<body style="text-align: center;">
		<!--
		var:变量名 
		scope:变量的作用域
		value:要储存的值
		-->
		<c:set var="salary" scope="session" value="${2000*2}"/>
		<h1>我的工资是:${ salary }</h1>
		<!-- 
		scope的作用域大小依次为:
		application > session > request > page(默认)
		
		jsp处理变量的作用域先后依次为:
		page(默认) -> request -> session -> application
		 -->
		 
		 <!-- 比较的变量money是从Request转发过来的 -->
		<c:if test="${ salary < money }">
			<h1>我的工资小于5000</h1>
		</c:if>
		
		<h1><a href="http://www.runoob.com/jsp/jsp-jstl.html" target="view_window" style="text-decoration: none; color: red;">查看更多使用方法点我</a></h1>
	</body>
</html>

 

 项目图片:

需要引用的lib里的2个jar包 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值