javaweb上机实验报告.docx

上传人:b****6 文档编号:4717858 上传时间:2022-12-07 格式:DOCX 页数:23 大小:793.67KB
下载 相关 举报
javaweb上机实验报告.docx_第1页
第1页 / 共23页
javaweb上机实验报告.docx_第2页
第2页 / 共23页
javaweb上机实验报告.docx_第3页
第3页 / 共23页
javaweb上机实验报告.docx_第4页
第4页 / 共23页
javaweb上机实验报告.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

javaweb上机实验报告.docx

《javaweb上机实验报告.docx》由会员分享,可在线阅读,更多相关《javaweb上机实验报告.docx(23页珍藏版)》请在冰豆网上搜索。

javaweb上机实验报告.docx

javaweb上机实验报告

实验一JSP内置对象的应用

packagecom.count.stoptime;

importjava.util.*;

publicclassStopTime{

privateinth=0;

privateintm=0;

privateints=0;

publicStopTime(){}

publicvoidcounttime(Datestart){

Dateend=newDate();

longhowmuch=end.getTime()-start.getTime();

h=(int)(howmuch/1000/60/60);

howmuch=howmuch-h*60*60*1000;

m=(int)(howmuch/1000/60);

howmuch=howmuch-m*60*1000;

s=(int)(howmuch/1000);

}

publicintgetH(){

returnthis.h;

}

publicintgetM(){

returnthis.m;

}

publicintgetS(){

returnthis.s;

}

}

Index.jsp

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

<%@pageimport="java.util.*"%>

useBeanid="mycounttime"class="com.count.stoptime.StopTime"scope="page"/>

<%

session.setMaxInactiveInterval(11);

Datenow=newDate();

if(session.isNew()){

session.setAttribute("start",now);

}

else{

mycounttime.counttime((Date)session.getAttribute("start"));

}

%>

统计用户在某一页停留的时间

200">

统计用户在某一页停留的时间

您登录的时间为:

<%=((Date)session.getAttribute("start")).toLocaleString()%>

您在本页的停留时间为:

<%=mycounttime.getH()%>小时<%=mycounttime.getM()%>分<%=mycounttime.getS()%>秒

实验二

应用session对象实现用户登录

<%@pagelanguage="java"contentType="text/html;charset=GB18030"pageEncoding="GB18030"%>

<%@pageimport="java.util.*"%>

<%

String[][]userList={{"mr","mrsoft"},{"wgh","111"},{"sk","111"}};

booleanflag=false;

request.setCharacterEncoding("GB18030");

Stringusername=request.getParameter("username");

Stringpwd=request.getParameter("pwd");

for(inti=0;i

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.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

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

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