阿里面试题目总结

1、StringBuffer是线程安全的,StringBuilder不是线程安全的。

2、数据在各个网络层之间是怎么传输的?数据在各层之间的单位都是不一样的,在物理层数据的单位称为比特(bit);在数据链路层,数据的单位称为帧(frame);在网络层,数据的单位称为数据包(packet);传输层,数据的单位称为数据段(segment)。 

Hashtable、HashMap

Hashmap 是一个最常用的Map,它根据键的HashCode 值,重新计算hashmap中要使用的哈希值作为key,然后根据新计算的key存储数据。根据这个新计算的key,可以直接获取它的值,具有很快的访问速度。

Hashtable 与 HashMap类似,但是主要有6点不同。

1.HashTable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。

2.HashTable不允许null值,key和value都不可以,HashMap允许null值,key和value都可以。HashMap允许 key值只能由一个null值,因为hashmap如果key值相同,新的key, value将替代旧的。

3.HashTable有一个contains(Object value)功能和containsValue(Object value)功能一样。

4.遍历的时候,HashTable使用Enumeration,HashMap使用Iterator。

5.HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数。

6.哈希值的使用不同,HashTable直接使用对象的hashCode。

Hashtable继承自Dictionary类,实现了Map接口。而HashMap是继承自AbstractMap,实现了Map接口。

session和cookie的区别:

session放在服务器,cookie放在客户端

session不区分路径,在同一个用户在访问一个网站期间,所有的session在任何一个地方都可以访问到。而cookie中如果设置了路径参数,那么同一个网站中不同路径下的cookie互相是访问不到的。也就是说,同一个用户的cookie,他换了浏览器,就访问不到之前的那个不同牌子的浏览器的cookie了。session中保存的是对象,cookie中保存的是字符串。 

由于采用服务器端保持状态的方案在客户端也需要保存一个标识,所以session机制可能需要借助于cookie机制来达到保存标识的目的,但实际上它还有其他选择【经常被使用的一种技术叫做URL重写,就是把session id直接附加在URL路径的后面。还有一种技术叫做表单隐藏字段】。

servlet的生命周期以及servlet和cgi的区别
      Servlet被服务器实例化后,容器运行其init方法,请求到达时运行其service方法,service方法自动派遣运行与请求对应的doXXX方法(doGet,doPost)等,当服务器决定将实例销毁的时候调用其destroy方法。
      与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而cgi对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。
-----------------------------------------------------------

struts的工作流程。 


上图可以看出,struts简化了用户多少的操作。

1、容器启动的时候加载actionservlet

2、请求被提交到一系列(主要是三层)的过滤器(Filter),如(ActionContextCleanUp、其他过滤器(SiteMesh等)、 FilterDispatcher)。注意这里是有顺序的,先ActionContextCleanUp,再其他过滤器(SiteMesh等)、最后到FilterDispatcher。

3、FilterDispatcher是控制器的核心,就是mvc中c控制层的核心。FilterDispatcher询问ActionMapper是否需要调用某个Action来处理这个(request)请求,如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy。

4、ActionProxy通过Configuration Manager(struts.xml)询问框架的配置文件,找到需要调用的Action类

5、ActionProxy创建一个ActionInvocation的实例,同时ActionInvocation通过代理模式调用Action。但在调用之前ActionInvocation会根据配置加载Action相关的所有Interceptor。

Interceptor 的调度流程大致如下:
1. ActionInvocation初始化时,根据配置,加载Action相关的所有Interceptor。
2. 通过ActionInvocation.invoke方法调用Action实现时,执行Interceptor。

一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。但大部分时候都是返回另外一个action,那么流程又得走一遍……

-------------------------------------------------------------------------------

Statement与PreparedStatement的区别

1。PreparedStatement是预编译的,对于批量处理可以大大提高效率,也叫JDBC存储过程
2。使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。

statement每次执行sql语句,相关数据库都要执行sql语句的编译。

preparedstatement是预编译得,  preparedstatement支持批处理。

The best reasons for using PreparedStatements are these:

(1) Executing the same query multiple times in loop, binding different parameter values each time, and
(2) Using the setDate()/setString() methods to escape dates and strings properly, in a database-independent way.

The second one compels me to use PreparedStatement often, even if I'm not executing it in a loop. I don't have to worry about String or Date formatting that way.

I don't worry about the performance hit until it becomes a problem. Network latency is usually the bigger problem, and that has to do with the way queries are done rather the Statement vs PreparedStatement.
SQL injection attacks on a system are virtually impossible when using Prepared Statements.
what is SQL injection ?
Suppose your web application asks the user for their ID number. They type it into a box and click submit. This ends up calling the following method:
public List processUserID(String idNumber)
   throws SQLException
{
   String query = "SELECT role FROM roles WHERE id = '" + idNumber +"'";
   ResultSet rs = this.connection.executeQuery(query);
   // ... process results ...
}
If out of a sense of informed malice, your user enters the following text into the ID number field:
12345'; TRUNCATE role; SELECT '


They may be able to drop the contents of your role table, because the string that ends up in "query" will be:
SELECT role FROM roles WHERE id = '12345'; TRUNCATE role; SELECT ''

They have successfully injeced SQL into your application that wasn't there before, hence the name. The specifics of this depend to some extent on your database, but there's some pretty portable SQL you can use to achieve this.

On the other hand, if you use a prepared statement:
public List processUserID(String idNumber)
   throws SQLException
{
   String query = "SELECT role FROM roles WHERE id = ?";
   PreparedStatement statement = this.connection.prepare(query);
   statement.setString(id,idNumber);
 
   ResultSet rs = this.connection.executeQuery(query);
   // ... process results ...
}
The database is told to compile the SQL in query first. The parameter is then submitted - so whatever you put into it will never get executed as SQL (well, ok, it's possible if you're passing it as a parameter to a stored proc, but it's very unlikely) - it will just return no matching records (because there won't be any users with id "12345'; TRUNCATE role; SELECT '"

--------------------------------------------------------------------------

forward和redirect的区别

1.从地址栏显示来说

forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以它的地址栏还是原来的地址.

redirect是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址.所以地址栏显示的是新的URL.客户浏览器发现是302响应,则自动再发送一个新的http请求,请求url是新的location地址。

2.从数据共享来说

forward:转发页面和转发到的页面可以共享request里面的数据.

redirect:不能共享数据.

3.从运用地方来说

forward:一般用于用户登陆的时候,根据角色转发到相应的模块.

redirect:一般用于用户注销登陆时返回主页面和跳转到其它的网站等.

4.从效率来说

forward:高.

redirect:低.

重定向,其实是两次request,

他们的 共同点都是实现了页面的跳转。

--------------------------------------------------------------------

如何实现session共享:

用数据库或者是文件,跨站点的应用都去读取这个数据库或者是文件,就实现了session共享。此实现session共享的方法通过每次会话的session-id都不同【可以做主键】,将会话内容保存在数据库中.然后通过session-id在将数据库中的会话数据取出进行验证.将会话内容保存在数据库中,可以实现session在不同主机之间共享。

负载均衡的时候如何实现相同的session被分配到同一个服务器:

根据网络第七层的负载均衡机制【应用层】,判断应用中session应该被分配到哪个机器。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值