JSP实用教程实验四内置对象

阅读: 评论:0

JSP实用教程实验四内置对象

JSP实用教程实验四内置对象

实验四   内置对象

一、实验目的

1. 掌握request对象。

2. 掌握response对象。

3. 掌握session对象。

4. 掌握application对象。

5. 掌握out对象。

6. 掌握每一个内置对象的内容及作用域和生命周期,熟练运用内置对象。

二、实验内容

1、教材4.6.1节 196算法之谜。

2、教材4.6.2节 计算器。

3、教材4.6.3节 单词的频率。

4、教材4.6.4节 成绩与饼图。

5、教材4.6.5节 记忆测试。

三、实验步骤

1.196算法之谜

①实验代码

文件ch4_1.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding ="utf-8" %>
<style>#tomStyle{font-family:宋体;font-size:36;color:blue }
</style> 
<HTML><body id=tomStyle bgcolor = #ffccff><form action="ch4_2.jsp" method=post name=form>输入一个正整数数:<br><input type=text name="number" id=tomStyle size=16 value = 520 /><br><input type="submit" id=tomStyle value="提交" /></form> </body>
</HTML>

文件ch4_2.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<%@ page import = "java.math.BigInteger" %> 
<HTML><body bgcolor = cyan><p style="font-family:宋体;font-size:36;color:black"><%!public static String reverse(String s) {  StringBuffer  buffer = new StringBuffer(s);StringBuffer  reverseBuffer = verse();  return  String();}%><% String regex = "[1-9][0-9]*";String startNumber&#Parameter("number");if(startNumber==null||startNumber.length()==0) {startNumber = "520";}if(!startNumber.matches(regex)) {response.sendRedirect("ch4_1.jsp"); return;}long step = 1;BigInteger number = new  BigInteger(startNumber);  BigInteger reverseNumber = new BigInteger(String())); BigInteger resultNumber = number.add(reverseNumber);   out.print("<br>"+number+"+"+reverseNumber+"="+resultNumber);BigInteger p = new BigInteger(String())); while(!resultNumber.equals(p)) {   number = new  String());reverseNumber = new  BigInteger(String()));resultNumber = number.add(reverseNumber);p = new BigInteger(String()));out.print("<br>"+number+"+"+reverseNumber+"="+resultNumber);step++;}out.print("<br>"+step+"步得到回文数:"+resultNumber);         %></p></body>
</HTML>

②运行结果

2.计算器

①实验代码

文件ch4_3.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = #ffccff><form action="ch4_4.jsp" method=post name=form><p style="font-family:宋体;font-size:18;color:blue">输入运算数、选择运算符号:<br><input type=text name="numberOne" size=6/><select name="operator" ><option selected="selected" value="+">加<option value="-">减<option value="*">乘<option value="/">除</select> <input type=text name="numberTwo"  size=6 /><br><input type="submit" name="submit" value="提交" /></form> </p></body>
</HTML>

 文件ch4_4.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = cyan><p style="font-family:宋体;font-size:18;color:black"><% String numberOne&#Parameter("numberOne");String numberTwo&#Parameter("numberTwo");String operator&#Parameter("operator");if(numberOne==null||numberOne.length()==0) {response.sendRedirect("ch4_3.jsp"); return;}else if(numberTwo==null||numberTwo.length()==0) {response.sendRedirect("ch4_3.jsp"); return;}try{double a=Double.parseDouble(numberOne);double b=Double.parseDouble(numberTwo);double r=0;if(operator.equals("+"))r=a+b;else if(operator.equals("-"))r=a-b;else if(operator.equals("*"))r=a*b;else if(operator.equals("/"))r=a/b;out.print(a+""+operator+""+b+"="+r);}catch(Exception e){out.println("请输入数字字符");}%></body>
</HTML>

②运行结果

3.单词的频率

①实验代码

文件ch4_5.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><style>#textStyle{font-family:宋体;font-size:23;color:blue }</style><body bgcolor = #ffccff><p id = "textStyle">输入英文文章,单击提交键:<%String content = "We are student,are you student?";%><form action="ch4_6.jsp" method=post name=form><textArea  name="english" id = "textStyle" rows=5 cols=38><%= content %></textArea><br><input type="submit"  name="submit" id = "textStyle" value="提交"/></form>  </p></body>
</HTML>

 文件ch4_6.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="Pattern" %>
