ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:137.52KB ,
资源ID:3607748      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3607748.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验七 会话编程实验学生.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验七 会话编程实验学生.docx

1、实验七 会话编程实验学生实验七 会话编程实验实验目的: 掌握会话的基本知识熟悉会话的生命周期 掌握Cookie和Session实现会话跟踪实验内容:1、 假设学生登录一个图书管理系统,在登录界面上提供学生输入账号、密码、验证码(可以使用Servlet生成4位随机数的验证码,并保存在会话对象中,同时生成带干扰的验证码图片,登录页面使用此Servlet生成的验证码),并给学生选择是否保存Cookie,如果保存,选择保存时间。登录成功后,跳转到的页面上显示学生的账号信息,学生也可以自己退出系统。实验操作步骤1 新建一个Web Project,工程名为ExperiSevenCookieSession,

2、新建一个登录页面文件login.jsp,设置提示学生用户登录的账号、密码、验证码框,复选框提示用户是否决定选择Cookie的保存时间。界面如图1所示。login.jsp的代码如下: 登录页面 用户名: 密  码: 验证码: 一天 一周 一月 时间内不再重新登录 图1 登录界面2 在src目录下新建一个Servlet,类名CheckCodeGet,用于生成验证码,验证码是随机生成的数字,CheckCodeGet.java代码如下:package cn.javaee.session;import java.awt.Color;import java.awt.Font;import jav

3、a.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRespo

4、nse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;/本Servlet的功能用于生成验证码public class CheckCodeGet extends HttpServlet private static final long serialVersionUID=1L; private final int TYPE_NUMBER=0; private final int TYPE_LETTER=1; private final int TYPE_MULT

5、IPLE=2; private int width; private int height; private int count; private int type; private String validate_code; private Random random; private Font font; private int line; /* * Constructor of the object. */ public CheckCodeGet() super(); /* * Destruction of the servlet. */ public void destroy() su

6、per.destroy(); / Just puts destroy string in log / Put your code here /* * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * param request the request send by the client to the server * param response the response send by the server to

7、 the client * throws ServletException if an error occurred * throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setHeader(Pragma, No-cache); response.setHeader(Cache-Control,no-cache); r

8、esponse.setDateHeader(Expires,0);/ 不过期 response.setContentType(image/jpeg); /响应类型为图片 String reqCount=request.getParameter(count); String reqWidth=request.getParameter(width); String reqHeight=request.getParameter(height); String reqType=request.getParameter(type); if(reqCount!=null & reqCount!=) thi

9、s.count=Integer.parseInt(reqCount); if(reqWidth!=null & reqWidth!=) this.width=Integer.parseInt(reqWidth); if(reqHeight!=null & reqHeight!=) this.height=Integer.parseInt(reqHeight); if(reqType!=null & reqType!=) this.type=Integer.parseInt(reqType); font =new Font(Courier New,Font.BOLD,width/count);

10、BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics g=image.getGraphics(); g.setColor(getRandColor(160,240); g.fillRect(0, 0, width, height); g.setColor(getRandColor(20,100); g.setFont(font); validate_code=getValidateCode(count,type); request.getSession().setAttr

11、ibute(validate_code,validate_code); /将验证码存放到Session中,用于和输入的进行比较 for (int i=0;i255) from=255; if (to255) to=255; int rang=Math.abs(to-from); int r=from+random.nextInt(rang); / 红色 int g=from+random.nextInt(rang); / 蓝色 int b=from+random.nextInt(rang); / 绿色 return new Color(r,g,b); /获得验证码字符串 private Str

12、ing getValidateCode(int size,int type) StringBuffer validate_code=new StringBuffer(); for (int i=0;isize;i+) validate_code.append(getOneChar(type); return validate_code.toString(); / 根据验证码类型取得实际验证字符 / 类型0-取得0-9之间的数字 / 类型1-取得A-Z之间的字母 / 类型2-随机取得数字或是字母的组合 private String getOneChar(int type) String resu

13、lt=null; switch(type) case TYPE_NUMBER: result=String.valueOf(random.nextInt(10); break; case TYPE_LETTER: result=String.valueOf(char)(random.nextInt(26)+65); break; case TYPE_MULTIPLE: if (random.nextBoolean() result=String.valueOf(random.nextInt(10); else result=String.valueOf(char)(random.nextInt

14、(26)+65); break; default: result=null; break; if(result=null) throw new NullPointerException(获取验证码出错); return result; /* * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to post. * * param request the request send by the client to the server *

15、 param response the response send by the server to the client * throws ServletException if an error occurred * throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doGet(request,response); /* * In

16、itialization of the servlet. * * throws ServletException if an error occurs */ public void init(ServletConfig config) throws ServletException super.init(config); width=150; height=50; count=4; type=TYPE_NUMBER; random=new Random(); line=200; 3 配置web.xml,添加对CheckCodeGet的配置,配置代码如下: This is the descrip

17、tion of my J2EE component This is the display name of my J2EE component CheckCodeGet cn.javaee.session.CheckCodeGet CheckCodeGet /CheckCodeGet 4 可以发布工程进行测试登录页面,能够打开如图1所示的界面。输入如图2所示的正确结,点击“提交”按钮后,读取数据库中的记录,查找和用户账号及密码匹配的记录,如果找到,转到主页mainPage.jsp,提示用户登录成功,如图3所示。如果找不到,再次转到登录界面。在src下新建一个Servelt类LoginServl

18、et用于处理“提交”表单后的处理,LoginServlet.java的代码如下:package cn.javaee.session;import java.io.IOException;import java.io.PrintWriter;import .URLEncoder;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.servlet.Se

19、rvletConfig;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet priv

20、ate Connection cn = null; public LoginServlet() super(); / 销毁方法,关闭数据库连接,释放对象占用内存 public void destroy() / TODO Auto-generated method stub super.destroy(); try if (cn != null & (!cn.isClosed() cn.close(); cn = null; catch (Exception e) System.out.println(Destroy Error: + e.getMessage(); public void do

21、Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request, response); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException String userID = request.getParameter(userName); String passw

22、ord = request.getParameter(password); String sql = select * from student_info where stu_id = + userID + ; Statement st = null; try st = cn.createStatement(); catch (SQLException e1) / TODO Auto-generated catch block e1.printStackTrace(); ResultSet rs = null; try rs = st.executeQuery(sql); catch (SQL

23、Exception e) / TODO Auto-generated catch block e.printStackTrace(); try if (rs.next() String dbPassword = rs.getString(stu_pwd).toString(); String dbUserName=rs.getString(stu_name).toString(); if (dbPassword.equals(password) / 输入的密码正确 String cookSelected = request.getParameter(chkSelect); if (cookSe

24、lected != null) / 设置Cookie的保存时间 Cookie name = new Cookie(myname,URLEncoder.encode(userID,utf-8); Cookie pass = new Cookie(mypass,URLEncoder.encode(password,utf-8); String ageString=new String(request.getParameter(selectTime).getBytes(ISO-8859-1),UTF-8); /System.out.println(ageString); if (ageString.

25、equals(一天) name.setMaxAge(24*60*60); pass.setMaxAge(24*60*60); else if (ageString.equals(一周) name.setMaxAge(7*24*60*60); pass.setMaxAge(7*24*60*60); else if(ageString.equals(一月) /假定一月就按30天算,不计详细的哪个月份 name.setMaxAge(30*24*60*60); pass.setMaxAge(30*24*60*60); response.addCookie(name); response.addCookie(pass); HttpSession session=request.getSession(true); session.setAttribute(userName,dbUserName);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1