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
ii[i] =Integer.parseInt(s[i]);
}
Arrays.sort(ii);
//asc
for(int i=0;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请求应用场景的一个例子
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.
*/
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.
*
* 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(“”);
out.println(“”);
out.println(” A Servlet”);
out.println(” ”);
out.print(” This is “);
out.print(this.getClass());
out.println(“, using the GET method”);
out.println(” ”);
out.println(“”);
out.flush();
out.close();
}
/**
* The doPost method of the servlet.
*
* 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(“”);
out.println(“”);
out.println(” A Servlet”);
out.println(” ”);
out.print(” This is “);
out.print(this.getClass());
out.println(“, using the POST method”);
out.println(” ”);
out.println(“”);
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @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();
}
}
}