Java代码:
1package com.java.servlet; 2 3import java.io.IOException; 4import javax.servlet.ServletException; 5import javax.servlet.http.HttpServlet; 6import javax.servlet.http.HttpServletRequest; 7import javax.servlet.http.HttpServletResponse; 8 9public class Request extends HttpServlet { 10 private static final long serialVersionUID = 1L; 11 12 public Request() { 13 super(); 14 } 15 16 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 request.setAttribute("money", 5000); 18 19 //转发到页面 20 request.getRequestDispatcher("HTML/index.jsp").forward(request, response); 21 } 22 23 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 doGet(request, response); 25 } 26 27}
JSP代码:
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<!-- 引入C标签库 --> 4<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 5<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6<html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>JSP 标准标签库(JSTL)</title> 10 </head> 11 <body style="text-align: center;"> 12 <!-- 13 var:变量名 14 scope:变量的作用域 15 value:要储存的值 16 --> 17 <c:set var="salary" scope="session" value="${2000*2}"/> 18 <h1>我的工资是:${ salary }</h1> 19 <!-- 20 scope的作用域大小依次为: 21 application > session > request > page(默认) 22 23 jsp处理变量的作用域先后依次为: 24 page(默认) -> request -> session -> application 25 --> 26 27 <!-- 比较的变量money是从Request转发过来的 --> 28 <c:if test="${ salary < money }"> 29 <h1>我的工资小于5000</h1> 30 </c:if> 31 32 <h1><a href="http://www.runoob.com/jsp/jsp-jstl.html" target="view_window" style="text-decoration: none; color: red;">查看更多使用方法点我</a></h1> 33 </body> 34</html>
项目图片:

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