JSP实验报告3.4.5

时间:2024.4.20

实验三:试用attribute指令和variable指令


1.相关知识点

一个Tag文件中通过使用attribute指令:

inputAndShow.jsp

<%@ page contentType="text/html;Charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="computer" %>

<HTML>

输入的三个数值a,b,c(代表三角形的三边或梯形的上底、下底和高):

<BODY color=cyan>

   <FORM action="" method=get name=form>

<table>

<tr><td>输入数值a:</td>

    <td><INPUT type="text" name="a"></td>

</tr>

<tr><td>输入数值b:</td>

    <td><INPUT type="text" name="b"></td>

</tr>

<tr><td>输入数值c:</td>

    <td><INPUT type="text" name="c"></td>

</tr>

</table>

<INPUT type="radio" name="R" value="triangle">代表三角形

<INPUT type="radio" name="R" value="lader">代表梯形

<br><INPUT TYPE="submit" value="提交" name=submit>

</FORM>

<% String a=request.getParameter("a");

   String b=request.getParameter("b");

   String c=request.getParameter("c");

   String cd=request.getParameter("R");

   if(a==null||b==null||c==null){

      a="0";

      b="0";

      c="0";

      cd="0";

   }

   if(a.length()>0&&b.length()>0&&c.length()>0){

%> <computer:GetArea numberA="<%=a%>" numberB="<%=b%>"

                     numberC="<%=c%>" condition="<%=cd%>"/>

   <br><%=message%>

   <br><%=area%>

<% }

%>

</BODY></HTML>

GetArea.Tag

<%@ attribute name="numberA" required="true" %>

<%@ attribute name="numberB" required="true" %>

<%@ attribute name="numberC" required="true" %>

<%@ attribute name="condition" required="true" %>

<%@ variable name-given="area" variable-class="java.lang.Double" scope="AT_END"

%>

<%@ variable name-given="message" scope="AT_END" %>

<%!

public double getTriangleArea(double a,double b,double c){

if(a+b>c&&a+c>b&&c+b>a){

double p=(a+b+c)/2.0;

double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));

return area;

}

else

return -1;

}

public double getLaderArea(double above,double bottom,double h){

double area=(above+bottom)*h/2.0;

return area;

}

%>

<% try{  double a=Double.parseDouble(numberA);

         double b=Double.parseDouble(numberB);

         double c=Double.parseDouble(numberC);

         double result=0;

         if(condition.equals("triangle")){

            result=getTriangleArea(a,b,c);

            jspContext.setAttribute("area",new Double(result));

            jspContext.setAttribute("message","三角形的面积");

         }

         else if(condition.equals("lader")){

            result=getLaderArea(a,b,c);

            jspContext.setAttribute("area",new Double(result));

            jspContext.setAttribute("message","梯形的面积");

         }

   }

   catch(Exception e){

         jspContext.setAttribute("area",new Double(-1.0));

         jspContext.setAttribute("message",""+e.toString());

   }

%>


实验结果图:

实验四:session对象


inputGuess.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<HTML>

<BODY bgcolor=cyan><FONT Size=2>

<P>随机分给了你一个1到100之间的数,请猜!

<%

int number=(int)(Math.random()*100)+1;

session.setAttribute("count",new Integer(0));

session.setAttribute("save",new Integer(number));

%>

<FORM action="result.jsp" method="post" name=form>

输入你的猜测:<INPUT type="text" name="boy">

<INPUT TYPE="submit" value="送出" name="submit">

</FORM>

</FONT></BODY></HTML>

Result.jsp

<% String str=request.getParameter("boy");

if(str.length()==0){

response.sendRedirect("inputGuess.jsp");

}

int guessNumber=-1;

try{

guessNumber=Integer.parseInt(str);

Integer integer=(Integer)session.getAttribute("save");

int realnumber=integer.intValue();

if(guessNumber==realnumber){

int n=((Integer)session.getAttribute("count")).intValue();

n=n+1;

session.setAttribute("count",new Integer(n));

response.sendRedirect("success.jsp");

}

else if(guessNumber>realnumber){

int n=((Integer)session.getAttribute("count")).intValue();

n=n+1;

session.setAttribute("count",new Integer(n));

response.sendRedirect("large.jsp");

}

else if(guessNumber<realnumber){

int n=((Integer)session.getAttribute("count")).intValue();

n=n+1;

session.setAttribute("count",new Integer(n));

response.sendRedirect("small.jsp");

}

}

catch(Exception e){

response.sendRedirect("inputGuess.jsp");

}

%>

Small.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<HTML>

<BODY bgcolor=cyan>

<FONT Size=2>

<FORM action="result.jsp" method="get" name=form>

猜小了,请再猜:<INPUT type="text" name="boy">

<INPUT TYPE="submit" value="送出" name="submit">

