JavaWeb之JSP零碎整理

jsp脚本

1.编写脚本的位置
<%  %>
2.输出语句
<%=%>
out.print()
3.成员变量 成员方法   在_jspService方法外面
<%! int a = 10; public void show(){}  %>

request

1.获取表单数据
String request.getParameter()
2.获取多个相同name的表单数据
String[] request.getParameterValues()
3.存取作用域属性  
Object request.getAttribute()
request.setAttribute()
4.设置字符编码
request.setCharacterEncoding()
5.请求转发
request.getRequestDispatcher().forward(request,response)

response

1.重定向
response.sendRedirect()

session

1.设置作用域属性
session.setAttribute()
2.获取作用域属性
session.getAttribute()
3.删除属性   
session.removeAttribute()
4.清除session属性
session.invalidate()
5.设置session有效期
session.setMaxInactiveInterval(int interval); 
6.获取sessionid
session.getId()

cookie

1.获取cookie    request  
Cookie[] getCookies()
2.添加cookie    response
addCookie(Cookie cookie)
3.这只cookie的有效期 s:秒
setMaxAge()

application

1.存取作用域属性
application.getAttribute()
application.setAttribute()    

四大作用域的区别

JDBC

1.加载驱动
Class.forName("com.mysql.jdbc.Driver")
2.获取连接对象
String url = "jdbc:mysql://localhost:3306/数据库名?useUnicode=true;&charactorEncoding=utf-8";
Connection conn = DriverManager.getConnection(url,"root","root")
3.获取语句执行者对象
Statement state = conn.createStatement()
state.executeQuery()
state.executeUpdate()
4.有预编译的执行者对象
PreparedStatement prep = conn.prepareStatement(String sql)   
设置参数
prep.setxxx(1,"")
5.执行sql语句获取结果集    
ResultSet rs = prep.executeQuery()
int row = prep.executeUpdate()
6.遍历结果集
rs.next()  判断是否有下一个元素
rs.getxxx()  获取元素
7.关流
xxx.close()
    
    

 

dao模式

BaseDao

1. 获取连接封装
public static  Connection getConnection() throws Exception{
        // 1.加载驱动
        Class.forName(driver);
        // 2.获取连接对象   conn.isClosed():连接对象是关闭状态也要创建对象
        if (conn == null || conn.isClosed()){
            conn = DriverManager.getConnection(url,username,password);
        }
        return conn;
        // 通过连接池技术获取连接对象 每次关闭连接对象时并不会关闭掉  而是让连接对象返回到连接池
        /*Context context = new InitialContext();
        DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/news");
        conn = dataSource.getConnection();*/

    }
2. 通用的增删改
public static int update(String sql,Object...obj) throws Exception{
        getConnection();
        ps = conn.prepareStatement(sql);
        if (obj != null){
            for (int i = 0; i < obj.length; i++) {
                ps.setObject((i+1),obj[i]);
            }
        }
        return ps.executeUpdate();
    }
3. 通用的查询
public static void select(String sql,Object...obj) throws Exception{
        getConnection();
        ps = conn.prepareStatement(sql);
        if (obj != null){
            // 设置参数
            for (int i = 0; i < obj.length; i++) {
                ps.setObject((i+1),obj[i]);
            }
        }
        rs = ps.executeQuery();
    }

4. 通用的关流
public static  void closeAll() throws Exception{
        if (rs != null){
            rs.close();
        }
        if (ps != null){
            ps.close();
        }
        if (conn != null){
            conn.close();
        }
    }

配置文件

# 链接地址
url=jdbc:mysql://localhost:3306/kgcnews?useUnicode=true&characterEncoding=utf-8
# 驱动
driver=com.mysql.jdbc.Driver
# 用户名
username=root
#密码
password=root

读取配置文件

static{
        //部署项目 -> tomcat -> 被编译后的文件
        // 读取配置文件获取io流  通过类加载器加载配置文件成为输入流
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
        Properties prop = new Properties();
        try {
            prop.load(is);// 通过输入流加载配置文件对象
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 通过key 获取值
        url = prop.getProperty("url");
        driver = prop.getProperty("driver");
        username = prop.getProperty("username");
        password = prop.getProperty("password");

    }

单例模式

1.饿汉模式 Single
	私有化构造
    private Single(){}
    私有化静态属性创建对象
    private static Single single = new Single();
    公共的获取实例的方法返回对象 
    public static Single getInstence(){
    	return single;
    }

2.懒汉模式
	私有化构造
    private Single(){}
    私有化静态属性
    private static Single single;
    公共的获取实例的方法 有线程问题加线程同步锁
    public static Single getInstence(){
    	if(single != null){
        	return single;
        }else{
        	return new Single;
        }
    	
    }

连接池

1.修改timcat中conf/context.xml

<!--
		name:自行设置  唯一
		auth:Container 固定 容器来托管数据源
		type:javax.sql.DataSource 当服务器启动的时候 会自动帮你创建一个DataSource数据源的实现类
		maxActive:最大活跃 (最大连接)
		maxIdle:最大闲置(备胎)
		maxWait:最大等待时间 
		username:数据库用户名
		password:数据库密码
		driverClassName:驱动名称
		url:数据库地址
		
		&amp; :与符号&
		useUnicode=true&amp;characterEncoding=utf-8 防止项目编码和数据库编码不一致的情况  统一采用UTF-8
	-->
    <Resource name="jdbc/news" auth="Container" type="javax.sql.DataSource"
      maxActive="100" maxIdle="30" maxWait="10000" username="root"
      password="root" driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://127.0.0.1:3306/kgcnews?
              useUnicode=true&amp;characterEncoding=utf-8" />

2.修改获取连接对象的方式

// 通过连接池技术获取连接对象 每次关闭连接对象时并不会关闭掉  而是让连接对象返回到连接池
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/news");
conn = dataSource.getConnection();

分层开发

 JSP 标签

将公共部分的代码放入到一个jsp中
就可以用以下标签引入到其他页面中 实现页面的拼接
<jsp:include page="URL">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值