<%@ page import="Matcher" %>
<HTML><body bgcolor = cyan><p style="font-family:宋体;font-size:25;color:black"><%!class Word  {   String englishWord;int  count ;   public boolean equals(Object o) {Word wd=(Word)o;return englishWord.lishWord);} }%><%  request.setCharacterEncoding("utf-8");String englishText&#Parameter("english");ArrayList<Word> wordList = new ArrayList<Word>(); Pattern pattern;             Matcher matcher;             String regex="[a-zA-Z]+" ;   pattern = Patternpile(regex);       matcher = pattern.matcher(englishText);    int allWordAmount = 0;  while(matcher.find()) {String str = up();allWordAmount++;  Word wd = new Word();wd.englishWord = str;if(!ains(wd)) { wordList.add(wd);wd.count = 1;  }else {int index = wordList.indexOf(wd);wd = (index);wd.count += 1;}} int diffrentWords = wordList.size();Iterator<Word> ite = wordList.iterator();out.print("出现了"+ allWordAmount+"个单词:<br>"); out.print("使用了"+ diffrentWords+"个不相同的单词:<br>");while(ite.hasNext()) {  Word wd = ();double fq =(double)(wd.count)/allWordAmount;String frequency =String.format("%.2f",fq);out.print("<br>"&#lishWord+"频率(保留2位小数)"+frequency);   }%></p></body>
</HTML>

②运行结果

4.成绩与饼图

①实验代码

文件ch4_7.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = #ffccff><style>#textStyle{font-family:宋体;font-size:18;color:blue }</style><p id="textStyle"><form action="ch4_8.jsp"  method=post >输入考试有关数据:<br>考试人数:<input type="text" name="totalStudents" value=1 size=12 /><br>90~100人数:<input type="text" name="student90" value=1 size=12 /><br>80~89人数:<input type="text" name="student80" value=1 size=12 /><br>70~79人数:<input type="text" name="student70" value=1 size=12 /><br>60~69人数:<input type="text" name="student60" value=1 size=12 /><br>0~59人数:<input type="text" name="student59" value=1 size=12 /><br><input type="submit" name="submit" id="textStyle" value="看成绩分析"/><br><input type="submit" name="submit" id="textStyle" value="看成绩饼图"/></form> </p></body>
</HTML>

 文件ch4_8.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<%@ page import="java.awt.*" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="*" %>  
<%@ page import="javax.imageio.ImageIO" %>
<%  request.setCharacterEncoding("utf-8"); String submitValue = Parameter("submit");String totalStudents = Parameter("totalStudents"); String students90 = Parameter("student90");String students80 = Parameter("student80");String students70 = Parameter("student70");String students60 = Parameter("student60");String students59 = Parameter("student59");int students90_number = Integer.parseInt(students90); int students80_number = Integer.parseInt(students80); int students70_number = Integer.parseInt(students70); int students60_number = Integer.parseInt(students60);int students59_number = Integer.parseInt(students59); int totalStudents_number = Integer.parseInt(totalStudents);  boolean isRight = totalStudents_number == students90_number+students80_number+students70_number+students60_number+students59_number ;if(isRight == false) {response.sendRedirect("ch4_7.jsp"); return;}double percent90 = (double)students90_number/totalStudents_number;double percent80 = (double)students80_number/totalStudents_number;double percent70 = (double)students70_number/totalStudents_number;double percent60 = (double)students60_number/totalStudents_number;double percent59 = (double)students59_number/totalStudents_number;double angSt = 0;double angExt = 0;int x = 450,y =50;if(submitValue.equals("看成绩饼图")){response.setContentType("image/jpeg");int width=1000, height=500;BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);Graphics g = Graphics();g.fillRect(0, 0, width, height);Graphics2D g_2d=(Graphics2D)g; Arc2D arc=new  Arc2D.Double(0,0,400,400,0,360,Arc2D.PIE);arc.setAngleStart(angSt);     angExt = -percent59*360;arc.setAngleExtent(angExt);  g_2d.setFont(new Font("",Font.PLAIN,22));g_2d.setColor(Color.black);g_2d.drawString("黑色不及格",x,y);g_2d.fillRect(x+150,35,30,15);g_2d.fill(arc);              angSt = -percent59*360;angExt = -percent60*360;arc.setAngleStart(angSt);     arc.setAngleExtent(angExt);  g_2d.);g_2d.drawString("绿色为60-69",x,y=y+30);g_2d.fillRect(x+150,65,30,15);g_2d.fill(arc);              angSt = -(percent59*360+percent60*360);angExt = -percent70*360;arc.setAngleStart(angSt);    arc.setAngleExtent(angExt);  g_2d.llow);g_2d.drawString("黄色为70-79",x,y=y+30);g_2d.fillRect(x+150,95,30,15);g_2d.fill(arc);              angSt = -(percent59*360+percent60*360+percent70*360);angExt = -percent80*360;arc.setAngleStart(angSt);    arc.setAngleExtent(angExt);  g_2d.setColor(Color.blue);g_2d.drawString("蓝色为80-89",x,y=y+30);g_2d.fillRect(x+150,125,30,15);g_2d.fill(arc);              angSt = -(percent59*360+percent60*360+percent70*360+percent80*360);angExt = -percent90*360;arc.setAngleStart(angSt);     arc.setAngleExtent(angExt);  g_2d.setColor(Color.pink);g_2d.drawString("粉色为90-100",x,y=y+30);g_2d.fillRect(x+150,155,30,15);g_2d.fill(arc);              try {OutputStream outClient= OutputStream();boolean boo =ImageIO.write(image,"jpeg",outClient);}catch(Exception exp){}}%>
<HTML><body bgcolor = #EEEEFF><p style="font-family:宋体;font-size:18;color:blue"><%  String percent90Result = String.format("%.3f%%",percent90*100);String percent80Result = String.format("%.3f%%",percent80*100);String percent70Result = String.format("%.3f%%",percent70*100);String percent60Result = String.format("%.3f%%",percent60*100);String percent59Result = String.format("%.3f%%",percent59*100);%> <b>成绩百分比分布(保留3位小数)<br>90~100分:<%=percent90Result %><br>80~89分:<%=percent80Result %><br>70~79分:<%=percent70Result %><br>60~69分:<%=percent60Result %><br>不及格:<%=percent59Result %><br></p></body>
</HTML>

