jsp中写java代码 + jstl

以前没有写过jsp页面中写java代码,今天被迫用了一次

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import ="org.apache.log4j.Logger"%>
<%@ page import ="java.text.SimpleDateFormat"%>
<%@ page import ="java.sql.*"%>
<%@ page import ="com.xima.db.DBSource"%>
<%
	Connection conn = null;  
	ResultSet rs = null;
	PreparedStatement pst = null;
	Logger logger = Logger.getLogger(this.getClass());
	
    String title = "";	//标题
    String time = ""; //时间
    String sourcename = "";	//来源
    String fileUrl = "";	//附件地址
    String fileSize = "";	//附件大小
    String fileName = "";	//附件名称
    String content = "";	//正文
    String sourceName = "";	//来源
    
	int errCode = 0;
	String msg = "ok";
	int type = 0;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  //用来格式化时间

	String sql = "";
	String infocode = request.getParameter("infocode");
	
	if(infocode.startsWith("NW") || infocode.startsWith("NS")) {
		type = 1;		
		//新闻
		sql = "select A.title,A.showtime,A.source,null,null,null,A.source,B.content from info_an_newspa A,info_an_newscontentpa B where A.infocode=? and A.infocode=B.infocode";
		
	} else if(infocode.startsWith("AN")) {
		type = 2;		
		//公告
		sql = "select A.noticetitle,A.noticedate,A.sourcename,A.sourceurl,A.attachsize,A.attachname,A.sourcename,B.infobodycontent from info_an_basinfo A,info_an_content B where A.infocode=? and A.infocode=B.infocode";
	
	} else {
		type = 3;	
		//研报
		sql = "select A.REPORTTITLE,A.PUBLISHDATE,A.COMPANYNAME,A.SOURCEURL,A.ATTACHSIZE,A.ATTACHNAME,A.COMPANYNAME,B.INFOBODYCONTENT from INFO_RE_BASINFO A,INFO_RE_CONTENT B where A.infocode=? and A.infocode=B.infocode";
	}

	try {
		//获取连接
		conn = DBSource.getConnection();
		//设置手动提交
		conn.setAutoCommit(false);
		//设置事务的隔离级别。
		//在一个事务中进行查询时,不允许读取其他事务update的数据,允许读取到其他事务提交的新增数据
	    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    
	    pst = conn.prepareStatement(sql);	
	    	
	    pst.setString(1, infocode);
	    
	    rs = pst.executeQuery();
	
	    if(rs.first()) {
	    	if(type == 1) {
	    		
	    		title = rs.getString("title");
	    		time = rs.getString("showtime");
	    		sourcename = rs.getString("source");
	    		content = rs.getString("content");
	    		sourceName = rs.getString("source");
	    		
	    	} else if(type == 2) {
	    		title = rs.getString("noticetitle");
	    		time = rs.getString("noticedate");
	    		sourcename = rs.getString("sourcename");
	    		fileUrl = rs.getString("sourceurl");
	    		fileSize = rs.getString("attachsize");
	    		fileName = rs.getString("attachname");
	    		content = rs.getString("infobodycontent");
	    		sourceName = rs.getString("sourcename");

	    	} else if(type == 3) {    		
	    		title = rs.getString("reporttitle");
	    		time = rs.getString("publishdate");
	    		sourcename = rs.getString("companyname");
	    		fileUrl = rs.getString("sourceurl");
	    		fileSize = rs.getString("attachsize");
	    		fileName = rs.getString("attachname");
	    		content = rs.getString("infobodycontent");
	    		sourceName = rs.getString("companyname");
	    	}
	    }
   
	    conn.commit();
	    
	    if(!time.isEmpty()) {
		    time = time.substring(0, time.length() - 2);
	    }
	    
	    if(!fileName.isEmpty()) {
	    	String[] a = fileName.split("/");
	    	fileName = a[a.length - 1];
	    }
	    
	    if(!fileSize.isEmpty()) {
	    	fileSize = fileSize.split("\\.")[0] + "K";
	    }
	    
	    if(!sourceName.isEmpty()) {
	    	sourceName = "来源:" + sourceName;
	    }
	    
	    if(type == 3) {
	    	//调整研报正文格式
	 		content = content.replace(" ", "");
	    	content = content.replace("  ", "<p style=\"text-indent: 2em;\">");
			
	    	//设置研报fileUrl
	    	fileUrl = "http:\\//pdf.dfcfw.com/pdf/H3_" + infocode + "_1." + fileName;
	    } else if(type == 2) {
	    	//调整公告正文格式
	    	content = content.replaceAll("\r\n", "<p style=\"text-indent: 2em;\">");
	    }

	    
	} catch (Exception e) {
		
		errCode = -1;
		msg = "查询失败";
		
		logger.info(sql + "语句执行出错,准备事务回滚", e);
		
		try {
			//出错事务回滚
			conn.rollback();
		} catch (SQLException e1) {
			logger.info("事务回滚失败", e1);
		}
		
	} finally {
		try {
			//关闭连接
			pst.close();
			conn.close();
		} catch (SQLException e) {
			logger.info("关闭连接失败", e);
		}
	}	