</FORM>

</FONT></BODY></HTML>

Large.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<HTML>

<BODY bgcolor=cyan>

<FONT Size=2>

<FORM action="result.jsp" method="get" name=form>

猜大了,请再猜:<INPUT type="text" name="boy">

<INPUT TYPE="submit" value="送出" name="submit">

</FORM>

</FONT></BODY></HTML>


实验结果图:

实验五:使用文件字节流读写文件


giveContent.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="file" %>

<head>

<A href="giveContent.jsp">我要写文件</A>

<A href="lookContent.jsp">我要读文件</A>

</head>

<HTML><BODY bycolor=yellow>

<Font size=2>

<FORM action="writeContent.jsp" method=post>

请选择一个目录:

<Select name="fileDir">

<Option value="C:/1000">C:/1000

<Option value="D:/2000">D:/2000

<Option value="D:/1000">D:/1000

</SELECT>

<BR>输入保存文件的名字: <Input type=text name="fileName">

<BR>输入文件的内容:<BR>

<TextArea name="fileContent" Rows="5" Cols="38"></TextArea>

<BR><Input type=submit value="提交">

</FORM></FONT></BODY></HTML>

writeContent.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="file" %>

<HTML><BODY bycolor=cyan>

<Font size=2>

<% String fileDir=request.getParameter("fileDir");

    String fileName=request.getParameter("fileName");

    String fileContent=request.getParameter("fileContent");

    byte c[]=fileContent.getBytes("iso-8859-1");

     fileContent=new String(c);

%>

<file:Write fileDir="<%= fileDir %>" fileName="<%= fileName %>"

      fileContent="<%= fileContent %>" />

</FONT></BODY></HTML>

lookContent.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<head>

<A href="giveContent.jsp">我要写文件</A>

<A href="lookContent.jsp">我要读文件</A>

</head>

<HTML><BODY bycolor=yellow>

<Font size=2>

<FORM action="readContent.jsp" method="post" name="from">

输入文件的路径(如:d:/1000):<INPUT type="text" name="fileDir">

<BR>输入文件的名字(如:Hello.java): <Input type="text" name="fileName">

<BR><Input type="submit" value="读取" name="submit">

</FORM></FONT></BODY></HTML>

readContent.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="file" %>

<HTML><BODY bycolor=cyan>

<Font size=2>

<% String fileDir=request.getParameter("fileDir");

    String fileName=request.getParameter("fileName");

  

%>

<file:Read fileDir="<%= fileDir %>" fileName="<%= fileName %>" />

</FONT></BODY></HTML>

Write.tag

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.io.*" %>

<%@ attribute name="fileContent" required="true" %>

<%@ attribute name="fileDir" required="true" %>

<%@ attribute name="fileName" required="true" %>

<%

   File f=new File(fileDir,fileName);

    try{

       FileOutputStream output=new FileOutputStream(f);

        byte bb[]=fileContent.getBytes();

        output.write(bb,0,bb.length);

        output.close();

        out.println("文件写入成功!");

         out.println("<br>文件所在目录:"+fileDir);

          out.println("<br>文件的名字:"+fileName);

        }

     catch(IOException e){

          out.println("文件写入失败"+e);

         }

%>

Read.tag

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.io.*" %>

<%@ attribute name="fileDir" required="true" %>

<%@ attribute name="fileName" required="true" %>

<%

   File dir=new File(fileDir);

   File f=new File(dir,fileName);

    try{

       FileInputStream in=new FileInputStream(f);

       int m=-1;

        byte bb[]=new byte[1024];

        String content=null;

        while((m=in.read(bb))!=-1){

             content=new String(bb,0,m);

             out.println(content);

                }

           in.close();

        }

     catch(IOException e){

          out.println("文件读取失败"+e);

         }

%>


实验结果图:

实验六:JSP中使用数据库


1.更新记录

Example6_8.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire" %>

<HTML><BODY bgcolor=pink><FONT size=2>

<FORM action="newResult.jsp" method=post>

<table border=1>

<tr><td>输入要更新的产品的产品号:</td>

<td><Input type="text" name="number"></td></tr>

<tr><td>输入新的名称:</td><td><Input type="text" name="name"></td></tr>

<tr><td>输入新的生产日期:</td><td><Input type="text" name="madeTime"></td>

</tr>

<tr><td>输入新的价格:</td><td><Input type="text" name="price"></td></tr>

</table>

<BR><Input type="submit" name="b" value="提交更新">

<BR>product表更新前的记录是:

<inquire:QueryTag dataSource="mystar" tableName="product" user="sa" password="sa"/>

<BR><%=queryResult%>

</Font></BODY></HTML>

NewResult.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="renew" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire" %>

<HTML><BODY bgcolor=cyan><Font size=2>

