需求是利用servlet转发技术,实现对html网页中用户输入的内容进行统计,统计每个字母出现的次数,忽略大小写。其中统计功能在一个servlet中,转发功能在另一个servlet中。
1.新建dynamic web project,命名为CounterCharacter
2.在webContent目录中新建index.html文件,设置form表单
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/CountCharacter/LetterCountServlet" method="post"> 请输入文章<textarea rows="5" cols="50" name="content"></textarea> <input type="submit" value="提交"/> </form> </body> </html>
package servletDemo; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LetterCountServlet * 统计用户输入内容中每个字母出现次数 */ @WebServlet(asyncSupported = true, urlPatterns = { "/LetterCountServlet" }) public class LetterCountServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LetterCountServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("Servlet works"); //获得form表单提交的内容 String content = request.getParameter("content"); //忽略大小写,把用户输入全部转换成大写 content = content.toUpperCase(); int[] times = new int[26]; for(int i = 0; i < content.length();i++) { char c = content.charAt(i); //判断C是不是字母 if(Character.isLetter(c)) times[c-'A']++; } //交给下一个servlet显示结果 ServletContext context = getServletContext(); //添加count属性 context.setAttribute("count", times); //跳转至下一个servlet RequestDispatcher dispatcher = context.getRequestDispatcher("/ResulstServlet"); dispatcher.forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request,response); } }
package servletDemo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ResulstServlet * 显示统计结果 */ @WebServlet(asyncSupported = true, urlPatterns = { "/ResulstServlet" }) public class ResulstServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ResulstServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ServletContext context = getServletContext(); int[] times = (int[]) context.getAttribute("count"); //输出结果 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); for(int i = 0; i < times.length;i++) { char c = (char) ('A'+ i); int frequency = times[i]; out.println("字母:"+c+",出现次数"+frequency+"次!<br/>"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request,response); } }