设为首页 加入收藏

TOP

JSP/Servlet面试问题及详细答案
2014-10-31 22:15:06 来源: 作者: 【 】 浏览:48
Tags:JSP/Servlet 面试 问题 详细 答案

1.JSP的内置对象及方法


Request表示HttpServletRequest对象。它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie, header, 和session数据的有用的方法。


Response表示HttpServletResponse对象,并提供了几个用于设置送回 浏览器的响应的方法(如cookies,头信息等)


out对象是javax.jsp.JspWriter的一个实例,并提供了几个方法使你能用于向浏览器回送输出结果。


pageContext表示一个javax.servlet.jsp.PageContext对象。它是用于方便存取各种范围的名字空间、servlet相关的对象的API,并且包装了通用的servlet相关功能的方法。


Session表示一个请求的javax.servlet.http.HttpSession对象。Session可以存贮用户的状态信息


applicaton 表示一个javax.servlet.ServletContext对象。这有助于查找有关servlet引擎和servlet环境的信息


config表示一个javax.servlet.ServletConfig对象。该对象用于存取servlet实例的初始化参数。


Page表示从该页面产生的一个servlet实例


2.JSP的常用指令


<%@ page
language =”scriptingLanguage”: 主要指定JSP Container要用什么语言来编译JSP 网页。JSP 2.0 规范中指出,目前只可以使用Java语言,不过未来不排除增加其他语言,如C、C++、Perl 等等。默认值为Java
extends = “className”: 主要定义此JSP 网页产生的Servlet 是继承哪个父类
import = “importList”: 主要定义此JSP 网页可以使用哪些Java API
session = “true | false”: 决定此JSP 网页是否可以使用session 对象。默认值为true
buffer = “none | size in kb”: 决定输出流 (output stream)是否有缓冲区。默认值为8KB 的缓冲区
autoFlush = “true | false”: 决定输出流的缓冲区是否要自动清除,缓冲区满了会产生异常 (Exception)。默认值为 true
isThreadSafe = “true | false”: 主要是告诉JSP Container,此JSP 网页能处理超过一个以上的请求。默认值为 true,如果此值设为 false,SingleThreadModel 将会被使用。SingleThreadModel 在Servlet 2.4 中已经声明不赞成使用(deprecate)
info = “text”: 主要表示此JSP 网页的相关信息
errorPage = “error_url”: 表示如果发生异常错误时,网页会被重新指向那一个URL
isErrorPage = “true | false”: 表示此JSP Page 是否为处理异常错误的网页
contentType = “ctinfo”: 表示MIME 类型和JSP 网页的编码方式
pageEncoding = “ctinfo”: 表示JSP 网页的编码方式
isELIgnored = “true | false”: 表示是否在此JSP 网页中执行或忽略EL 表达式。如果为true 时,JSP Container将忽略EL表达式;反之为false时,EL 表达式将会被执行


注意:只有import 这个属性可以重复设定,其他则否。


%>


<%@include file=”filename”%>


<%@taglib prefix=”c”uri=” http://……”%>


3.Request对象的主要方法:


setAttribute(String name,Object):设置名字为name的request的参数值


getAttribute(String name):返回由name指定的属性值


getAttributeNames():返回request对象所有属性的名字集合,结果是一个枚举的实例


getCookies():返回客户端的所有Cookie对象,结果是一个Cookie数组


getCharacterEncoding():返回请求中的字符编码方式


getContentLength():返回请求的Body的长度


getHeader(String name):获得HTTP协议定义的文件头信息


getHeaders(String name):返回指定名字的request Header的所有值,结果是一个枚举的实例


getHeaderNames():返回所以request Header的名字,结果是一个枚举的实例


getInputStream():返回请求的输入流,用于获得请求中的数据


getMethod():获得客户端向服务器端传送数据的方法


getParameter(String name):获得客户端传送给服务器端的有name指定的参数值


getParameterNames():获得客户端传送给服务器端的所有参数的名字,结果是一个枚举的实例


getParameterValues(String name):获得有name指定的参数的所有值


getProtocol():获取客户端向服务器端传送数据所依据的协议名称


getQueryString():获得查询字符串


getRequestURI():获取发出请求字符串的客户端地址


getRemoteAddr():获取客户端的IP地址


getRemoteHost():获取客户端的名字


getSession([Boolean create]):返回和请求相关Session


getServerName():获取服务器的名字


getServletPath():获取客户端所请求的脚本文件的路径


getServerPort():获取服务器的端口号


removeAttribute(String name):删除请求中的一个属性


4.jsp有哪些动作 作用分别是什么


JSP共有以下6种基本动作


jsp:include:在页面被请求的时候引入一个文件。


Jsp:useBean:寻找或者实例化一个JavaBean。


Jsp:setProperty:设置JavaBean的属性。


