java题目
1.httpSession 作用,可以列举他的方法。
HttpSession 中可以跟踪并储存用户信息,把值设置到属性中,有2 个方法:
setAttribute(),getAttrribute();
例如:在一个方法中用session.setAttribute(“student”,student);在
session 中设置一个属性名为student,值为一个名为student 的对象。而后可在
同一session 范围内用getAttribute(“student”)取出该属性,得到student
对象。
(原理)
ServletContext 更倾向于一个Web应用的全局范围,而session则只是在于保存用户和服务器之间交互的一个来回完整性。
一个Web服务只有一个ServeltContext,一般都是存放一些全局信息,或者获取一些全局信息.
1 新客户端向服务器第一次发送请求的时候,request中并无sessionID .
2 此时server端会创建一个session对象,并分配一个sessionID,serssion对象会保存在服务器端。此时session的状态处于new state状态,如果调用session.isNew(),则返回true
3 当服务器段处理完毕后,将此sessionID,同response一同传回到客户段,并将其存入到cookie中。
4 当客户段再次发送请求时,会将sessionID 同request一起发送,传递给服务器端。
5 服务器端可以根据传递过来的sessionID将这次请求(request)与保存在服务器端的session对象联系起来,此时的session
? 已不处于new state状态,如果调用session.isNew(),则返回false.
6 循环3-5 ,直到session超时或销毁。
注意:一个具有上亿的访问用户的系统,要在服务端数据库中检索出用户的偏好信息显然是低效的,
Session管理器不管用什么数据结构和算法都要耗费大量内存和CPU时间;而用cookie,则根本不用检索和维护session数据,服务器可以做成无状态的,当然高效);
2.get和post 区别
1、get是从服务器上获取数据,post则是向服务器传送数据;
2、get将表单中数据的按照variable=value的 形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使用“&”连接。Post是将表单中的数据放在form的 数据体中,按照变量和值相对应的方式,传递到action所指向URL
所以从上述也可以得到另外两个结论:
1、get传送的数据量较小,不能大于2KB,这主要是因为受URL长度限制。post传送的数据量较大,所以在上传文件只能使用Post。一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB;
2、get安全性非常低(因为用户可以通过URL看到),post安全性较高(其所有操作对用户来说都是不可见的)。但是执行效率却比post方法好;
3.说说连接池 的优点以及原理
作用:提高程序的运行效率,能够支持更多的用户。方便监视连接的数量和使用情况。
原理:先初始化一定的数据库连接对象,并且把这些连接保存在连接池中。当程序需要访问数据库的时候,从连接池中取出一个连接,数据库操作结束后,再把这个用完的连接重新放回连接池。
4.获得登录页面上传过的用户登录信息,包含用户名和密码。怎样将用户名和密码显示输出页面post.jsp上 。写出对应的post.jsp页面。
<%
?? String userName = request.getParameter("userName");
?? String pwd = request.getParameter("pwd");
?? -----如果是中文
?? String userName = new String(request.getParameter("userName").getBytes("ISO-8859-1"));
%>
5. 现在编码方式有多种多样ISO-8859-1,GBK,有没有其它的编码方式,请说明。
常用的字符编码方式:ISO8859-1、GB2312、GBK、UTF-8/UTF-16/UTF-32.
ISO-8859-1:用来编码拉丁文;
GB2312、GBK:用来编码简体中文;
UTF-8/UTF-16/UTF-32:是国际标准UNICODE的编码方式,用得最多的是UTF-8,主要是因为它对拉丁文编码时节约空间。
6.有n个整数,需要将n个整数从m位置之前的整数,插入到第m个位置。
public class InsertNum {
?? ?? public void insertNum(int [] a,int index)
?? ?? {
?? ?int [] c = new int[a.length+index];
?? ??? ?? for (int i = 0; i < c.length; i++)
?? ??? ?? {
?? ??? ??? ?if(i<index)
?? ??? ??? ?{
?? ??? ??? ??? ?c[i] = a[i];
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?c[i] = a[i-index];
?? ??? ??? ?}
?? ??? ? }
?? ??? ?? for(int j=0;j<c.length;j++)
?? ??? ????? {
?? ??? ??? ??? ? System.out.println(j+"="+c[j]);
?? ??? ????? }
?? ?? }
?? ?? public static void main(String[] args)
?? ?? {
?? ??? ?InsertNum t = new InsertNum();
?? ??? ?int[] a = {1,10,14,21,3,43,54};
?? ??? ?t.insertNum(a,3);
?? ?? }
}
7.编写socket客户端代码。
socket客户端的响应的格式为:报文ID(三位数)+用户名+密码。
socket服务端的响应的格式为:
对应服务IP为:192.168.0.1 端口:9999
当输入“END”程序退出
public Class Client{
? public static void main(String[] args){
???? Socket socket = null;
??? ??? ?try
?? ??? ?{
?? ??? ???? socket = new Socket("192.168.0.1",9999);
?? ??? ??? ?BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
?? ??? ??? ?PrintWriter out = new PrintWriter(server.getOutputStream());
?? ??? ??? ?BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
?? ??? ?while(true)
?? ??? ?{
?? ??? ??? ?String str = bf.readLine();
?? ??? ??? ?out.println(str);
?? ??? ??? ?out.flush();
?? ??? ??? ?if("end".equals(str))
?? ??? ??? ?{
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?System.out.println(in.readLine());
?? ??? ?}
?? ??? ? server.close();
?? ??? ? in.close();
?? ??? ? out.close();
?? ??? ? bf.close();
?? ??? ?}catch(IOException e)
?? ??? ?{
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
? }
}
8. 写连接oracle程序并执行查询语句,写出关键代码即可,查询语句为 select * from table where name = “111111”
public class DBHelper {
?? ?
?? ?private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
?? ?private static final String URL="jdbc:oracle:thin:@localhost:1521:ORCL";
?? ?private static final String USER = "";
?? ?private static final String PWD = "";
?? ?private static Connection con=null;
?? ?private static PreparedStatement pst = null;
?? ?private static ResultSet rst=null;
??? ?
?? ?/**
?? ? * 创建数据库的链接
?? ? * @return
?? ? */
?? ?public static Connection getConnection()
?? ?{
?? ??? ?Connection con=null;
?? ??? ?try {
?? ??? ??? ?Class.forName(DRIVER);
?? ??? ??? ?con=DriverManager.getConnection(URL,"scott","tiger");
?? ??? ?}
?? ??? ?catch (Exception e)
?? ??? ?{
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return con;
?? ?}
?? ?
???? /**
????? * 关闭数据库的连接
????? * @param con
????? * @param pst
????? * @param rst
????? */
?? ?
?? ?public static void getClose(Connection con,PreparedStatement pst,ResultSet rst)
?? ?{
?? ??? ?try
?? ??? ?{
?? ??? ??? ?if(con!=null)
?? ??? ??? ??? ?con.close();
?? ??? ??? ?if(pst!=null)
?? ??? ??? ??? ?pst.close();
?? ??? ??? ?if(rst!=null)
?? ??? ??? ??? ?rst.close();
?? ??? ?}
?? ??? ?catch (SQLException e)
?? ??? ?{
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?
?? ?public static void getData()
?? ?{ ?
?? ??? ?String sql = "select * from table where name = '111111'";
?? ??? ?try
?? ??? ?{
?? ??? ??? ?con = getConnection();
?? ??? ??? ?pst = con.prepareStatement(sql);
?? ??? ??? ?rst = pst.executeQuery();
?? ??? ??? ?while(rst.next())
?? ??? ??? ?{??? //获取查询到的值
?? ??? ??? ??? ?//rst.getString(0);
?? ??? ??? ???? //rst.getInt(1);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?catch (SQLException e)
?? ??? ?{
?? ??? ??? e.printStackTrace();
?? ??? ?}
?? ??? ?finally
?? ??? ?{
?? ??? ??? ?getClose(con, pst, rst);
?? ??? ?}
?? ?}
}
9.判断字符串是否有空值,写junit测试
public class Space{
? public boolean isSpace(String str){
???? boolean flag=false;
???? int index = str.indexOf(" ");
???? if(index == -1){
??????? System.out.println("no space!");
???? }else{
??????? System.out.println("yes space!");
??????? flag=true;
???? }
???? return flag;
? }
}
---Junit测试类(继承TestCase时,setUp和tearDown方法不能为静态的)
public class TestSpace extends TestCase{
?? ?
?? ?private Space s = null;
?? ?
?? ?@Before
?? ?public void setUp() throws Exception {
?? ??? ?try {
?? ??? ??? ?s = new Space();
?? ??? ?} catch (Exception e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?@After
?? ?public void tearDown() throws Exception {
?? ??? ?super.tearDown();
?? ?}
?? ?@Test
?? ?public void testIsSpace() {
?? ??? s.isSpace("hello world !");
?? ?}
}