%>

<!DOCTYPE html> 
<html> 
	<head> 
	    <meta charset="utf-8" /> 
	    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" /> 
	    <title>XXXX</title> 
	    <meta http-equiv="pragma" content="no-cache" /> 
	    <meta http-equiv="Cache-Control" content="no-cache, must-revalidate" /> 
	    <meta http-equiv="expires" content="0" /> 

	    <meta name="apple-touch-fullscreen" content="yes" /> 
	    <meta name="format-detection" content="telephone=no" /> 
	    <meta name="apple-mobile-web-app-capable" content="yes" /> 
	    <meta name="apple-mobile-web-app-status-bar-style" content="black" /> 
	    <meta http-equiv="Expires" content="-1" /> 
	    <meta http-equiv="pragram" content="no-cache" /> 
	    <link rel="stylesheet" style="text/css" href="" /> 
	    
		<style type="text/css"> 
		html {
			background-color: #172838;
		}
		
		.content-content {
			margin: 10px;
		}
			
		#title {
			font-size: 28px;
			color: #FFFFFF;
			text-align: center;
		}
			
		#info {
			font-size: 20px;
			color: #4D5E8A;
			margin-top: 30px;
		}
		
		#content {
			font-size: 23px;
			color: #94B4D8;
			margin-top: 30px;
		}	
		</style> 
	</head> 
	
	<body> 
	    <!-- header start --> 
	    <header class="page-header"> 
	    </header> 
	    <!-- header end --> 
	    <!-- content start --> 
	    <section class="page-content"> 
	    <!-- header start --> 
	    <header class="content-header"> 
	    </header> 
	    <!-- header end --> 
		<section class="content-content"> 
				
			<div id="title">
				<%=title %>
			</div>
			
			<div id="info">
				<div style="text-align:right;">
					<label style="margin-right: 25px;"><%=time %></label>
					<label style="margin-right: 5px;"><%=sourceName %></label>
				</div>
			</div>
			
			<c:if test="<%=type != 1 && fileName != null%>">
				<div id="attachInfo" style="text-align:center;">
					<a href="<%=fileUrl %>" style="color:red; font-weight:bold; text-decoration:none; font-size:23px;">【点击查看PDF全文】(<%=fileSize %>)</a>
				</div> 
			</c:if>
			
			<div id="content">
				<%=content %>
			</div>
			
		</section> 
		<!--footer start--> 
		<footer class="content-footer"> 
		</footer> 
		<!--footer end--> 
		</section> 
		<!-- content end --> 
		<!--footer start--> 
		<footer class="page-footer"> 
		</footer> 
		<!--footer end--> 
		<script type="text/javascript"></script>   
	</body> 
</html> 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值