Java、JSP/Servlet面试编程题 - 高飞网
4人看过

Java、JSP/Servlet面试编程题

2014-05-21 01:23:38

1.现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供reset

import java.util.*;

public class bycomma{

public static String[] splitStringByComma(String source){

if(source==null||source.trim().equals(“”))

return null;

StringTokenizer commaToker = new StringTokenizer(source,”,”);

String[] result = new String[commaToker.countTokens()];

int i=0;

while(commaToker.hasMoreTokens()){

result[i] = commaToker.nextToken();

i++;

}

return result;

}

public static void main(String args[]){

String[] s = splitStringByComma(“5,8,7,4,3,9,1″);

int[] ii = new int[s.length];

for(int I = 0;i<s.length;i++){

ii[i] =Integer.parseInt(s[i]);

}

Arrays.sort(ii);

//asc

for(int i=0;i<s.length;i++){

System.out.println(ii[i]);

}

//desc

for(int i=(s.length-1);i>=0;i–){

System.out.println(ii[i]);

}

}

}

2.我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?

Public String translate (String str) {

String tempStr = “”;

try {

tempStr = new String(str.getBytes(“ISO-8859-1″), “GBK”);

tempStr = tempStr.trim();

}

catch (Exception e) {

System.err.println(e.getMessage());

}

return tempStr;

}

3. Session中存储一个String变量,变量名称为studentname,写出在jsp中如何得到这个session变量的值的语句。

<%

String studentName = session.getAttribute(“studentname”) == null ? “” : (String)session.getAttribute(“studentname”);

%>

4.在Web应用中,各举POST请求和GET请求应用场景的一个例子

<form name=” …” method=”get” action=”…”></form>

<form name=” …” method=”post” action=” …”></form>

5.编写一个servlet。servlet的功能要求:记录访问该servlet的客户端的ip和访问时间,把记录写入client_ip.log文件。 client_ip.log里的格式大致如下:

9:36 2005-4-8 192.168.0.1

9:36 2005-4-8 127.0.0.1

package com.itjob.st;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

private File clientIp;

private FileOutputStream fos;


/**

* Constructor of the object.

*/

public MyServlet() {

super();

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts “destroy” string in log

// Put your code here

}


public void service(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException {

openFileOutputStream();

String clientIp = req. getRemoteAddr();

SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm yyyy-MM-dd”);

Date date = new Date();

String currDate = sdf.format(date);

String clientIpLog = clientIp + ” ” + currDate;

this.fos.write(clientIpLog.getBytes());

this.fos.flush();

closeFileOutputStream();

}


/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

out

.println(“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\”>”);

out.println(“<HTML>”);

out.println(” <HEAD><TITLE>A Servlet</TITLE></HEAD>”);

out.println(” <BODY>”);

out.print(” This is “);

out.print(this.getClass());

out.println(“, using the GET method”);

out.println(” </BODY>”);

out.println(“</HTML>”);

out.flush();

out.close();

}

/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

out

.println(“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\”>”);

out.println(“<HTML>”);

out.println(” <HEAD><TITLE>A Servlet</TITLE></HEAD>”);

out.println(” <BODY>”);

out.print(” This is “);

out.print(this.getClass());

out.println(“, using the POST method”);

out.println(” </BODY>”);

out.println(“</HTML>”);

out.flush();

out.close();

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occure

*/

public void init() throws ServletException {

clientIp = new File(“D:\\client_ip.log”);

}


private void openFileOutputStream() {

try {

fos = new FileOutputStream(clientIp);

}

catch(FileNotFoundException fnfe) {

fnfe.printStackTrace();

}

}


private void closeFileOutputStream() {

try {

fos.close();

fos = null;

}

catch(IOException fnfe) {

fnfe.printStackTrace();

}

}

}

还没有评论!
54.158.248.167