java servlet,实现与网页联动,冒泡排序

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//java servlet,实现与网页联动,冒泡排序


/**

 * Servlet implementation class Homework

 */
@WebServlet("/work")
public class work_sort extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//创建数组

int[] arry2Sort = new int[5] ; 

int temp;

//get到响应的writer

PrintWriter writer = response.getWriter();

String rec="";

//设置响应和请求的编码和内容格式

request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

response.setCharacterEncoding("UTF-8");

//冒泡排序代码

for(int i=0;i arry2Sort[i]=Integer.parseInt(request.getParameter("para"+(i+1))); 
}
for(int a=0;a for(int idx =0;idx if(arry2Sort[idx]>arry2Sort[idx+1]){
temp = arry2Sort[idx+1];
arry2Sort[idx+1]=arry2Sort[idx];
arry2Sort[idx]=temp;
}
}
}

 

//将结果输出,输出成另一个页面,这个页面也能继续排序

rec="
";
rec+="Number 1:
\r\n";
rec+="Number 2:
\r\n"; 
rec+="Number 3:
\r\n"; 
rec+="Number 4:
\r\n"; 
rec+="Number 5:
\r\n"; 
rec+="\r\n"; 
rec+="
";
writer.write(rec);
}
// }
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

*******************************************************以下为html页面,用于提交数字***********************************************************************************************





Insert title here



Number1:

Number2:

Number3:

Number4:

Number5:







你可能感兴趣的:(java servlet,实现与网页联动,冒泡排序)