好吧,无聊的时候就发下博客,记录最近的心得 核心代码 document.cookie = sName + “=” + escape(sValue) + “;domain=.abc.com; expires=Fri, 31 Dec 2016 23:59:59 GMT; path=/” 完整代码……
分类目录:web
jsp直连mysql数据库,SQLserver数据 测试数据库可用页面
好吧,平时难免有时候连接不上数据库,最常用的方法就是写个页面看看能不能连接上数据库,现将代码记录如下: (jar包必须要有) mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<%@page contentType="text/html;charset=utf-8" import="java.sql.*%> <% String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/web"; //数据库web String user = "root"; String password = "root"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed()) out.println("数据库连接成功!"); conn.close(); } catch(ClassNotFoundException e) { out.println("找不到驱动程序"); e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } %> |
sqlserver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<%@page contentType="text/html;charset=utf-8" import="java.sql.*"%> <% String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=web"; //连接服务器和数据库web String userName = "sa"; //默认用户名 String userPwd = "sa"; //密码 Connection dbConn; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); out.println( "Connection Successful! "); //如果连接成功 页面输出 } catch (Exception e) { e.printStackTrace(); } %> |
另外,千万不要随意更改SQLSERV……
将大文本字段导出到word中
今天遇到一个需求,需要将一个大文本字段内的数据导出,以供别人进行相关中问的翻译,现在记录一下导出成word的jsp,以备后用 导出名称为:product.doc content字段类型为:longtext
1 2 |
<%@page contentType="text/html;charset=utf-8" import="java.sql.*,java.util.*,abc.jsp.database.*,abc.jsp.util.*,java.text.SimpleDateFormat,java.net.*"%> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<% PoolManager pool = new PoolManager(); if (pool == null) { pool = new PoolManager(); } Connection conn = null; try { conn = pool.getConnection(); String query = "select name,content from information"; ResultSet rs = DataManager.executeQuery(conn,query); response.setContentType("application/vnd.ms-word;charset=UTF-8"); response.setHeader("Content-disposition","attachment; filename=product.doc"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <% int m=1; while(rs!=null&&rs.next()){ %> <strong><%=m%>栏目名称:</strong><%=Common.getFormatStr(rs.getString("name"))%><br/> <strong>栏目内容:</strong><%=Common.getFormatStr(rs.getString("content"))%><br/> <hr/> <br/> <br/> <%m++;}%> </body> </html> <% } catch (Exception e) { e.printStackTrace(); } finally { pool.freeConnection(conn); } %> |
时……
jsp记录传过来的select默认值
JSP页面SQL注入简单处理
方法如下:
1 2 3 4 5 6 7 8 |
public static String getFormatPara(String str) { String formatStr = ""; if (str != null) { formatStr = str.replace("00:00:00.0", "").replace(":00.0", "").replace("'", "").replace("\"", "").replace("<", "").replace(">", "").replace("script", "").replace(" or ", "").replace( " and ", "").replace("-", "").replace("iframe", "").replace(" href ", "").replaceAll(".*([';]+|(--)+).*", " ").trim(); } return formatStr; } |
时间:2013-02-05 20:38:42
多语言版本根据浏览器版本自动跳转 并记录
现在很多网站是中英文分开的,用文件夹 一个是/zh-cn 和/en-us 这个时候我们在与zh-cn 和 en-us 放一个jsp页面 页面中放入如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<script type="text/javascript"> function GetCookie(sName) { var aCookie = document.cookie.split("; "); for (var i=0; i < aCookie.length; i++) { var aCrumb = aCookie[i].split("="); if (sName == aCrumb[0]) return unescape(aCrumb[1]); } return 0; } var cookieId=GetCookie("zone"); if(cookieId!="t"){ <% String lang = request.getHeader("Accept-Language"); if(lang!=null&&lang.length()>2){ lang = lang.substring(0,2); } if(lang!=null&&lang.equals("en")){ %> window.location.href="http://www.abc.com/en-us/"; <% }else if(lang!=null&&lang.equals("fr")){ %> window.location.href="http://www.abc.com/fr-fr/"; <% }else if(lang!=null&&lang.equals("ar")){ %> window.location.href="http://www.abc.com/ar-ar/"; <% }else if(lang!=null&&lang.equals("pt")){ %> window.location.href="http://www.abc.com/po-po/"; <% }else if(lang!=null&&lang.equals("ru")){ %> window.location.href="http://www.abc.com/be-by/"; <% } %> } </script> |
然后我们要做的就是到英文站点放到cookie中放一个zone=t… 时间你可以写的长一些,然后就可以自……