JSP第七周作业
1.教材P78-79 例4-9
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body bgcolor=#ffccff> <% double p = 98.78; %> <p>商品编号A1001,价格 8765</p> <a href="receive.jsp?id=A1001&price=8765">购买</a> <p>商品编号A1002,价格</p> <a href="receive.jsp?id=A1002&price=<%=p%>">购买</a> </body> </html>

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body bgcolor=#eeeeff>
<%
String id = request.getParameter("id");
String price = request.getParameter("price");
%>
<b>商品编号:<%=id%></b>
<br>
<b>商品价格:<%=price%></b>
</body>
</html>

2.教材P97 实验2
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body bgcolor=#ffccff> <form action="computer.jsp" method=post> <p> 输入运算数,选择运算符号:<br> <input type=text name="no" size=6 /> <select name="oa"> <option selected="selected" value="+">加 <option value="-">减 <option value="*">乘 <option value="/">除 </select> <input type=text name="nt" size=6 /> <br> <input type="submit" name="submit" value="提交"> </p> </form> </body> </html>

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body bgcolor=cyan>
<p>
<%
String no = request.getParameter("no");
String nt = request.getParameter("nt");
String oa = request.getParameter("oa");
if (no == null || no.length() == 0) {
response.sendRedirect("input.jsp");
return;
} else if (nt == null || nt.length() == 0) {
response.sendRedirect("input.jsp");
return;
}
try {
double a = Double.parseDouble(no);
double b = Double.parseDouble(nt);
double r = 0;
if (oa.equals("+")) {
r = a + b;
} else if (oa.equals("-")) {
r = a - b;
} else if (oa.equals("*")) {
r = a * b;
} else if (oa.equals("/")) {
r = a / b;
}
out.print(a + oa + b + "=" + r);
} catch (Exception e) {
}
%>
</p>
</body>
</html>

3.制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
4.在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”。
5.在页面1的表单内输人一个数字N,提交,能够在另一个页面打印N个“欢迎”字符串。

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function reloadCode() {
var time = new Date().getTime();
document.getElementById("imagecode").src = "code?d=" + time;
}
function login() {
if (formtext.account.value == "") {
alert('账号不能为空');
return;
}
if (formtext.password.value == "") {
alert('密码不能为空');
return;
}
formtext.submit();
}
</script>
<form name="formtext" action="loginServlet" method="post">
<b>账号</b> <input type="text" name="account" /> <br> <b>密码</b> <input
type="password" name="password" /> <br> <b>是否注册为会员</b> <input
type="checkbox" name="vip" value="vip"> <br> <b>输入欢迎次数</b>
<input type="text" name="number" value="0"> <br> <b>验证码</b>
<input type="text" name="tjcode" /> <img alt="验证码" id="imagecode"
src="code"> <a href="#" onclick="reloadCode()">换一个</a> <br>
<input type="button" onclick="login()" value="登录" />
</form>
</body>
</html>
package servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CodeServlet
*/
@WebServlet("/code")
public class CodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
Color c = new Color(200, 150, 255);
g.setColor(c);
g.fillRect(0, 0, 68, 22);
String ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random r = new Random();
int len = ch.length();
int index;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
index = r.nextInt(len);
g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt(255)));
g.setFont(new Font(null, Font.BOLD, 24));
g.drawString(ch.substring(index, index + 1) + "", (i * 15) + 3, 18);
sb.append(ch.substring(index, index + 1) + "");
}
response.setContentType("image/jpg");
request.getSession().setAttribute("pic", sb.toString());
// System.out.println(""+request.getAttribute("pic"));
OutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
out.close();
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "loginServlet", urlPatterns = { "/loginServlet" })
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String piccode = (String) request.getSession().getAttribute("pic");
String checkcode = request.getParameter("tjcode");
String account = request.getParameter("account");
String password = request.getParameter("password");
String vip = request.getParameter("vip");
request.setAttribute("account", account);
request.setAttribute("password", password);
request.setAttribute("vip", vip);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
if (checkcode.equals(piccode)) {
request.getRequestDispatcher("verification.jsp").forward(request, response);
} else {
out.flush();
out.println("<script type='text/javascript'>");
out.println("alert('验证码输入错误,请重新输入');");
out.println("history.back();");
out.println("</script>");
}
out.close();
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String account = (String) (request.getAttribute("account"));
String password = (String) (request.getAttribute("password"));
String vip = (String) (request.getAttribute("vip"));
if (account.equals(password)) {
%>
<b>姓名</b>
<input type="text" name="cname">
<%
request.getRequestDispatcher("ok.jsp").forward(request, response);
} else {
request.getRequestDispatcher("no.jsp").forward(request, response);
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录成功</h1>
<%
String vip = (String) (request.getAttribute("vip"));
if (vip != null) {
out.print("<h1>恭喜您注册为会员</h1>");
}
String number = request.getParameter("number");
String cname = request.getParameter("cname");
String account = request.getParameter("account");
if (number != null && number != "0" && number != "") {
int n = Integer.parseInt(number);
for (int i = 0; i < n; i++) {
out.print("<h1>欢迎</h1>");
}
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>密码错误,登录失败</h1> </body> </html>









6.在页面1中输入账号和密码,进行登录,如果账号和密码相同,则认为成功登录到页面2,在页面2中显示一个文本框输人用户姓名,输人之后提交,在页面3中显示用户的账号和姓名。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function login() {
if (formtext.caccount.value != formtext.cpassword.value) {
alert('账号与密码不同');
return;
}
formtext.submit();
}
</script>
<form name="formtext" action="setName.jsp" method="post">
<b>账号</b> <input type="text" name="caccount" /> <br> <b>密码</b> <input
type="password" name="cpassword" /> <br> <input type="button"
onclick="login()" value="登录" />
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<%
String account = request.getParameter("caccount");
%>
<form action="verificationthird.jsp" method="post">
<b>姓名</b><input type="text" name="cname"> <input
name="caccount" type="hidden" value="<%=account%>"> <input
type="submit" value="登录" />
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.getRequestDispatcher("twoShow.jsp").forward(request, response);
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<%
String account = request.getParameter("caccount");
String name = request.getParameter("cname");
%>
<h1>
账号:<%=account%></h1>
<h1>
姓名:<%=name%></h1>
</body>
</html>




浙公网安备 33010602011771号