javaweb输出JavaScript代码(相当于别人引用了一个.js的文件)

html里面使用:<script language='javascript' src='http://www.yousite.com/rootpath/printWriterHtmlJavascript/PrintWriter'></script>


	    @RequestMapping(value="/PrintWriter", method={RequestMethod.GET,RequestMethod.POST},produces="text/html;charset=UTF-8")
	    @ResponseBody
	    public void PrintWriter(HttpServletRequest request,HttpServletResponse response){
	    	
	    	 //设置跨域访问
	    	 response.setHeader("Access-Control-Allow-Origin","*");
	         response.setHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE");
	         response.setHeader("Access-Control-Max-Age","3600");
	         response.setHeader("Access-Control-Allow-Headers","x-requested-with,Authorization");
	         response.setHeader("Access-Control-Allow-Credentials","true");
	         
	        //设置JavaScript方式输出
	    	response.setContentType("text/javascript;charset=utf-8");
	    	PrintWriter out =  = response.getWriter();
	    	StringBuilder sb = new StringBuilder();
	
		    //"<script type='text/javascript'>"	
			sb.append("window.onload = function(){");	    		
			sb.append("console.log('javaweb输出JavaScript代码(相当于别人引用了一个.js的文件)');");	    					
			sb.append("};");			      			     			          
		    //"</script>"

			 out.write(sb.toString());//或者out .println(mm);
			 out.flush();
             out.close();
           //自己处理异常try/catch,这里不做列举
	    	
	    }

注意请求头:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值