您的位置:首页 > Web前端 > JavaScript

JSP大学实用教程(第2版)代码一

2016-04-13 13:22 537 查看
由于书本上的代码直接运行会有些小问题,所以贴一些改过的代码。

十九页【例2-9】
Input.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="gb2312"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>My JSP '1.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This
is my page">

<!--

<link rel="stylesheet" type="text/css"href="styles.css">

-->

</head>

<bodybgcolor=cyan>

<fontsize=3>

<formaction="2.jsp"method="post"name="form"/>

<p>请输入下列信息:<br>

输入您的姓名:

<inputtype="text"name="name"value="张三"></BR>

<br>

选择性别:

<inputtype="radio"name="R"value="男"checked="default">男

<inputtype="radio"name="R"value="女">女

<br>

选择您喜欢的歌手:

<inputtype="checkbox"name="superstar"value="张歌手">张歌手

<inputtype="checkbox"name="superstar"value="李歌手">李歌手

<inputtype="checkbox"name="superstar"value="刘歌手">刘歌手

<inputtype="checkbox"name="superstar"value="王歌手">王歌手

<br>

<inputtype="submit"value="提交"name="submit">

</form>

</font>

</html>

receive.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="gb2312"%>

<%

String path = request.getContextPath();

request.setCharacterEncoding("gb2312");

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>My JSP '2.jsp' starting page</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="This
is my page">

<!--

<link rel="stylesheet" type="text/css"href="styles.css">

-->

</head>

<body bgcolor=cyan>

<font size=3>

<% String yourName=request.getParameter("name"); // 获取text提交的值

String yourSex=request.getParameter("R"); //获取radio提交的值

String secretMess=request.getParameter("secret");
//获取hidden提交的值

String personName[]=request.getParameterValues("superstar"); //获取checkbox提交的值

out.println("<P> 您的姓名:"+yourName+"</P>");

out.println("<P> 您的性别:"+yourSex+"</P>");

out.println("<P> 您喜欢的歌手:");

if(personName==null){

out.print("一个都不喜欢");

}else {

for(int k=0;k<personName.length;k++){

out.println(""+personName[k]);

}

}

out.println("<P> hidden提交的值:"+secretMess);

%>

</font>

</body>

</html>

二十一页【例2-10】
example2_10.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'example2-10.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This
is my page">

<!--

<link rel="stylesheet" type="text/css"
href="styles.css">

-->

</head>

<body bgcolor=yellow>

<table align="Center"border=1>

<tr width=400>

<td align="Center">welcome</td>

<td align="Right">to</td>

<td align="LEFT">Beijing</td>

</tr>

<tr>

<th valign="Top">We</th>

<td valign="Bottom">Love</td>

<td valign="Bottom"Align="Center">JSP</td>

</tr>

<tr>

<td valign="Top"> 你好</td>

<td valign="Bottom">Hello</td>

<td valign="Bottom"Aligin="Center">how
are you</td>

</tr>

</table>

</body>

</html>

三十一页【例3-4】
Example3-4.jsp

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

<HTML><BODY
bgcolor=yellow>

<P>加载一个jsp文件,该文件负责计算连续整数之和:

<jsp:includepage="computer.jsp">

<jsp:paramname="item"value="100"/>

</jsp:include>

</BODY></HTML>

Computer.jsp

<%@pagecontentType="text/html;charset=GB2312"%><HTML><BODY>

<% String str=request.getParameter("item");
//获取值param标记中name属性的值

int n=Integer.parseInt(str);

int sum=0;

for(int i=1;i<=n;i++){

sum=sum+i;

}

out.println("<BR>从1到"+n+"的连续和是:</BR>"+sum);

%>

</BODY></HTML>

三十二页【例3-5】
Example3-5.jsp

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

<HTML><BODY>

<%

out.println("根据不同的值转向不同的页面:<BR>");

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

if(n>50){

%>

<jsp:forwardpage="num1.jsp">

<jsp:paramname="item"value="<%= n
%>"/>

</jsp:forward><%

} else {

%>

<jsp:forwardpage="num2.jsp">

<jsp:paramname="item"value="<%= n
%>"/>

</jsp:forward><%

}

out.println("看不见这句话");

%>

</BODY></HTML>

Num1.jsp

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

<HTML><BODY
bgcolor=cyan>

<%

String str=request.getParameter("item");
//获取值param标记中name属性的值

int n=Integer.parseInt(str);

out.println("<BR>不大于"+n+"的素数:</BR>");

int i=0,j=0;

for(i=1;i<=n;i++){

for(j=2;j<i;j++){

if(i%j==0)

break;

}

if(j==i)

out.println(","+i);

}

%>

</BODY></HTML>

Num2.jsp

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

<HTML><BODYbgcolor=cyan>

<%

String str=request.getParameter("item");
//获取值param标记中name属性的值

int n=Integer.parseInt(str);

out.println("<BR>不大于"+n+"奇数:</BR>");

for(int i=1;i<=n;i++) {

if(i%2!=0)

out.println(","+i);

}

%>

</BODY></HTML>

二十三页第五题
a.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'a.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<br>

<form action="b.jsp" method="get">

帐号:<input type="text" name="name" value="name"><br>

密码:<input type="text" name="password" value="password"><br>

<input type="submit" value="login">

</form>

</body>

</html>

b.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'b.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<font>

<br>

<%

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

String Password=request.getParameter("password");

if(Name==null){

out.print("未输入字符串");

}else{

%>

<%="你输入的字符串为:"+Name+"<br>字符串长度为:"+Name.length()

%><br>

<%="你输入的字符串为:"+Password+"<br>字符串长度为:"+Password.length()

%>

<%}

%>

</font>

</body>

</html>

二十三页第六题
InputNumber

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

<html>

<body>

<form action="getNumber.jsp" method="post" name="form">

<p>请输入一个数字:<br/>

<input type="text" name="number"><br>

<input type="submit" name="submit" value="提交">

</form>

</body>

</html>

getNumber

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

<html>

<body>

<br>

<%

int number=Integer.parseInt(request.getParameter("number"));

out.println(number+"的平方为:"+number*number+"<br>");

out.println(number+"的立方为:"+number*number*number+"<br>");

%>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: