JSPServletLab AssignmentI.docx
- 文档编号:10019882
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:24
- 大小:438.52KB
JSPServletLab AssignmentI.docx
《JSPServletLab AssignmentI.docx》由会员分享,可在线阅读,更多相关《JSPServletLab AssignmentI.docx(24页珍藏版)》请在冰豆网上搜索。
JSPServletLabAssignmentI
1.Writeaservletthatprints"HelloWorld"
Selveletcode:
packagemyPackage.huangcaiwang;
importjava.io.*;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassHelloWorldextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
out.print("
Helloworld
");}
}
XMLCode(web.xml):
Output:
2.Writeaservletthatknowstowhomit'ssayinghello,ThisservletmustbecalledfromanHTMLpagetakingusernameasinput.(Usebothgetandpostmethod)
HTMLcode:
UserName:
Selveletcode:
packagemyPackage.huangcaiwang;
importjava.io.*;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassWhoSayingHelloextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
Stringname=request.getParameter("name");
out.print("
"+name+"issayingHello
");
out.println("usingthedoGETmethod");
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
Stringname=request.getParameter("name");
out.print("
"+name+"issayingHello
");
out.println("usingthedoPOSTmethod");
}
}
XMLCode(web.xml):
Output:
Changethecode“method=”get””into“method=”post””inhtml,thenshowtheoutput:
3.Writeaservletthatcountsanddisplaysthenumberoftimesithasbeenaccessedsincethelastserverreboot(Manythreads,oneservletinstance).
Selveletcode:
packagemyPackage.huangcaiwang;
importjava.io.*;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassCountsextendsHttpServlet{
staticintcounts=0;
publicvoidinit(ServletConfigconfig)throwsServletException
{
super.init(config);
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
++counts;
response.setContentType("text/html");
PrintWriterout=response.getWriter();
out.print("
thispagehasbeenaccessed"+counts+"times
");}
}
XMLCode(web.xml):
Output:
Everytimewhenyourefresh,thecouts’stimespulsone,thenoutput:
4.Writeaservletthatprintsthenameandvalueforallofitsinitparameters.
Selveletcode:
packagemyPackage.huangcaiwang;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassGetInitParametersextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
java.util.Enumerationparams=this.getServletConfig().getInitParameterNames();
while(params.hasMoreElements()){
Stringname=params.nextElement().toString();
Stringvalue=this.getServletConfig().getInitParameter(name);
out.println(name+"="+value);
out.println("
");
}
}
}
XMLCode(web.xml):
Output:
5.Writeaservletthatdisplaysinformationaboutitsserver(TheprocessiscalledSnooping).
Hints:
-publicStringServletRequest.getServerName()
publicintServletRequest.getServerPort()
publicStringServletContext.getServerInfo()
publicObjectServletContext.getAttribute(Stringname)
Selveletcode:
packagemyPackage.huangcaiwang;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassSoonpingextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
StringserverName=request.getServerName();
intserverPort=request.getServerPort();
StringServerInfo=getServletContext().getServerInfo();
getServletContext().setAttribute("Color","red");
Objectattribute=getServletContext().getAttribute("Color");
out.print("informationaboutmyserverareasfollows:
");
out.println("
");
out.print("serverName:
"+serverName);
out.print("
");
out.print("serverPort:
"+serverPort);
out.print("
");
out.print("ServerInfo:
"+ServerInfo);
out.print("
");
out.print("attribute:
"+attribute);
out.print("
");
}
}
XMLCode(web.xml):
Output:
6.Writeaservletthatsnoopstheserver'sservletandJavaversion.
Selveletcode:
packagemyPackage.huangcaiwang;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importmyPackage.huangcaiwang.VersionDetector;
publicclassSnoopVersionextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
PrintWriterout=response.getWriter();
out.println("ServletVersion:
"+VersionDetector.getServletVersion());
out.println("
");
out.println("JavaVersion:
"+VersionDetector.getJavaVersion());
}
}
JavacodeofVersionDetectorclass:
packagemyPackage.huangcaiwang;
//Copyright(C)2000-2001byJasonHunter
//Allrightsreserved.Useofthisclassislimited.
//PleaseseetheLICENSEformoreinformation.
/***
*AclasstodeterminethecurrentServletAPIversionnumber,andthe
*currentJDKversionnumber.Itlooksattheavailableclassesand
*variablestomakethedetermination.TheclasscandetectServlet
*APIversionsupto2.2,andJDKversionsupto1.3.
*
*Itcanbeusedlikethis:
*
*StringservletVersion=VersionDetector.getServletVersion();
*StringjavaVersion=VersionDetector.getJavaVersion();
*@authorJasonHunter,Copyright©2000
*@version1.3,2002/04/22,addeddetectionofJDK1.5andServletAPI2.4
*@version1.2,2001/04/11,addeddetectionofJDK1.4
*@version1.1,2000/09/22,addeddetectionofServletAPI2.3
*@version1.0,2000/02/08
*/
publicclassVersionDetector{
staticStringservletVersion;
staticStringjavaVersion;
/***
*DeterminestheServletAPIversionnumber.
*@returnaStringrepresentationoftheservletversion
*/
publicstaticStringgetServletVersion(){
if(servletVersion!
=null){
returnservletVersion;
}
//Determinetheservletversionbylookingatavailableclasses
//andvariables
//javax.servlet.http.HttpSessionwasintroducedinServletAPI2.0
//javax.servlet.RequestDispatcherwasintroducedinServletAPI2.1
//javax.servlet.http.HttpServletResponse.SC_EXPECTATION_FAILEDwas
//introducedinServletAPI2.2
//javax.servlet.FilterisslatedtobeintroducedinServletAPI2.3
//CountupversionsuntilaNoClassDefFoundErrororNoSuchFieldException
//endsthetry
Stringver=null;
try{
ver="1.0";
Class.forName("javax.servlet.http.HttpSession");
ver="2.0";
Class.forName("javax.servlet.RequestDispatcher");
ver="2.1";
Class.forName("javax.servlet.http.HttpServletResponse")
.getDeclaredField("SC_EXPECTATION_FAILED");
ver="2.2";
Class.forName("javax.servlet.Filter");
ver="2.3";
Class.forNam
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JSP ServletLab AssignmentI
![提示](https://static.bdocx.com/images/bang_tan.gif)