Servlet开发
作业:防止重复注册
<%@ 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>
<form action="process.jsp">
用户:<input type="text" name="user"><br>
密码:<input type="password" name="pass"><br>
<input type="submit" value="注册">
<input type="reset" value="重置">
</form>
</body>
</html>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ 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 user = request.getParameter("user");
String pass = request.getParameter("pass");
List<String> list = (List<String>) application.getAttribute("userMessage");
boolean flag = true;
if (list == null) {
list = new ArrayList<String>();
} else {
if (list.contains(user)) {
flag = false;
response.sendRedirect("failed.jsp");
}
}
if (flag) {
list.add(user);
application.setAttribute("userMessage", list);
response.sendRedirect("success.jsp");
}
%>
</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>
注册成功!
</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>
注册失败!用户已经存在。
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>用户注册</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"> <style type="text/css"> table{ font-size:12px; font-family: 隶书; color:gray; border: 1px green solid; } input{ font-size:12px; font-family: 隶书; color:gray; } </style> </head> <body> <form action="/SerlvetTest/show" method="post"> <table align="center"> <tr> <td>用户名:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="pwd" /></td> </tr> <tr> <td>性别:</td> <td> <input type="radio" name="sex" value="男" />男 <input type="radio" name="sex" value="女" />女 </td> </tr> <tr> <td>年龄:</td> <td><input type="text" name="age" /></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>验证码:</td> <td> <img alt="网络加载失败" src="validatecode" > </td> </tr> <tr> <td>输入验证码:</td> <td> <input type="text" name="code"/> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="注 册" /> <input type="reset" value="重 置" /> </td> </tr> </table> </form> </body> </html>
package com.tx.demo6; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //原因分析:MyEclipse默认把这些访问受限的API当成了错误来处理 //解决办法:在MyEclipse中点Window——>Preferences——>Java——>Compiler——>Errors/Warnings, //展开Deprecated and restricted API,把Forbidden reference(access rules)后面的选项的Error改为Warning, import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ValidateCodeServlet extends HttpServlet { public ValidateCodeServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public Color getRandomColor(int fc, int bc) { Random random = new Random(); Color randomColor = null; if (fc > 255) fc = 255; if (bc > 255) bc = 255; // 设置个0-255之间的随机颜色值 int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); randomColor = new Color(r, g, b); return randomColor;// 返回具有指定红色、绿色和蓝色值的不透明的 sRGB 颜色 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //禁止页面缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); //设置响应正文的MIME类型为图片 int width=60, height=20; /**创建一个位于缓存中的图像,宽度60,高度20 */ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); //获取用于处理图形上下文的对象,相当于画笔 Random random = new Random(); //创建生成随机数的对象 g.setColor(getRandomColor(200,250)); //设置图像的背景色 g.fillRect(0, 0, width, height); //画一个矩形 ,坐标(0,0),宽度60,高度20 g.setFont(new Font("Times New Roman",Font.PLAIN,18)); //设定字体格式 g.setColor(getRandomColor(160,200)); for(int i=0;i<130;i++){ //产生130条随机干扰线 int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x,y,x+xl,y+yl); //在图象的坐标(x,y)和坐标(x+x1,y+y1)之间画干扰线 } String strCode=""; for (int i=0;i<4;i++){ String strNumber=String.valueOf(random.nextInt(10)); //把随机数转换成String字符串 strCode=strCode+strNumber; //设置字体的颜色 g.setColor(new Color(15+random.nextInt(120),15+random.nextInt(120),15+random.nextInt(120))); System.out.println(strCode); g.drawString(strNumber,13*i+6,16); //将验证码依次画到图像上,坐标(x=13*i+6,y=16) } request.getSession().setAttribute("Code",strCode); //把验证码保存到Session中 g.dispose(); //释放此图像的上下文以及它使用的所有系统资源 ImageIO.write(image, "JPEG", response.getOutputStream()); //输出JPEG格式的图像 response.getOutputStream().flush(); //刷新输出流 response.getOutputStream().close(); //关闭输出流 } public void init() throws ServletException { } }
package com.tx.demo6; 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(value="/show") public class ShowServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String code = request.getParameter("code"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); String sex = request.getParameter("sex"); String age = request.getParameter("age"); String email = request.getParameter("email"); String servicecode = (String)request.getSession().getAttribute("Code"); PrintWriter pw = response.getWriter(); if(servicecode.equals(code)){ pw.println("账号:"+name); pw.println("密码:"+pwd); pw.println("性别:"+sex); pw.println("年龄:"+age); pw.println("邮箱:"+email); }else{ pw.println("验证码错误"); response.setHeader("refresh", "3;URL=jsp/demo6/index.jsp"); } pw.flush(); pw.close(); } }