②运行结果

5.记忆测试

①实验代码

文件ch4_9.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = #ffccff><style>#textStyle{font-family:宋体;font-size:26;color:blue }</style><form action="ch4_10.jsp" id ="textStyle" method=post name=form><input type=radio name="grade"  value="5" />初级<input type=radio name="grade"  value="7" checked="ok" />中级<input type=radio name="grade"  value="10" />高级<br><input type="submit"  name="submit" id ="textStyle" value="提交"/><input type="reset" id ="textStyle" value="重置" /></form> </body>
</HTML>

 文件ch4_10.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Random" %>
<HTML><body bgcolor = #ffccff><style>#textStyle{font-family:宋体;font-size:36;color:blue }</style><%!  static ArrayList<String> list = new  ArrayList<String>();static {list.add("☆");list.add("○");list.add("△");list.add("□");list.add("◇");}String getNextTestString(int length) {StringBuffer buffer = new StringBuffer();Random random = new Random(); for(int i=0;i<length;i++) {int index = Int(list.size());String str = (index);buffer.append(str);}return new String(buffer);}%><%  String grade = Parameter("grade");if(grade == null){grade =(String) Attribute ("grade");}int number = Integer.parseInt(grade);session.setAttribute ("grade" ,grade);String testString = null;   String yesORNo = null;      yesORNo = (Attribute ("yesORNo"); if(yesORNo == null) {testString = getNextTestString(number);session.setAttribute ("yesORNo" ,"yes");session.setAttribute ("testString" ,testString);}else if(yesORNo.equals("yes")){response.sendRedirect("ch4_11.jsp");return;}else if(yesORNo.equals("no")){testString = getNextTestString(number); session.setAttribute ("yesORNo" ,"yes");session.setAttribute ("testString" ,testString);}%><p id = "textStyle">给5秒种记住您看到的字符序列:<br><%=testString %><br>5秒种后,将转到答题页。<%  response.setHeader("refresh","5");%></p></body>
</HTML>

 文件ch4_11.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = #ffccff><style>#textStyle{font-family:宋体;font-size:26;color:blue }</style><form action="ch4_12.jsp" id = textStyle method=post ><%int n =Integer.parseInt((Attribute ("grade"));session.setAttribute ("yesORNo" ,"no");for(int i=1;i<=n;i++){out.print("<br>第"+i+"个字符:");out.print("<input type=radio name=R"+i+" value='☆'/>☆"+"<input type=radio name=R"+i+" value='○' />○"+ "<input type=radio name=R"+i+" value='△' />△"+ "<input type=radio name=R"+i+" value='□' />□"+ "<input type=radio name=R"+i+" value='◇' />◇");} %><br><input type="submit"  name="submit" id ="textStyle" value="提交"/><input type="reset" id ="textStyle" value="重置" /></form> </body>
</HTML>

文件ch4_12.jsp:

<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %> 
<HTML><body bgcolor = white ><p style="font-family:宋体;font-size:26;color:blue"> <%   session.setAttribute ("yesORNo" ,"no");request.setCharacterEncoding("utf-8"); int n =Integer.parseInt((Attribute ("grade")); StringBuffer buffer = new StringBuffer();for(int i=1;i<=n;i++){buffer.Parameter("R"+i)); //获取radio提交的值out.print(""&#Parameter("R"+i));}String userAnswer = new String(buffer);String testString = (Attribute ("testString");if(testString.equals(userAnswer)){out.print("您记忆不错");}else {out.print("您没记忆住额!答案是:<br>"+testString);}%><br><a href = "ch4_10.jsp">返回,继续练习记忆</a><br><a href = "ch4_9.jsp">重新选择级别</a></p></body>
</HTML>

②运行结果

 

四、实验总结

本次实验通过五个小实验学习了Jsp中内置对象request获取from内的数据进行提交,使用了response对象实现重定向。并使用response对象改变MIME类型,最后一个实验使用了session对象存储用户的数据,以及使用response对象实现重定向和添加新的响应头。

本文发布于:2024-02-03 00:32:28,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170689154647483.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对象   实用教程   JSP
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23