JSPServletLab AssignmentIWord下载.docx
- 文档编号:5830789
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:24
- 大小:438.52KB
JSPServletLab AssignmentIWord下载.docx
《JSPServletLab AssignmentIWord下载.docx》由会员分享,可在线阅读,更多相关《JSPServletLab AssignmentIWord下载.docx(24页珍藏版)》请在冰点文库上搜索。
"
}
}
XMLCode(web.xml):
<
servlet>
servlet-name>
HelloWorld<
/servlet-name>
servlet-class>
myPackage.huangcaiwang.HelloWorld<
/servlet-class>
/servlet>
servlet-mapping>
url-pattern>
/HelloWorld<
/url-pattern>
/servlet-mapping>
Output:
2.Writeaservletthatknowstowhomit'
ssayinghello,ThisservletmustbecalledfromanHTMLpagetakingusernameasinput.(Usebothgetandpostmethod)
HTMLcode:
html>
head>
title>
myInput.html<
/title>
/head>
body>
formmethod="
get"
action="
WhoSayingHello"
>
UserName:
inputtype="
text"
name="
name"
/>
br>
submit"
value="
/form>
/body>
/html>
publicclassWhoSayingHelloextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
Stringname=request.getParameter("
"
+name+"
issayingHello<
out.println("
usingthedoGETmethod<
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
usingthedoPOSTmethod<
WhoSayingHello<
myPackage.WhoSayingHello<
/WhoSayingHello<
Changethecode“method=”get””into“method=”post””inhtml,thenshowtheoutput:
3.Writeaservletthatcountsanddisplaysthenumberoftimesithasbeenaccessedsincethelastserverreboot(Manythreads,oneservletinstance).
importjavax.servlet.ServletConfig;
publicclassCountsextendsHttpServlet{
staticintcounts=0;
publicvoidinit(ServletConfigconfig)throwsServletException
{
super.init(config);
++counts;
thispagehasbeenaccessed"
+counts+"
times<
Counts<
myPackage.huangcaiwang.Counts<
/Counts<
Everytimewhenyourefresh,thecouts’stimespulsone,thenoutput:
4.Writeaservletthatprintsthenameandvalueforallofitsinitparameters.
importjava.io.IOException;
importjava.io.PrintWriter;
publicclassGetInitParametersextendsHttpServlet{
java.util.Enumerationparams=this.getServletConfig().getInitParameterNames();
while(params.hasMoreElements()){
Stringname=params.nextElement().toString();
Stringvalue=this.getServletConfig().getInitParameter(name);
out.println(name+"
="
+value);
out.println("
}
GetInitParameters<
myPackage.huangcaiwang.GetInitParameters<
<
init-param>
<
param-name>
class<
/param-name>
param-value>
Ecom612<
/param-value>
/init-param>
/GetInitParameters<
5.Writeaservletthatdisplaysinformationaboutitsserver(TheprocessiscalledSnooping).
Hints:
-publicStringServletRequest.getServerName()
publicintServletRequest.getServerPort()
publicStringServletContext.getServerInfo()
publicObjectServletContext.getAttribute(Stringname)
publicclassSoonpingextendsHttpServlet{
StringserverName=request.getServerName();
intserverPort=request.getServerPort();
StringServerInfo=getServletContext().getServerInfo();
getServletContext().setAttribute("
Color"
"
red"
Objectattribute=getServletContext().getAttribute("
informationaboutmyserverareasfollows:
serverName:
+serverName);
serverPort:
+serverPort);
ServerInfo:
+ServerInfo);
attribute:
+attribute);
Soonping<
myPackage.huangcaiwang.Soonping<
/Soonping<
6.Writeaservletthatsnoopstheserver'
sservletandJavaversion.
importmyPackage.huangcaiwang.VersionDetector;
publicclassSnoopVersionextendsHttpServlet{
ServletVersion:
+VersionDetector.getServletVersion());
JavaVersion:
+VersionDetector.getJavaVersion());
JavacodeofVersionDetectorclass:
//Copyright(C)2000-2001byJasonHunter<
jhunter_AT_acm_DOT_org>
.
//Allrightsreserved.Useofthisclassislimited.
//PleaseseetheLICENSEformoreinformation.
/***
*AclasstodeterminethecurrentServletAPIversionnumber,andthe
*currentJDKversionnumber.Itlooksattheavailableclassesand
*variablestomakethedetermination.TheclasscandetectServlet
*APIversionsupto2.2,andJDKversionsupto1.3.
*<
p>
*Itcanbeusedlikethis:
blockquote>
pre>
*StringservletVersion=VersionDetector.getServletVersion();
*StringjavaVersion=VersionDetector.getJavaVersion();
*@author<
b>
JasonHunter<
/b>
Copyright&
#169;
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"
2.0"
Class.forName("
javax.servlet.RequestDispatcher"
2.1"
javax.servlet.http.HttpServletResponse"
)
.getDeclaredField("
SC_EXPECTATION_FAILED"
2.2"
javax.servlet.Filter"
2.3"
Class.forNam
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JSP ServletLab AssignmentI