String的不可变性

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	<%	List<String> li = new ArrayList<String>(); 
  		session.setAttribute("user", li);
  	  	li.add("wang");
  	  	List<String> li2; 
  	  	li2 = (List<String>)session.getAttribute("user");
  	  	li2.add("zi");
  		out.write(((List<String>)session.getAttribute("user")).toString());
  		
  		String name1 = "aa";
  		session.setAttribute("aaa", name1);
  		String name2 = (String)session.getAttribute("aaa"); 
  		name2 = "bb";
  		out.write((String)session.getAttribute("aaa"));


                StringBuffer name1 = new StringBuffer("aa");
  		session.setAttribute("aaa", name1);
  		StringBuffer name2 = (StringBuffer)session.getAttribute("aaa"); 
  		name2.append("bb");
  		out.write(((StringBuffer)session.getAttribute("aaa")).toString());
   %>
  </body>
</html>


运行结果:
[wang, zi] aa aabb

注:还是由于String的不可变性。

你可能感兴趣的:(java,html,jsp)