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("  ", "

"); //设置研报fileUrl fileUrl = "http:\\//pdf.dfcfw.com/pdf/H3_" + infocode + "_1." + fileName; } else if(type == 2) { //调整公告正文格式 content = content.replaceAll("\r\n", "

"); } } 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); } } %> XXXX

<%=title %>
<%=content %>


你可能感兴趣的:(jsp中写java代码 + jstl)