如何在JSP文件中打印Java字符串 [英] How to print a java String in a jsp file

查看:391
本文介绍了如何在JSP文件中打印Java字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我的jsp文件打印字符串变量,这是我的代码:

I'm trying to print a string variable via my jsp file, here is my code:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*;"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


<!DOCTYPE html>

<html>
<head>
<title>why are you not working</title>
<meta charset="utf-8" />
</head>

<body>
    <%
        String test = "<b><u>bold and underlined</u></b>";
     %>

    <c:set var="test1" value="<u>underlined</u>" />
    <c:set var="test2" value="${test}" />

    <c:out value="${test}" escapeXml="false" />
    <c:out value="${test1}" escapeXml="false" />
    <c:out value="${test2}" escapeXml="false" />

</body>
</html>

输出:

是否可以使用JSTL打印 test test2 ?如您在上面的代码中看到的,我已经成功打印了变量test1,但是页面上没有显示变量test或test2.

Is there a way to print test or test2 using JSTL ? As you can see in the code above I've managed to print the variable test1 but nothing appear on the page for the variables test or test2.

PS:为什么我要使用JSTL?因为它提供了一种评估html标签而不逃脱它们的方法

PS: why I want to use JSTL ? Because it offers a way to evaluate html tags and not escape them

推荐答案

是的.您可以使用

yes there is.You can set your variable test in page scope using pageContext object.

<body>
    <%
        String test = "<b><u>bold and underlined</u></b>";
        pageContext.setAttribute("test", test);
     %>

    <c:set var="test1" value="<u>underlined</u>" />
    <c:set var="test2" value="${test}" />

    <c:out value="${test}" escapeXml="false" />
    <c:out value="${test1}" escapeXml="false" />
    <c:out value="${test2}" escapeXml="false" />

</body>

输出

粗体和下划线粗体和下划线

JSTL完全适用于范围变量可以为requestsessionpage的范围变量,默认范围为page. scriplet是原始Java,它被插入到JSP页面Servlet的服务方法中. 因此,如果要访问JSTL中的任何scriplet变量,则需要在范围内进行设置.

JSTL works entirely with scoped variables where scope can be request,session or page.By default scope is page. While scriplet is raw java which is inserted into the service method of the JSP page’s servlet. So if you want to access any scriplet variable in JSTL,you need to set in scope.

这篇关于如何在JSP文件中打印Java字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