Jsp:getProperty:输出某个JavaBean的属性。


Jsp:forward:把请求转到一个新的页面。


Jsp:plugin:使您能插入所需的特定的浏览器的OBJECT或EMBED元素来指定浏览器运行一个JAVA Applet所需的插件。根据浏览器类型为Java插件生成OBJECT或EMBED标记。


<jsp:plugin type=”bean|applet” code=”classFileName” codebase=”classFileDirectoryName”
[ name="instanceName" ]
[ archive="URIToArchive, ..." ]
[ align="bottom|top|middle|left|right" ]
[ height="displayPixels" ]
[ width="displayPixels" ]
[ hspace="leftRightPixels" ]
[ vspace="topBottomPixels" ]
[ jreversion="JREVersionNumber | 1.1" ]
[ nspluginurl="URLToPlugin" ]
[ iepluginurl="URLToPlugin" ] >
[ <jsp:params>
[ <jsp:param name="parameterName"
value="{parameterValue | <%= expression %>}" /> ]+
</jsp:params> ]
[ <jsp:fallback> text message for user </jsp:fallback> ]
</jsp:plugin>


举例:


<jsp:plugin type=applet code=”Molecule.class” codebase=”/html”>
<jsp:params>
<jsp:param name=”molecule” value=”molecules/benzene.mol” />
</jsp:params>
<jsp:fallback>
<p>Unable to load applet</p>
</jsp:fallback>
</jsp:plugin>


5.
过滤器 监听 servlet 三者的区别和实际中主要的应用场景


过滤器(Filter)


作为过滤器,在Servlet作为过滤器使用时,它可以对客户的请求进行过滤处理,当它处理完成后,它会交给下一个过滤器处理,就这样,客户的请求在过滤链里一个个处理,直到请求发送到目标。举个例子,某个网站里有提交”修改的注册信息”的网页,当用户填写完成修改信息并提交后,服务端在进行真正的处理时需要做两个处理:客户端的会话是否有效;对提交的数据进行统一的编码,比如GB2312。这两个处理可以在由两个过滤器组成的过滤链里进行处理。当过滤器处理成功后,把提交的数据发送到最终目标;如果过滤器处理不成功(比如客户端的会话无效),它将把视图派发到指定的错误页面。可以看出,过滤器就像一扇门,客户端要和服务端的某个目标交互,必须通过这扇门。


过滤器是用于过滤Servlet的请求和响应,过滤器是存在于请求和被请求资源之间的。
过滤器就像当于一个中间件,请求要经过过滤器,然后过滤器才去调用Servlet,Servlet的响应也会被过滤器截获并作相应的处理。


Filter是一个接口,要写一个自己的Filter就只能实现Filter接口。
Filter也有自己的生命周期,他的生命周期和Servlet比较相似,也是会先调用init()方法,然后再调用核心的处理过滤的方法doFilter(),这个方法中可定义过滤规则,然后是destory()方法销毁Filter对象。


dofilter(ServletRequest request,ServletResponse response,FilterChain chain)
这个是过滤的核心方法,FilterChain的方法doFilter(ServletRequest request, ServletResponse response)也就是过滤后的请求调用资源的方法,如果不写这个方法,也就不会去调用相应的资源。

Filter的配置


Filter的配置和Servlet相似。

SessionFilter
alan.filter.SessionFilter


