换行问题(out.println与<br>)

本文探讨了在JSP中实现换行的方法。通过具体示例说明如何利用HTML的&lt;br&gt;标签来确保文本在网页上正确换行。特别强调了out.println()函数在输出文本时需要附加&lt;br&gt;标签的重要性。

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

写JSP时的换行问题:

代码如下:

<!-- radiocheck.jsp -->
<%@ page contentType="text/html; charset=utf-8" language="java" %>

<html>
	<head>
	    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
		<title>单选按钮与复选框</title>
	</head>
	
	<body>
		<%
			String str1=request.getParameter("radiobutton");
			String str2[]=request.getParameterValues("check");
			if(str1!=null && str2!=null)
			{
		      //str1=new String(str1.getBytes("ISO-8859-1"),"utf-8");
				out.println("你经常使用的是"+str1+"上网<br>");
				out.println("经常上网的地方是:");
				if(str2!=null)
				{
					for(int i=0; i<str2.length; i++)
					{
						out.println(str2[i]+" ");
					}
				}
			}
		 %>
		 
		 <form name="form1" method="post" action="">
		 	<p>你使用什么方式上网</p>
		 	<p><input name="radiobutton" type="radio" value="ASDL" checked>ASDL</p>
		 	<p><input name="radiobutton" type="radio" value="拨号上网">拨号上网</p>
		 	<p><input name="radiobutton" type="radio" value="无线接入">无线接入</p>
		 	<p><input name="radiobutton" type="radio" value="DDN专线">DDN专线</p>
		 	<p>你经常在什么地方上网</p>
		 	<p><input name="check" type="checkbox" value="home">home</p>
		 	<p><input name="check" type="checkbox" value="school">school</p>
		 	<p><input name="check" type="checkbox" value="Internet Bar">Internet Bar</p>
		 	<p><input name="submit" type="submit" value="Submit"></p>
		 </form>
	</body>
</html>

 

     out.println()在web上显示出来的不换行。

     原因:out.println()相当于在页面写html代码,html代码并不能导致页面显示的换行,还需要在换行的代码后面加上<br>(html的换行是<br>)。

     因此:若要换行则需在out.println()中加上<br>。

如代码中:out.println("你经常使用的是"+str1+"上网<br>");

               out.println(str1+"br");

 

import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/cookie-session") // 映射URL为/cookie-session public class CookieSessionServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); // 1. 抓取请求中的Cookie out.println("<h2>请求中的Cookie:</h2>"); Cookie[] cookies = req.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { out.println("名称:" + cookie.getName() + ",值:" + cookie.getValue() + "<br>"); } } else { out.println("无Cookie<br>"); } // 2. 获取或创建Session(会自动生成JSESSIONID Cookie) HttpSession session = req.getSession(); out.println("<h2>Session信息:</h2>"); out.println("Session ID:" + session.getId() + "<br>"); out.println("是否新Session:" + session.isNew() + "<br>"); // 3. 设置新的Cookie(有效期30分钟,路径为当前应用) Cookie newCookie = new Cookie("user_role", "guest"); newCookie.setMaxAge(30 * 60); // 30分钟(-1为会话Cookie,0为删除) newCookie.setPath(req.getContextPath()); // 限定路径为当前应用 resp.addCookie(newCookie); out.println("<h2>已设置新Cookie:user_role=guest(有效期30分钟)</h2>"); } }请将这段代码修改成符合谷歌代码规范的格式
最新发布
08-09
"\n\n\n \n Avis de marché - Avis n° 25-20867 du 21/02/2025\n \n \n \n \n \n \n <div>\n <div>Avis de marché</div>\n </div>\n <div>Département(s) de publication : \n <strong>22</strong>\n \n </div>\n <div>\n Annonce n°\n <strong>25-20867</strong>\n </div>\n <div>Services</div>\n <hr />\n <div>Section 1 - Identification de l'acheteur</div>\n <div>\n <div>\n Nom et adresse officiels de l'organisme acheteur : Services Interhospitaliers du Tregor Goëlo</div>\n <div>\n Correspondant : MME Marie-Laure LE BERRE</div>\n <div>\n Adresse : 2 rue Louis PASTEUR,\n 22220 \n Minihy-Treguier\n </div>\n <div>\n Coordonnées :\n <div>\n <div>\n Courriel :\n Contact@si-tregorgoelo.fr\n </div>\n </div>\n </div>\n </div>\n <div>Section 2 - Description du marché</div>\n <div>\n <div>\n Objet du marché : Prestation de maintenance et assistance relative à l'infrastructure et au parc \ninformatique</div>\n <div>\n Lieu d'exécution et de livraison :2 rue Louis PASTEUR,\n 22220 \n Minihy-treguier\n </div>\n </div>\n <div>Section 8 - Mode de passation du marché</div>\n <div>\n <div>\n Type de procédure : procédure adaptée</div>\n </div>\n <div>Section 10 - Conditions de délai</div>\n <div>\n <div>\n Date limite de réception des offres : 21/03/2025 à 12:00</div>\n </div>\n <div>Section 11 - Autres renseignements</div>\n <div>\n <div>\n Numéro de référence attribué au marché par le pouvoir\n adjudicateur / l'entité adjudicatrice :\n 2025-02</div>\n </div>\n <div>Section 12 - Adresses complémentaires</div>\n <div>\n <div>\n Adresse auprès de laquelle les documents peuvent être\n obtenus :\n \n <div>\n <div>\n Coordonnées : \n <div>\n <div>\n Adresse internet :\n <a>\n https://www.achatpublic.com/sdm/ent/gen/ent_detail.do?PCSLID=CSL_2025_PzUIi1q4Pa\n </a>\n </div>\n </div>\n </div>\n </div>\n </div>\n </div>\n <hr />\n <div>\n Date d'envoi du présent avis à la publication : 21/02/2025</div>\n \n\n\n \n \n "将这串字符串使用jsoup去除html标签后,将文本中的多个连续\n替换为单个\n,原本的单个\n不动,输出代码处理后的结果
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值