实现B/S架构,输入一串字母或数字,将它们按从小到大排序,排序算法在服务端实现。
以下是实现:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取页面中输入框的数据 String str = request.getParameter("str"); //将字符串转换为char数组 char[] ch = str.toCharArray(); //利用冒泡排序进行排序 for(int i = 0;i<ch.length-1;i++){ for(int j =0;j<ch.length-i-1;j++){ if(ch[j]>ch[j+1]){ char temp;//定义一个中间变量 temp = ch[j]; ch[j] = ch[j+1]; ch[j+1]= temp; } } } //将char数组转换为String 字符串 String ss = String.valueOf(ch); request.setAttribute("sss", ss); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
jsp页面代码:
<%-- 拿到保存到request中的字符串sss--%> <h1>${sss }</h1> <form action="/test/Test"method="get"> <input type="text" name="str"/> <input type="submit" id="btn" value="check"/> </form>