<%  String nu=request.getParameter("number");

    String na=request.getParameter("name");

    String mT=request.getParameter("madeTime");

    String pr=request.getParameter("price");

    byte bb[]=na.getBytes("iso-8859-1");

    na=new String(bb);

%>

<renew:NewRecord number="<%=nu%>" name="<%=na%>" madeTime="<%=mT%>" price="<%=pr%>"/>

<BR>product表更新后的数据记录是:

<inquire:QueryTag dataSource="mystar" tableName="product" user="sa" password="sa"/>

<BR><%=queryResult%>

</Font></BODY></HTML>

NewRecord.jsp

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="number" required="true" %>

<%@ attribute name="name" required="true" %>

<%@ attribute name="madeTime" required="true" %>

<%@ attribute name="price" required="true" %>

<%  float p=Float.parseFloat(price);

    String condition1="UPDATE product SET name='"+name+"'WHERE number="+"'"+number+"'",

           condition2="UPDATE product SET madeTime='"+madeTime+"'WHERE number="+"'"+number+"'",

           condition3="UPDATE product SET price='"+price+"'WHERE number="+"'"+number+"'";

     try{  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

     catch(Exception e){}

     Connection con;

     Statement sql;

     ResultSet rs;

     try{  String uri="jdbc:odbc:mystar";

           con=DriverManager.getConnection(uri,"sa","sa");

           sql=con.createStatement();

           sql.executeUpdate(condition1);

           sql.executeUpdate(condition2);

           sql.executeUpdate(condition3);

           con.close();

}

catch(Exception e){

      out.print(""+e);

}

%>



2.添加记录

Example6_9.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire" %>

<HTML><BODY bgcolor=pink><FONT size=2>

<FORM action="newDatabase.jsp" method=post>

添加新纪录:

<table border=1>

<tr><td>输入产品号:</td><td><Input type="text" name="number"></td></tr>

<tr><td>输入名称:</td><td><Input type="text" name="name"></td></tr>

<tr><td>输入生产日期:</td><td><Input type="text" name="madetime"></td></tr>

<tr><td>输入价格:</td><td><Input type="text" name="price"></td></tr>

</table>

<BR><Input type="submit" name="b" value="提交">

<BR>product表添加新纪录前的记录是:

<inquire:QueryTag dataSource="mystar" tableName="product" user="sa" password="sa"/>

<BR><%=queryResult%><%-- queryResult是例6-3中Tag文件返回的对象 --%>

</Font></BODY></HTML>

newDatabase.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire" %>

<HTML><BODY bgcolor=cyan><Font size=2>

<%  String nu=request.getParameter("number");

    String na=request.getParameter("name");

    String mT=request.getParameter("madeTime");

    String pr=request.getParameter("price");

    byte bb[]=na.getBytes("iso-8859-1");

    na=new String(bb);

%>

<inquire:AddRecord number="<%=nu%>" name="<%=na%>" madeTime="<%=mT%>" price="<%=pr%>"/>

<BR>product表添加新纪录后的数据记录是:

<inquire:QueryTag dataSource="mystar" tableName="product" user="sa" password="sa"/>

<BR><%=queryResult%><%-- queryResult是例6-3中Tag文件返回的对象 --%>

</Font></BODY></HTML>

AddRecord.tag

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="number" required="true" %>

<%@ attribute name="name" required="true" %>

<%@ attribute name="madeTime" required="true" %>

<%@ attribute name="price" required="true" %>

<%  float p=Float.parseFloat(price);

    String condition="INSERT INTO product VALUES"+"("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";

     try{  Class.forName("sun.jdbc.odbcJdbcOdbcDriver");

}

     catch(Exception e){}

     Connection con;

     Statement sql;

     ResultSet rs;

     try{  String uri="jdbc:odbc:mystar";

           con=DriverManager.getConnection(uri,"sa","sa");

           sql=con.createStatement();

           sql.executeUpdate(condition);

           con.close();

}

catch(Exception e){

      out.print(""+e);

}

 %>


3.删除记录


Example6_10.jap

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="number" required="true" %>

<%@ attribute name="name" required="true" %>

<%@ attribute name="madeTime" required="true" %>

<%@ attribute name="price" required="true" %>

<%  float p=Float.parseFloat(price);

    String condition="INSERT INTO product VALUES"+"("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";

     try{  Class.forName("sun.jdbc.odbcJdbcOdbcDriver");

}

     catch(Exception e){}

     Connection con;

     Statement sql;

     ResultSet rs;

     try{  String uri="jdbc:odbc:mystar";

           con=DriverManager.getConnection(uri,"sa","sa");

           sql=con.createStatement();

           sql.executeUpdate(condition);

           con.close();

}

catch(Exception e){

      out.print(""+e);

}

 %>

Delete.jsp

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="number" required="true" %>

<%@ attribute name="name" required="true" %>

<%@ attribute name="madeTime" required="true" %>

<%@ attribute name="price" required="true" %>

<%  float p=Float.parseFloat(price);

    String condition="INSERT INTO product VALUES"+"("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";

     try{  Class.forName("sun.jdbc.odbcJdbcOdbcDriver");

}

     catch(Exception e){}

     Connection con;

     Statement sql;

     ResultSet rs;

     try{  String uri="jdbc:odbc:mystar";

           con=DriverManager.getConnection(uri,"sa","sa");

           sql=con.createStatement();

           sql.executeUpdate(condition);

           con.close();

}

catch(Exception e){

      out.print(""+e);

}

 %>

DelRecord.tag

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="number" required="true" %>

<%@ attribute name="name" required="true" %>

<%@ attribute name="madeTime" required="true" %>

<%@ attribute name="price" required="true" %>

<%  float p=Float.parseFloat(price);

    String condition="INSERT INTO product VALUES"+"("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";

     try{  Class.forName("sun.jdbc.odbcJdbcOdbcDriver");

}

     catch(Exception e){}

     Connection con;

     Statement sql;

     ResultSet rs;

     try{  String uri="jdbc:odbc:mystar";

           con=DriverManager.getConnection(uri,"sa","sa");

           sql=con.createStatement();

           sql.executeUpdate(condition);

           con.close();

}

catch(Exception e){

      out.print(""+e);

}

 %>


更多相关推荐:
我做的PHOTOSHOP实验报告参考

实验报告一实验目的与要求掌握Photoshop的各种基本操作技巧并能够灵活使用处理各种文字图像二实验原理工具Photoshop软件中的钢笔工具移动工具缩放工具等命令调整抠出图片大小调整图层位置抠出所需图片将图片...

photoshop图像处理实验报告

云南大学软件学院实验报告序号:31实验老师:**实验名称:图像处理课程名称:数字媒体技术实验学号:***姓名:**一.实验名称:图像处理:二.实验目的:◆认识图像处理的原理并初步使用常用的图像处理工具Photo…

实验报告ps

甘肃政法学院本科学生实验报告姓名景红玉学院计算机科学学院专业计算机科学与技术班级20xx专升本实验课程名称多媒体技术及应用指导教师及职称王云峰开课时间20xx20xx学年一学期甘肃政法学院教务处印制

ps实验报告

一Photoshop的基本操作学号20xx12103姓名王胜楠专业电子信息工程成绩实验目的学习了Photoshop软件的基本工具后能运用软件结合自己的设计理念制作一个综合性作品实验内容要求能从不同图片中选取所需...

photoshop实验报告

景德镇陶瓷学院设计艺术学院实验报告课程名称计算机辅导设计姓名陈泽宇学号20xx10113304专业班级11装潢3班成绩教师魏文卿实验项目名称Photoshop基础知识与基本操作基本功能高级功能高级编辑技巧实验学...

ps实验报告

实验报告理工类20xx至20xx学年度第1学期课程名称图像处理系别班级经济贸易系10电子商务2班学号1004131008姓名章亚建授课教师周艳燕指导教师周艳燕实验项目一奥运五环制作三基色调制同组者填写日期实验日...

photoshop实验报告

景德镇陶瓷学院设计艺术学院实验报告课程名称photoshop姓名韩璐学号20xx10113421专业班级装潢四班成绩教师魏文卿实验项目名称Photoshop基础知识与基本操作基本功能高级功能高级编辑技巧实验学时...

简易photoshop代码数字图像处理实验报告

一一个简单的photoshop软件二设计目的数字图像处理就是用数字计算机及其他有关数字技术对图像进行处理以达到预期的目的随着计算机的发展图像处理技术在许多领域得到了广泛应用数字图像处理已成为电子信息通信计算机自...

ps实验报告创建和编辑选区(此轮的绘制)

XXX学院XX系实验报告20xx20xx学年第一学期课程名称photoshop平面设计实验名称创建和编辑选区专业年级学号姓名指导教师实验日期

ps实验报告 认识photoshop cs3

教育信息技术与传媒学院InstituteofEducationalInformationTechnologyandCommunication平面设计PhotoShop实验报告学号姓名日期教师评语日期年月日

实验报告ps化妆品

海南软件职业技术学院学生实验报告说明1学生必须认真地按照教师和实验要求填写学生实验报告2指导教师评语一栏由指导教师填写成绩评定为优良合格不合格四个等级其余栏目由学生本人填写

迷宫问题和过河问题实验报告

天津外国语大学国际商学院本科生课程论文课程名称论文题目姓名学号专业班级任课教师数据结构实验报告杨丽文1407114036信息管理与信息系统14707王斌老师20xx年6月年级14级目录一研究问题4二研究思路4三...

ps实验报告(31篇)