jsp经典代码段

jsp页码前镶嵌的连接数据库代码段

try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/study", 
                    "root", 
                    "");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            stmt = con.prepareStatement(
            "select user_Id    from  myuser where user_name=? and user_password=MD5(?)");
            stmt.setString(1, sUser);
            stmt.setString(2,sPassword);
            //stmt.setDate(55,new java.sql.Date());
            //stmt.setInt(2, 4);
            rs = stmt.executeQuery();


            if(rs.next()){
                bSuccess=true;
            }

        */  /*System.out.println("HHHHHHHHHHHHHHHHHHH");
            if(rs!=null){
                rs.close();
                rs=null;
            }

            stmt.setInt(1, 4);
            stmt.setInt(2,4);
            rs = stmt.executeQuery();


            while(rs.next()){
                strId=rs.getString(1);
                strUserName=rs.getString(2);
                System.out.println(strId+"\t"+strUserName);
            }*/
        /*} catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //refactor重构
        //DbUtils.closeResultSet(rs);
        try {

            DbUtils.close(rs);
            if(stmt!=null){
                stmt.close();
                stmt=null;
            }

            if (con != null) {
                con.close();
                con = null;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //System.out.println(e.);
        }

判断里不断改进的代码段(经典)

String sPage="loginOk.jsp";
DefaultKeyValue<String,String> entry=new DefaultKeyValue<String,String>();
/*AbstractMap.SimpleEntry<String,String> entry=
   new AbstractMap.SimpleEntry<String,String>("message","恭喜你,登录成功!用户名:系统管理员 密码:123");

if(bOk){
    //request.setAttribute("message","恭喜你,登录成功!用户名:系统管理员 密码:123");
    //request.getRequestDispatcher("loginOk.jsp").forward(request,response);
}else{
    entry=new AbstractMap.SimpleEntry<String,String>("error",sResult);

    sPage="login.jsp";
}
*/

if(!bOk){
    entry=new DefaultKeyValue<String,String>("error",sResult);  
    sPage="login.jsp";
}//end of if...
//统一最后处理
request.setAttribute(entry.getKey(),entry.getValue());
request.getRequestDispatcher(sPage).forward(request,response);

/*
if(!StringUtils.isBlank(sUser)&& !StringUtils.isBlank(sPassword)){
    sUser=sUser.trim();

用getAttribute()获得汉字参数时,会出现乱码.可以用此方法解决—(将汉字参数转化成数字类型测数组,get以后再将数组强转成响应的汉字字符),代码如下:

    StringBuffer sb=new StringBuffer();

        for(int i=0;i
            char chTemp=sUser.charAt(i);
          sb.append((0+chTemp)).append("_");
        }//end of for...

        sb.deleteCharAt(sb.length()-1);


----------
if(c.getName().equals("userUser")){
                    sUser=c.getValue();
                    //System.out.println("33331111:"+sb);

                    //String sValue="31995_32479_31649_29702_21592";

                    String[] strArr=sUser.split("_");
                    StringBuffer sbPrototy=new StringBuffer();
                    for(String s:strArr){
                        char chTemp=(char)Integer.parseInt(s);
                        sbPrototy.append(chTemp);
                    }

                    sUser=sbPrototy.toString();
                    //System.out.println("424242:"+sbPrototy.toString());
                    break;              
                }//end of for...

计算点击首页次数代码段


int iCount=0;

Integer iObj=(Integer)application.getAttribute("count");

if(iObj!=null){ 
    iCount=iObj;    
}//end of if...

iCount++;
application.setAttribute("count",iCount);

你可能感兴趣的:(java学习)