jsp 实验13 servlet

|------------------------------------------------|
| 一、实验目的 掌握怎样在JSP中使用javabean |
| 二、实验项目内容(实验题目) 编写代码,掌握servlet的用法。【参考课本 上机实验3 】 |

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 三、源代码以及执行结果截图: 源代码: inputIntegers.jsp: <%@ page contentType="text/html" %> <%@ page pageEncoding = "utf-8" %> <style> #tom { font-family:宋体 ;font-size:26 ;color:blue } </style> <HTML><body bgcolor=#ffccff > <p id=tom > <form action="handleForward" id =tom method=post > <br>输入整数,用逗号或非数字字符分隔:<br> <textArea name="number" id =tom rows=3 cols=32 ></textArea><br> <input type=submit name = "submit" id =tom value=" 提交 ( personOne 的分析 )" /><br> <input type=submit name = "submit" id=tom value=" 提交 ( personTwo 的分析 )" /> </form> ++</++ ++p++ ++>++ </body></HTML> web.xml: <?xml version="1.0" encoding="utf-8" ?> <web-app> <servlet> <servlet-name>handleForward</servlet-name> <servlet-class>moon.sun.HandleForward_Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>handleForward</servlet-name> <url-pattern>/handleForward</url-pattern> </servlet-mapping> <servlet> <servlet-name>personOne</servlet-name> <servlet-class>moon.sun.PersonOne_Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>personOne</servlet-name> <url-pattern>/personOne</url-pattern> </servlet-mapping> <servlet> <servlet-name>personTwo</servlet-name> <servlet-class>moon.sun.PersonTwo_Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>personTwo</servlet-name> <url-pattern>/personTwo</url-pattern> </servlet-mapping> </web-app> HandleForward_Servlet.Java package moon.sun; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HandleForward_Servlet extends HttpServlet{ public void init(ServletConfig config) throws ServletException{ super.init(config); } public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ RequestDispatcher dispatcher = null; request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String mess = request.getParameter("submit"); String integers = request.getParameter("number"); if(integers == null ||integers.length()==0) { response.sendRedirect("inputIntegers.jsp"); return; } if(mess.contains("personOne")) { dispatcher = request.getRequestDispatcher("personOne"); dispatcher.forward(request,response); } else if(mess.contains("personTwo")){ dispatcher = request.getRequestDispatcher("personTwo"); dispatcher.forward(request,response); } } } PersonOne_Servlet package moon.sun; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class ++PersonOne_Servlet++ extends HttpServlet{ public void init(ServletConfig config) throws ServletException{ super .init(config); } public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); String integers = request.getParameter("number"); String backMess = analysisIntegers(integers); out.print("<html><body><h1>"+backMess+"</body></html>"); } public String analysisIntegers(String input){ Pattern pattern; Matcher matcher; String regex="-?[0-9][0-9]*" ; pattern = Pattern.compile(regex); matcher = pattern.matcher(input); StringBuffer evenNumbers = new StringBuffer("<br>偶数:"); StringBuffer oddNumbers = new StringBuffer("<br>奇数:"); while (matcher.find()) { String str = matcher.group(); if (Integer.parseInt(str)%2==0){ evenNumbers.append(str+","); } else { oddNumbers.append(str+","); } } StringBuffer buffer = evenNumbers.append(oddNumbers); return new String(buffer); } } PersonTwo_Servlet package moon.sun; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class ++PersonTwo_Servlet++ extends HttpServlet{ public void init(ServletConfig config) throws ServletException{ super .init(config); } public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); String integers = request.getParameter("number"); String backMess = analysisIntegers(integers); out.print("<html><body><h1>"+backMess+"</body></html>"); } public String analysisIntegers(String input){ Pattern pattern; Matcher matcher; String regex="-?[0-9][0-9]*" ; pattern = Pattern.compile(regex); matcher = pattern.matcher(input); StringBuffer numbersZero = new StringBuffer("3的倍数:<br>"); StringBuffer numbersOne = new StringBuffer("<br>除以3余1:<br>"); StringBuffer numbersTwo = new StringBuffer("<br>除以3余2:<br>"); while (matcher.find()) { String str = matcher.group(); if (Integer.parseInt(str)%3==0){ numbersZero.append(str+","); } else if (Integer.parseInt(str)%3==1){ numbersOne.append(str+","); } else if (Integer.parseInt(str)%3==2){ numbersTwo.append(str+","); } } StringBuffer buffer = numbersZero.append(numbersOne.append(numbersTwo)); return new String(buffer); } } 运行结果图: |

相关推荐
二川bro16 小时前
量子计算入门:Python量子编程基础
python
夏天的味道٥17 小时前
@JsonIgnore对Date类型不生效
开发语言·python
tsumikistep17 小时前
【前后端】接口文档与导入
前端·后端·python·硬件架构
小白学大数据18 小时前
Python爬虫伪装策略:如何模拟浏览器正常访问JSP站点
java·开发语言·爬虫·python
头发还在的女程序员19 小时前
三天搞定招聘系统!附完整源码
开发语言·python
温轻舟19 小时前
Python自动办公工具06-设置Word文档中表格的格式
开发语言·python·word·自动化工具·温轻舟
花酒锄作田19 小时前
[python]FastAPI-Tracking ID 的设计
python·fastapi
AI-智能20 小时前
别啃文档了!3 分钟带小白跑完 Dify 全链路:从 0 到第一个 AI 工作流
人工智能·python·自然语言处理·llm·embedding·agent·rag
d***956221 小时前
爬虫自动化(DrissionPage)
爬虫·python·自动化
APIshop21 小时前
Python 零基础写爬虫:一步步抓取商品详情(超细详解)
开发语言·爬虫·python