servlet实验文档格式.docx
《servlet实验文档格式.docx》由会员分享,可在线阅读,更多相关《servlet实验文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
果
及
分
析
一、
1、请建立一个servlet类用于显示当前的系统时间。
写出它的url配置写法
packageservlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Date;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.Http
ServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassservletDateextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("
UTF-8"
);
response.setCharacterEncoding("
response.setContentType("
text/html"
PrintWriterout=response.getWriter();
StringdateString="
null"
;
Datedate=newDate();
dateString=date.toLocaleString();
out.print("
当前时间是:
"
+dateString);
out.flush();
out.close();
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
}
url配置方法:
首先在servlet包下新建servlet类:
servletDate
<
servlet>
<
servlet-name>
servletDate<
/servlet-name>
<
servlet-class>
servlet.servletDate<
/servlet-class>
/servlet>
然后进行映射,第一个斜杠是web应用的根路径;
servlet-mapping>
url-pattern>
/servlet/servletDate<
/url-pattern>
/servlet-mapping>
二、
1、请输入用户名和密码,如果用户名和密码是“zhangsan”和”123456”,则进入一个欢迎界面,否则要求用户重新输入。
涉及的方法:
(1)response.sendRedirect("
login.htm"
(2)request.getRequestDispatcher("
welcome.htm"
).forward(request,response);
(3)HttpSessionsession=request.getSession();
session.setAttribute("
islogin"
true);
(4)booleanisLogin=session.getAttribute("
Login.html:
!
DOCTYPEHTMLPUBLIC"
-//W3C//DTDHTML4.01Transitional//EN"
>
html>
head>
title>
MyHtml.html<
/title>
metahttp-equiv="
keywords"
content="
keyword1,keyword2,keyword3"
description"
thisismypage"
content-type"
text/html;
charset=UTF-8"
--<
linkrel="
stylesheet"
type="
text/css"
href="
./styles.css"
-->
/head>
body>
ThisismyHTMLpage.<
br>
formmethod="
post"
action="
/servlet/servlet/login1"
>
用户名
inputtype="
text"
name="
name"
&
nbsp;
密码
pwd"
submit"
value="
进入"
/form>
/body>
/html>
Welcome.java
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
publicclassWelcomeextendsHttpServlet{
this.doPost(request,response);
}
HttpSessionsession=request.getSession();
StringisLogin=(String)session.getAttribute("
isLogin"
response.setContentType("
PrintWriterout=response.getWriter();
out.println("
DOCTYPEHTMLPUBLIC\"
-//W3C//DTDHTML4.01Transitional//EN\"
HTML>
HEAD>
TITLE>
AServlet<
/TITLE>
/HEAD>
BODYbgcolor='
X0FFF'
style='
font-size:
100px'
out.println("
marquee>
欢迎进入JavaWeb世界!
/front>
/marquee>
isLogin:
+isLogin);
/BODY>
/HTML>
out.flush();
out.close();
Login.java
publicclasslogin1extendsHttpServlet{
this.doPost(request,response);
request.setCharacterEncoding("
//字符编码问题,解决中文乱码问题
StringName=request.getParameter("
StringPwd=request.getParameter("
if("
zhangsan"
.equals(Name)&
&
123456"
.equals(Pwd)){
HttpSessionsession=request.getSession();
session.setAttribute("
"
true"
request.getRequestDispatcher("
/welcome"
//转发
}else{
response.sendRedirect(request.getContextPath()+"
/login1.html"
//重定向
2、领会多用户并发访问的机制,请建立一个HttpServlet——“servletUsers”类,在该类的成员代码如下:
publicclassserveltUsersextendsHttpServlet{
privateIntegerx=1;
response.setCharacterEncoding("
utf-8"
BODY>
当前x的值是"
+x);
synchronized(x){
Threadthread=Thread.currentThread();
try{
thread.sleep(1000*5);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}x++;
当前serlvet实例阻塞5秒后,当前x的值是"
out.flush();
请在5秒内用两个浏览器窗口浏览该HttpServlet类,看看效果如何。
为什么是这个效果!
第一个窗口显示:
当前x的值是1当前serlvet实例阻塞5秒后,当前x的值是2
第二窗口显示:
当前x的值是2当前serlvet实例阻塞5秒后,当前x的值是3
分析与解析:
用户一访问x时,第一次x为初值1,用户一在休眠五秒钟内释放了对x的控制权,休眠后,当前x的值是3
用户二在五秒内再次访问x时,因为对核心语句加锁,必须等待用户一休眠后以及对x加1后才能访问x,这时用户二访问的x为已经加了1,值为2,当前serlvet实例阻塞5秒后,当前x的值是3
三、
1、请设计一种过滤器实现权限控制机制。
如果用户进入web应用没有登入时,要求用户必须进入登入页面。
packagefilter;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
publicclassencoderFilterimplementsFilter{//过滤器的使用
publicvoiddestroy(){//销毁,在销毁之前可以保存数据到数据库
publicvoiddoFilter(ServletRequestreq,ServletResponseres,
FilterChainchain)throwsIOException,ServletException{
HttpServletRequestrequest=(HttpServletRequest)req;
HttpServletResponseresponse=(HttpServletResponse)res;
//字码设置,解决乱码问题
if(session.isNew()){
response.sendRedirect(request.getContextPath()+"
/login.html"
}
chain.doFilter(request,response);
//过滤器的方法
publicvoidinit(FilterConfigarg0)throwsServletException{//初始化方法
配置WEB.xml
filter>
filter-name>
encoder<
/filter-name>
filter-class>
filter.encoderFilter<
/filter-class>
/filter>
filter-mapping>
/*<
过滤web应用下的文件
/filter-mapping>
2、请设计一种机制进行网站访问者统计
监听器类
packageListener;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletContextEvent;
importjavax.servlet.ServletContextListener;
importjavax.servlet.http.HttpSessionEvent;
importjavax.servlet.http.HttpSessionListener;
publicclassapplicationimplementsServletContextListener,HttpSessionListener{
publicvoidcontextDestroyed(ServletContextEventsce){
//TODOAuto-generatedmethodstub
publicvoidcontextInitialized(ServletContextEventsce){
ServletContextapplication=sce.getServletContext();
application.setAttribute("
count"
0);
System.out.print("
web应用初始化**********"
publicvoidsessionCreated(HttpSessionEventse){
一个新的session对象产生********"
ServletContextapplication=se.getSession().getServletContext();
HttpSessionsession=se.getSession();
session.setAttribute("
10);
Integercount=(Integer)application.getAttribute("
intcou=count+1;
application.setAttribute("
cou);
publicvoidsessionDestroyed(HttpSessionEventse){
统计类:
publicclasshello3extendsHttpServlet{
/**
*ThedoGetmethodoftheservlet.<
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServle