RAD上机课3.docx
《RAD上机课3.docx》由会员分享,可在线阅读,更多相关《RAD上机课3.docx(42页珍藏版)》请在冰豆网上搜索。
![RAD上机课3.docx](https://file1.bdocx.com/fileroot1/2023-6/9/a8a66df6-4d8b-4777-abe9-70d46d5dcd9d/a8a66df6-4d8b-4777-abe9-70d46d5dcd9d1.gif)
RAD上机课3
RAD上机课3(20130514)
本课内容
1.根据6-3编写程序跟踪Servlet生命周期
2.完成3-10对应代码
3.完成20-9对应代码
本课要用到的软件和几个路径
1.虚拟机VMwareWorkStation9.0
2.RAD6.0(RationalApplicationDeveloper)
3.默认的工作空间路径C:
\DocumentsandSettings\db2admin\IBM\rationalsdp6.0\workspace
自定义的工作空间路径C:
\DocumentsandSettings\db2admin\桌面\Sample
4.要用到的文件和文件夹的路径
第一个和第二个文件路径:
桌面\RAD\相关资料\rad\C4NUE_dev450_studentmanuals\rd792gv1_stuman1.pdf
第三个文件路径:
桌面\RAD\相关资料\rad\C4NUE_dev450_studentmanuals\rd792gv1_stuman2.pdf
其中6-3、3-10、20-9分别表示对应页码
第三次上机
这次上机全是自己做,坑
1.根据6-3编写程序跟踪Servlet生命周期
1.题目
6-3的内容:
Javaservletlifecycle(Javaservlet生命周期)
主要内容:
写Servlet的构造方法,重写Init方法、Service方法、HttpService方法、doGet方法、destory方法
2.操作
转到WEB透视图,新建动态WEB项目,名称填ServletExample_1,点完成
点项目名称(动态Web项目ServletExample_1),右键新建其它
在新建向导中,选择Webservlet,点下一步
名称填FirstServlet,点下一步
点下一步
点下一步
将Init和destory选上
点完成
动态Web项目ServletExample_1JavaResourcesJavaSource(缺省包)FirstServlet.java
将代码改成下面的形式,即用输出语句实现每个方法
importjava.io.IOException;
importjavax.servlet.Servlet;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassFirstServletextendsHttpServletimplementsServlet{
publicFirstServlet(){
super();
System.out.println("FirstServlet");
}
protectedvoiddoGet(HttpServletRequestarg0,HttpServletResponsearg1)throwsServletException,IOException{
System.out.println("doGet");
}
protectedvoiddoPost(HttpServletRequestarg0,HttpServletResponsearg1)throwsServletException,IOException{
System.out.println("doPost");
}
publicvoidinit(ServletConfigarg0)throwsServletException{
System.out.println("init");
}
publicvoiddestroy(){
System.out.println("destroy");
}
}
FirstServlet.java右键源代码覆盖/实现方法
勾选上HttpServlet中的两个service方法,不过它们的参数不同
勾选上GenericServlet中的Init方法
最终改完的代码
importjava.io.IOException;
importjavax.servlet.Servlet;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassFirstServletextendsHttpServletimplementsServlet{
publicFirstServlet(){
super();
System.out.println("FirstServlet_1");
}
protectedvoiddoGet(HttpServletRequestarg0,HttpServletResponsearg1)throwsServletException,IOException{
System.out.println("doGet_5");
}
protectedvoiddoPost(HttpServletRequestarg0,HttpServletResponsearg1)throwsServletException,IOException{
System.out.println("doPost");
}
publicvoidinit(ServletConfigarg0)throwsServletException{
System.out.println("init_2");
}
publicvoiddestroy(){
System.out.println("destroy_6");
}
protectedvoidservice(HttpServletRequestarg0,HttpServletResponsearg1)
throwsServletException,IOException{
System.out.println("serviceServletHttp_4");
super.service(arg0,arg1);
}
publicvoidservice(ServletRequestarg0,ServletResponsearg1)
throwsServletException,IOException{
System.out.println("serviceServlet_3");
super.service(arg0,arg1);
}
publicvoidinit()throwsServletException{
super.init();
}
}
下面是运行,FirstServlet右键运行1在服务器上运行
}
点完成
点在后台运行
右下角的进度条
双击显示进度
控制台显示运行结果
[13-5-160:
55:
04:
266CST]00000038SystemOutOFirstServlet_1
[13-5-160:
55:
04:
266CST]00000038SystemOutOinit_2
[13-5-160:
55:
04:
266CST]00000038ServletWrappeASRVE0242I:
[FirstServlet]:
初始化成功。
[13-5-160:
55:
04:
328CST]00000038SystemOutOserviceServlet_3
[13-5-160:
55:
04:
328CST]00000038SystemOutOserviceServletHttp_4
[13-5-160:
55:
04:
328CST]00000038SystemOutOdoGet_5
浏览器上是没有结果的
2.完成3-10对应代码
1.题目
下面是3-10中的代码
packagecom.ibm.example.servlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.ServletException;
importjava.io.IOException;
importjava.io.PrintWriter;
publicclassVerySimpleServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
Stringbrowser=request.getHeader("User-Agent");
response.setStatus(HttpServletResponse.SC_OK);//default
response.setContentType("text/html");//default
PrintWriterout=response.getWriter();
out.println("
Simpleservlet");out.println("
");
out.println("Browserdetails:
"+browser);
out.println("");
}
}
2.操作
还是新建动态Web项目,名称填ServletExample_2,点完成
点项目名称(动态Web项目ServletExample_2),右键新建其它
在新建向导中,选择Webservlet,点下一步
名称填SecondServlet,点下一步
点下一步
点下一步
下面是HelloWorld的测试代码,可以忽略
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.Servlet;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassSecondServletextendsHttpServletimplementsServlet{
protectedvoiddoGet(HttpServletRequestarg0,HttpServletResponsearg1)throwsServletException,IOException{
PrintWriterout=arg1.getWriter();
out.println("HelloWorld");
}
protectedvoiddoPost(HttpServletRequestarg0,HttpServletResponsearg1)throwsServletException,IOException{
}
}
下面复制题目中的代码覆盖原来的SecondServlet.java
保存,出现9个问题
更改第一个错误,除去包声明”com.ibm.example.servlet”
还有8个
更改第二个错误,将类型重命名为SecondServlet
改完的0错误
按提示改完之后的代码(这里还没有完全改完),每次运行必须重新打开RAD,否则不能生效
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.ServletException;
importjava.io.IOException;
importjava.io.PrintWriter;
publicclassSecondServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringbrowser=request.getHeader("User-Agent");
response.setStatus(HttpServletResponse.SC_OK);//default
response.setContentType("text/html");//default
PrintWriterout=response.getWriter();
out.println("
Simpleservlet");out.println("
");
out.println("Browserdetails:
"+browser);
out.println("");
}
}
运行方法
SecondServlet.java右键运行1在服务器上运行
没保存的会提示保存
直接点完成
运行之后没有结果,因为刚才的代码按提示改完之后任然有错,为什么呢?
因为RAD只能检查Java语法错误,不能检查网页语法错误,经过仔细检查可以发现和