成语接龙 |
您所在的位置:网站首页 › 制作接龙小程序app › 成语接龙 |
互联网软件开发期中测验
本程序需在Tomcat 8上运行,Tomcat7会导致判断汉字函数出错,可自行修改将该函数删去,不进行判断 注意事项 按班级序号就座,考试结束后不要关闭MyEclipse(Tomcat为运行状态)及浏览器。 一、创建项目:启动MyEclipse 2014,新建名为【你的学号】的【Web Project】。(20分) 二、使用JSP+ Servlet技术,实现如下功能,具体要求如下:1、创建add.jsp:如图1所示,点击【接龙】后提交到LinkServlet。(20分) 2、LinkServlet:作为Servlet接收来自add.jsp的表单数据,将成语接龙到上一成语之后保存到session中,并重定向到show.jsp。(20分) 3、show.jsp:以表格的形式列出session内置对象中的全部成绩(如图2所示)。 (20分) 4、在web.xml中配置LinkServlet(或者采用注解的方式)。(20分) 5、部署工程并启动Tomcat,打开浏览器访问:http://localhost:8080/你的学号/add.jsp,并测试相关功能。(20分) add.jsp input[type=submit]{ background-color: green; color:white; height: 30px; } 上一个成语: 最后一个字:show.jsp table { border-collapse:collapse; } th { background-color:green; color:white; } td { text-align:center; } 序号 内容 out.println("含有非汉字!"); }else if(a.equals("2")) { out.println("非四字成语!"); }else if(a.equals("3")) { out.println("接龙失败,换个词吧!"); } %> 查看成语接龙 继续成语接龙web.xml 44181 This is the description of my J2EE component This is the display name of my J2EE component LinkServlet lumen.LinkServlet LinkServlet /LinkServlet index.html index.htm index.jsp default.html default.htm default.jspLinkServlet package lumen; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LinkServlet extends HttpServlet { public boolean check(String str)//判断str是否都为汉字,tomcat 7识别不了 { int n = 0; for(int i = 0; i return false; } } return true; } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); //获取表单提交的成语str String str=request.getParameter("str"); //排除不为4位汉字的情况:不为汉字或不为4位都将重定向到error界面 if(!check(str)){ response.sendRedirect("error.jsp?action=1"); }else if((str.length()!=4)){ response.sendRedirect("error.jsp?action=2"); }else { //截取第一个字符,最后一个字符 String first = str.substring(0,1); String last = str.substring(3,4); Word word=new Word(); word.setStr(str); word.setLast(last); List list=(List)session.getAttribute("wordlist");//创建Word类型的集合,从session中获取 if(list==null) //说明还没有成语 { list=new ArrayList(); list.add(word); session.setAttribute("wordlist", list); //将list存入session response.sendRedirect("show.jsp"); }else{ Word lastword=list.get(list.size()-1);//取出list中最后一位的Word对象 if(lastword.getLast().equals(first))//判断尾首是否相等,只能用equals() { //==判断地址;equals判断内容 list.add(word); session.setAttribute("wordlist", list); response.sendRedirect("show.jsp"); }else { response.sendRedirect("error.jsp?action=3"); } } } } }Word.java package lumen; public class Word { private String str; private String last; public String getStr() { return str; } public void setStr(String str) { this.str = str; } public String getLast() { return last; } public void setLast(String last) { this.last = last; } } 运行结果Case1:非汉字情况 Case2:汉字不为4个字 Case3:输入正确
|
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |