if(userList[i][0].equals(username)){
if(userList[i][1].equals(pwd)){
flag=true;
break;
}
}
}
if(flag){
session.setAttribute("username",username);
response.sendRedirect("main.jsp");
}else{
response.sendRedirect("index.jsp");
}
%>
实验三javabean使用
打开指定大小的窗口
packagecom.lh.bean;
publicclassShowWindow{
privateStringurl;
privateStringopenWindowStr="";
privateintwidth;
privateintheight;
privateStringfunctionName;
publicStringgetUrl(){
returnurl;
}
publicvoidsetUrl(Stringurl){
this.url=url;
}
publicStringgetOpenWindowStr(){
StringBuffersb=newStringBuffer(openWindowStr);
sb.append("");
sb.append("\r\n\t");
sb.append("function"+this.functionName+"(){");
sb.append("\r\n\t\t");
sb.append("varreturnObj=window.open('"+this.url+"','window','width="+this.width+"px,height="+this.height+"px');");
sb.append("\r\n\t\t");
sb.append("varx=(screen.width-"+width+")/2;");
sb.append("\r\n\t\t");
sb.append("vary=(screen.height-"+height+")/2;");
sb.append("\r\n\t\t");
sb.append("returnObj.moveTo(x,y);");
sb.append("\r\n\t}");
sb.append("\r\n");
sb.append("");
returnsb.toString();
}
publicvoidsetOpenWindowStr(StringopenWindowStr){
this.openWindowStr=openWindowStr;
}
publicintgetWidth(){
returnwidth;
}
publicvoidsetWidth(intwidth){
this.width=width;
}
publicintgetHeight(){
returnheight;
}
publicvoidsetHeight(intheight){
this.height=height;
}
publicStringgetFunctionName(){
returnfunctionName;
}
publicvoidsetFunctionName(StringfunctionName){
this.functionName=functionName;
}
publicstaticvoidmain(String[]args){
ShowWindows=newShowWindow();
s.setFunctionName("openWindow");
s.setUrl("index.jsp");
s.setWidth(500);
s.setHeight(500);
System.out.println(s.getOpenWindowStr());
}
}
实验4用servlet生成动态验证码
packagecom.lh.servlet;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Random;
importjavax.imageio.ImageIO;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassValidateCodeServletextendsHttpServlet{
/**
*Constructoroftheobject.
*/
publicValidateCodeServlet(){
super();
}
/**
*Destructionoftheservlet.
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*ThedoGetmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
this.doPost(request,response);
}
/**
*ThedoPostmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicColorgetRandomColor(intfc,intbc){
Randomrandom=newRandom();
ColorrandomColor=null;
if(fc>255)fc=255;
if(bc>255)bc=255;
//设置个0-255之间的随机颜色值
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
randomColor=newColor(r,g,b);
returnrandomColor;
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","No-cache");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
intwidth=60,height=20;
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=image.getGraphics();
Randomrandom=newRandom();
g.setColor(getRandomColor(200,250));
g.fillRect(0,0,width,height);
g.setFont(newFont("TimesNewRoman",Font.PLAIN,18));
g.setColor(getRandomColor(160,200));
for(inti=0;i<130;i++){
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(12);
intyl=random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
StringstrCode="";
for(inti=0;i<4;i++){
StringstrNumber=String.valueOf(random.nextInt(10));
strCode=strCode+strNumber;
g.setColor(newColor(15+random.nextInt(120),15+random.nextInt(120),15+random.nextInt(120)));
g.drawString(strNumber,13*i+6,16);
}
request.getSession().setAttribute("Code",strCode);
g.dispose();
ImageIO.write(image,"JPEG",response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
/**
*Initializationoftheservlet.
*
*@throwsServletExceptionifanerroroccurs
*/
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
实验5字符编码过滤器
packagecom.mr.encoding;
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
publicclassEncodingFilterimplementsFilter{
privateStringEncoding;//在醒置web.xml编码
privatebooleanenabled;//是否启用Filter
publicvoidinit(FilterConfigconfig)throwsServletException{
Encoding=config.getInitParameter("Encoding");//编码方式
enabled="true".equalsIgnoreCase(Encoding.trim())//启用此FIlter
||"1".equalsIgnoreCase(Encoding.trim());
}
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,
FilterChainchain)throwsIOException,ServletException{
if(enabled||Encoding!
=null){//如果启用了此Filter
request.setCharacterEncoding(Encoding);//request的编码
response.setCharacterEncoding(Encoding);//response的编码
}
chain.doFilter(request,response);//继续执行下一个Filter
}
publicvoiddestroy(){
Encoding=null;
}
}
实验六
packagecom.lh.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassLoginServletextendsHttpServlet{
/**
*Constructoroftheobject.
*/
publicLoginServlet(){
super();
}
/**
*Destructionoftheservlet.
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*ThedoGetmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
this.doPost(request,response);
}
/**
*ThedoPostmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.