SessionFilter
/protected/*

request
forward
include


CharArrayWriter类,是一个将数据保存在字符数组中的输出流,我们可以使用它来构造一个PrintWriter对象,也就实现了向内存输出。CharArrayWriter类的toString()和toCharArray()方法就可以取得写入内存中的数据。
注意:CharArrayWriter类是一个不会真正输出的类,他的write()方法只会将内容写入字符数组,而且这个字符数组是会自动增长的。


例子:两个JSP页面,前一个页面用户输入一些信息然后提交,后一个页面显示用户提交的信息。在提交信息后,要经过两个过滤器的处理,一个检查用户是否登录,一个把用户的提交信息用GB2312进行重新编码。


开发一个Filter,这个Filter需要实现Filter接口,Filter接口定义了以下的方法:


destroy() //由Web容器调用,销毁此Filter


init(FilterConfig filterConfig) ///由Web容器调用,初始化此Filter


doFilter(ServletRequest request, ServletResponse response,


FilterChain chain)//具体过滤处理代码


对提交信息用GB2312进行重新编码的Filter:


1 // ==================== Program Discription =====================


2 // 程序名称:示例14-7 : EncodingFilter .java


3 // 程序目的:学习使用编码过滤器


4 // ==========================================================


5 import javax.servlet.FilterChain;


6 import javax.servlet.ServletRequest;


7 import javax.servlet.ServletResponse;


8 import java.io.IOException;


9 import javax.servlet.Filter;


10 import javax.servlet.http.HttpServletRequest;


11 import javax.servlet.http.HttpServletResponse;


12 import javax.servlet.ServletException;


13 import javax.servlet.FilterConfig;


15 public class EncodingFilter implements Filter


16 {


18 private String targetEncoding = “gb2312″;


19 protected FilterConfig filterConfig;


21 public void init(FilterConfig config) throws ServletException {


22 this.filterConfig = config;


23 this.targetEncoding = config.getInitParameter(“encoding”);


24 }


27 public void doFilter(ServletRequest srequest, ServletResponse sresponse,FilterChain chain)


28 throws IOException, ServletException {


30 HttpServletRequest request = (HttpServletRequest)srequest;


31 request.setCharacterEncoding(targetEncoding);//把请求用指定的方式编码


32 // 把处理发送到下一个过滤器


33 chain.doFilter(srequest,sresponse);


34 }


36 public void destroy()


37 {


38 this.filterConfig=null;


39 }


41 public void setFilterConfig(final FilterConfig filterConfig)


42 {


43 this.filterConfig=filterConfig;


44 }


45 }


【程序注解】
正如前面所说,EncodingFilter的目的是把客户端的请求用指定的方式编码,具体的处理在request.setCharacterEncoding(targetEncoding)完成了。LoginFilter判断用户在进入目标之前是否登录,if((isLog!=null)&&((isLog.equals(“true”))||(isLog==”true”)))将检查用户是否登录,如果已登录,那么把视图让过滤链继续处理,如果没有登录,把视图派发到登录页面,过滤链处理结束。
下面我们来看怎么在web.xml里配置这两个过滤器,代码如下所示:


1 // ==================== Program Discription =====================


2 // 程序名称:示例14-7 : EncodingFilter .java


3 // 程序目的:学习使用编码过滤器


4 // ==========================================================


5 import javax.servlet.FilterChain;


6 import javax.servlet.ServletRequest;


7 import javax.servlet.ServletResponse;


8 import java.io.IOException;


9 import javax.servlet.Filter;


10 import javax.servlet.http.HttpServletRequest;


11 import javax.servlet.http.HttpServletResponse;


12 import javax.servlet.ServletException;


13 import javax.servlet.FilterConfig;14


15 public class EncodingFilter implements Filter


16 {


18 private String targetEncoding = “gb2312″;


19 protected FilterConfig filterConfig;


21 public void init(FilterConfig config) throws ServletException {


22 this.filterConfig = config;


23 this.targetEncoding = config.getInitParameter(“encoding”);


24 }


27 public void doFilter(ServletRequest srequest, ServletResponse sresponse,FilterChain chain)


28 throws IOException, ServletException {


30 HttpServletRequest request = (HttpServletRequest)srequest;


31 request.setCharacterEncoding(targetEncoding);//把请求用指定的方式编码


32 // 把处理发送到下一个过滤器


33 chain.doFilter(srequest,sresponse);


34 }


36 public void destroy()


37 {


38 this.filterConfig=null;


39 }


40


41 public void setFilterConfig(final FilterConfig filterConfig)


42 {


43 this.filterConfig=filterConfig;


44 }


45 }


【程序注解】
可以看出,配置Filter时,首先指定Filter的名字和Filter的实现类,如果有必要,还要配置Filter的初始参数;然后为Filter做映射,这个映射指定了需要过滤的目标(JSP、Servlet)。在上面的例子中,指定了EncodingFilter 为所有的JSP和Servlet做过滤,LoginFilter为target.jsp做过滤。这样,当客户请求target.jsp时,首先要经过EncodingFilter的处理,然后经过LoginFilter的处理,最后才把请求传递给target.jsp。


【运行程序】
把程序部署到Web服务器里(比如Tomcat),然后启动Web服务器,在浏览器里输入以下URL(根据具体请求改变URL):
http://127.0.0.1:8080/ch14/target.jsp


那么Filter将会把视图派发到:http://127.0.0.1:8080/ch14/init.jsp


在init.jsp里,我们使用:


<% session.setAttribute(“isLog”,”true”);%>


来设置用户已经登录(这里是简化的,在实际项目中,可能要经过验证处理)。在init.jsp里,可以提交一些中文的信息。由于提交的信息被EncodingFilter使用GB2312统一编码了,故在target.jsp里能够正确显示中文。您可以做一个试验,把web.xml改为:




encoding


/nothing




然后重新启动Web服务器。那么在target.jsp里,中文将不能正确显示。


监听器


Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。


以下是3个Listener接口。


ServletContextAttributeListener:监听对ServletContext属性的操作,比如增加、删除、修改属性


ServletRequestListener:


HttpSessionListener:监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法


HttpSessionAttributeListener:监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法


ServletContextListener:监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法


这三个监听器接口,分别监听Servlet中3种比较重要的对象的创建和销毁。这三个接口中分别有监听该对象创建和销毁事件的方法,服务器本身就是事件源。


listener的配置

alan.servlet.listener.AlanContextListener



Servlet中的重要对象(只针对应用)



下面我们开发一个具体的例子,这个监听器能够统计在线的人数。在ServletContext初始化和销毁时,在服务器控制台打印对应的信息。当ServletContext里的属性增加、改变、删除时,在服务器控制台打印对应的信息。


要获得以上的功能,监听器必须实现以下3个接口:


HttpSessionListener


ServletContextListener


ServletContextAttributeListener


// ==================== Program Discription =====================


2 // 程序名称:示例14-9 : EncodingFilter .java


3 // 程序目的:学习使用监听器


4 // ==============================================================


5 import javax.servlet.http.*;


6 import javax.servlet.*;


8 public class OnLineCountListener implements HttpSessionListener,


ServletContextListener,ServletContextAttributeListener


9 {


10 private int count;


11 private ServletContext context = null;


13 public OnLineCountListener()


14 {


15 count=0;


16 //setContext();


17 }


18 //创建一个session时激发


19 public void sessionCreated(HttpSessionEvent se)


20 {


21 count++;


22 setContext(se);


24 }


25 //当一个session失效时激发


26 public void sessionDestroyed(HttpSessionEvent se)


27 {


28 count–;


29 setContext(se);


30 }


31 //设置context的属性,它将激发attributeReplaced或attributeAdded方法


32 public void setContext(HttpSessionEvent se)


33 {


34 se.getSession().getServletContext().setAttribute(“onLine”,new Integer(count));


35 }


36 //增加一个新的属性时激发


37 public void attributeAdded(ServletContextAttributeEvent event) {


39 log(“attributeAdded(‘” + event.getName() + “‘, ‘” +


40 event.getValue() + “‘)”);


42 }


44 //删除一个新的属性时激发


45 public void attributeRemoved(ServletContextAttributeEvent event) {


47 log(“attributeRemoved(‘” + event.getName() + “‘, ‘” +


48 event.getValue() + “‘)”);


50 }


52 //属性被替代时激发


53 public void attributeReplaced(ServletContextAttributeEvent event) {


55 log(“attributeReplaced(‘” + event.getName() + “‘, ‘” +


56 event.getValue() + “‘)”);


57 }


58 //context删除时激发


59 public void contextDestroyed(ServletContextEvent event) {


61 log(“contextDestroyed()”);


62 this.context = null;


64 }


66 //context初始化时激发


67 public void contextInitialized(ServletContextEvent event) {


69 this.context = event.getServletContext();


70 log(“contextInitialized()”);


72 }


73 private void log(String message) {


75 System.out.println(“ContextListener: ” + message);


76 }


77 }


【程序注解】
在OnLineCountListener里,用count代表当前在线的人数,OnLineCountListener将在Web服务器启动时自动执行。当OnLineCountListener构造好后,把count设置为0。每增加一个Session,OnLineCountListener会自动调用sessionCreated(HttpSessionEvent se)方法;每销毁一个Session,OnLineCountListener会自动调用sessionDestroyed(HttpSessionEvent se)方法。当调用sessionCreated(HttpSessionEvent se)方法时,说明又有一个客户在请求,此时使在线的人数(count)加1,并且把count写到ServletContext中。ServletContext的信息是所有客户端共享的,这样,每个客户端都可以读取到当前在线的人数。


为了使监听器生效,需要在web.xml里进行配置,如下所示:




OnLineCountListener




测试程序:


<%@ page contentType=”text/html;charset=gb2312″ %>


目前在线人数:


<%=getServletContext().getAttribute(“onLine”)%>


getServletContext().getAttribute(“onLine”)获得了count的具体值。客户端调用


退出会话:








客户端调用:


<%session.invalidate() ;%>


使Session失效,这样监听器就会使count减1。


web.xml做好以上的配置,把OnLineCountListener放在WEB-INF/class目录下,启动Web服务器,在浏览器里输入以下URL(根据具体情况不同):http://127.0.0.1:8080/ch14/listener.jsp


浏览器将会打印目前在线人数。在服务器端有以下输出:




ContextListener: contextInitialized()


ContextListener: attributeReplaced(‘org.apache.


catalina.WELCOME_FILES’, ‘[Ljava.lang.String;@1d98a’)




ContextListener: attributeAdded(‘onLine’, ’1′)


ContextListener: attributeReplaced(‘onLine’, ’1′)


ContextListener: attributeReplaced(‘onLine’, ’0′)


ContextListener: attributeReplaced(‘onLine’, ’1′)


ContextListener: attributeReplaced(‘onLine’, ’2′)



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇fqa部分笔试题目 下一篇正保科技Java面试题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: