Java Servlet的前100个问题

1)是“ servlets”目录还是“ servlet”目录?

回答:

对于Java Web Server:

2)如何从同一个Servlet支持GET和POST协议?

回答:

简单的方法是,仅支持POST,然后让您的doGet方法调用您的doPost方法:

public void doGet(HttpServletRequest req, HttpServletResponse res)
               throws ServletException, IOException
               {
                  doPost(req, res);
               }

3)如何确保我的servlet是线程安全的?

回答:

这实际上是一个非常复杂的问题。 一些准则:

  1. 确保在加载servlet时每个servlet实例调用一次init()方法。 您不必担心此方法内部的线程安全,因为它仅由单个线程调用,并且Web服务器将等待该线程退出,然后再将更多线程发送到service()方法中。
  2. 每个新的客户端请求都会生成(或分配)一个新线程; 该线程调用servlet的service()方法(依次调用doPost(),doGet()等)。
  3. 在大多数情况下,无论正在处理多少个客户端请求,您的servlet都只有一个实例。 这意味着在任何给定的时刻,您的单独实例的service()方法中可能运行着许多线程,所有线程共享同一实例数据,并且有可能踩到彼此的脚趾。 这意味着您应该小心使用synced关键字同步对共享数据(实例变量)的访问。

(请注意,如果您使用新名称(例如,新的init参数)注册servlet,则服务器还将分配新的实例。)

  1. 请注意,您不必(也不应)在本地数据或参数上同步。 特别是您不应该同步service()方法! (或doPost(),doGet()等。)
  2. 一种简单的同步解决方案是始终使用“已同步(this){…}”在servlet实例本身上进行同步。 但是,这可能会导致性能瓶颈。 通常最好在数据对象本身上进行同步。
  3. 如果您绝对不能处理同步,则可以声明您的servlet“实现SingleThreadModel”。 这个空接口告诉Web服务器一次只向您的Servlet发送一个客户端请求。 从JavaDoc: “如果用该接口标记目标小服务程序,则该小服务程序程序员保证没有两个线程将同时执行该小服务程序的服务方法。 通过为每个此类servlet维护一个servlet实例池,并将每个服务调用分派到一个免费的servlet,来确保这一保证。 本质上,如果该servlet实现该接口,则该servlet将是线程安全的。 请注意,这不是理想的解决方案,因为性能可能会受到影响(取决于实例池的大小),而且跨实例共享数据比在单个实例中共享更加困难。

另请参见什么是启用线程安全的servlet和JSP的更好方法? SingleThreadModel接口还是同步?

  1. 要在连续或并发请求中共享数据,可以使用实例变量或类静态变量,也可以使用会话跟踪。
  2. destroy()方法不一定像init()方法那样干净。 服务器调用销毁或者所有的服务电话都完成后, 几秒钟的一定数目的通过后,以先到者为准。 这意味着其他线程可能在调用destroy()方法的同时正在运行服务请求! 因此,请确保同步和/或等待其他请求退出。 Sun的Servlet教程提供了一个如何使用引用计数进行操作的示例。
  3. destroy()不能引发异常,因此,如果发生严重问题,请使用有用的消息(如异常)调用log()。 请参见“关闭JDBC连接”。 Sun教程中的示例。

4)URL编码,URL重写,HTML转义和实体编码之间有什么区别?

回答:

URL编码
是将用户输入转换为CGI表单的过程,因此适合于跨网络旅行-基本上是删除空格和标点符号,并以转义符代替。 URL解码是相反的过程。 要执行这些操作,请调用java.net.URLEncoder.encode()和java.net.URLDecoder.decode()(后者已(最终!)添加到了JDK 1.2(又名Java 2)中)。

例:

更改“我们是第一!” 变成“我们%27re +%231%21”

URL重写
是一种在两次页面点击之间将状态信息保存在用户浏览器上的技术。 有点像cookie,只是信息被存储在URL中,作为附加参数。 HttpSession API是Servlet API的一部分,有时在cookie不可用时会使用URL重写。

例:

将<A HREF=”nextpage.html”>更改为

<A HREF=”nextpage.html;$sessionid$=DSJFSDKFSLDFEEKOE”>(或实际语法是什么;我忘记了)(不幸的是,Servlet API中用于为会话管理进行URL重写的方法称为encodeURL()。叹…)

Apache Web服务器还有一个名为URL Rewriting的功能。 它由mod_rewrite模块启用。 它会进入服务器的过程重写URL,从而使您可以执行以下操作,例如将斜杠自动添加到目录名,或将旧文件名映射到新文件名。 这与servlet无关。

5)如何将文件上传到我的servlet或JSP?

回答:

在客户端,客户端的浏览器必须支持基于表单的上传。 大多数现代浏览器都可以,但不能保证。 例如,

<FORM ENCTYPE='multipart/form-data'  method='POST' action='/myservlet'>
                  <INPUT TYPE='file' NAME='mptest'>
                  <INPUT TYPE='submit' VALUE='upload'>
               </FORM>

输入类型“文件”可被输入。 会在浏览器上弹出一个用于文件选择框的按钮,并带有一个文本字段,该文本字段将在选定文件后显示文件名。 当请求的POST正文包含要解析的文件数据时,servlet可以使用GET方法参数来决定如何处理上传。

当用户单击“上传”按钮时,客户端浏览器将找到本地文件并使用HTTP POST发送该文件,该文件使用MIME类型的multipart / form-data进行编码。 当它到达您的servlet时,您的servlet必须处理POST数据以便提取编码的文件。 您可以在RFC 1867中了解有关此格式的所有信息。

不幸的是,Servlet API中没有方法可以做到这一点。 幸运的是,有许多可用的库。 其中一些假设您将把文件写入磁盘。 其他人将数据作为InputStream返回。

将表单数据流处理为上载文件后,您可以根据需要将其写入磁盘,将其写入数据库或作为InputStream处理。 请参阅如何从servlet内部访问或在当前目录中创建文件或文件夹? Servlets:Files主题中的其他问题以及有关从Servlet写入文件的信息。

请注意 ,您不能直接从Servlet访问客户端系统上的文件; 那将是一个巨大的安全漏洞。 您必须征求用户的许可,当前基于表单的上传是唯一的方式。

6)servlet如何与JSP页面通信?

回答:

以下代码段显示了Servlet如何实例化bean并使用浏览器发布的FORM数据对其进行初始化。 然后将bean放入请求中,然后通过请求分派器将调用转发到JSP页面Bean1.jsp,以进行下游处理。

public void doPost (HttpServletRequest request,
                                              HttpServletResponse response) {

                  try {
                    govi.FormBean f = new govi.FormBean();
                    String id = request.getParameter("id");
                    f.setName(request.getParameter("name"));
                    f.setAddr(request.getParameter("addr"));
                    f.setAge(request.getParameter("age"));
                              //use the id to compute
                              //additional bean properties like info
                               //maybe perform a db query, etc.
                              // . . .
                              f.setPersonalizationInfo(info);
                    request.setAttribute("fBean",f);
                    getServletConfig().getServletContext().getRequestDispatcher
                                              ("/jsp/Bean1.jsp").forward(request, response);
                  } catch (Exception ex) {
                    . . .
                  }
               }

在首先通过useBean操作从默认请求范围提取fBean之后,JSP页面Bean1.jsp可以处理fBean。

<jsp:useBean id="fBean" class="govi.FormBean" scope="request"/>
               <jsp:getProperty name="fBean" property="name" />
               <jsp:getProperty name="fBean" property="addr" />
               <jsp:getProperty name="fBean" property="age" />
               <jsp:getProperty name="fBean" property="personalizationInfo" />

SingleThreadModel接口还是同步?

回答:

尽管SingleThreadModel技术易于使用,并且在低容量站点上效果很好,但是它的伸缩性不好。 如果您希望用户将来会增加,那么最好对共享数据实施显式同步。 但是,关键是要有效地最小化同步的代码量,以便最大程度地利用多线程。

另外,请注意,从服务器的角度来看,SingleThreadModel占用大量资源。 但是,最严重的问题是并发请求数耗尽了servlet实例池。 在这种情况下,所有未服务的请求都将排队等待,直到有空闲的东西为止–这会导致性能下降。 由于使用情况不确定,因此即使您确实增加了内存并增加了实例池的大小,也可能无济于事。

8)Servlet可以在多个Servlet调用之间维护JTA UserTransaction对象吗?

回答:

不能。JTA事务必须在一次调用(对service()方法)中开始和完成。 请注意,此问题并未解决维护和操作JDBC连接(包括连接的事务处理)的servlet。

与Perl脚本相比如何?

回答:

JSP页面的性能非常接近servlet。 但是,当第一次访问JSP页面时,用户可能会遇到明显的延迟。 这是因为JSP页面经历了“翻译阶段”,在此阶段,JSP页面将其转换为servlet。 一旦该Servlet被动态编译并加载到内存中,它将遵循Servlet生命周期进行请求处理。 在此,JSP引擎在加载servlet时会自动调用jspInit()方法,然后是_jspService()方法,该方法负责处理请求并回复客户端。 请注意,此Servlet的生存期是不确定的–出于资源相关的原因,JSP引擎可随时将其从内存中删除。 发生这种情况时,JSP引擎会自动调用jspDestroy()方法,从而允许Servlet释放任何先前分配的资源。

只要Servlet缓存在内存中,随后对JSP页面的客户端请求就不会重复转换阶段,而是由Servlet的service()方法以并发方式直接处理(即,service()方法处理每个客户请求同时在单独的线程中)。

最近有一些研究将Servlet的性能与在“真实”环境中运行的Perl脚本进行了对比。 结果对Servlet有利,尤其是当它们在集群环境中运行时。

10)如何从另一个servlet调用一个servlet?

回答:

[简短答案:有几种方法可以做到这一点,包括

  • 使用RequestDispatcher
  • 使用URLConnection或HTTPClient
  • 发送重定向
  • 调用getServletContext()。getServlet(name)(不建议使用,在2.1+版本中不起作用)

–亚历克斯]

这取决于您所说的“通话”的含义,您要做什么以及为什么要这么做。

如果最终的结果是调用方法,那么最简单的机制就是将servlet像任何java对象一样对待,创建一个实例并调用该方法。

如果您的想法是从另一个Servlet的服务方法中调用服务方法,即转发请求,则可以使用RequestDispatcher对象。

但是,如果要访问由servlet引擎加载到内存中的servlet实例,则必须知道servlet的别名。 (如何定义它取决于引擎。)例如,要在JSDK中调用servlet,可以通过该属性来命名servlet。

myname.code=com.sameer.servlets.MyServlet

下面的代码显示了如何在另一个servlet的service方法中访问这个命名的servlet。

public void service (HttpServletRequest request, HttpServletResponse response)
                                throws ServletException, IOException {
                  ...
                  MyServlet ms=(MyServlet) getServletConfig().getServletContext().getServlet("myname");
                  ...
               }

就是说,由于安全问题,在Servlet API的2.1版本中已弃用了在另一个servlet中访问servlet的整个方法。 更干净,更好的方法是避免直接访问其他servlet,而使用RequestDispatcher。

(例如Web服务器,应用程序服务器等)

回答:

与处理和处理用户请求有关的服务器分为几种基本类型,每种基本类型都有其要解决的一个或多个任务。 这种灵活性为开发人员提供了在如何创建和部署应用程序方面的强大功能,但也导致对服务器能够或应该执行特定任务的困惑。

从基本级别开始,用户通常是通过Web浏览器向系统提交请求。 (为清楚起见,我们暂时忽略了所有其他类型的客户端(RMI,CORBA,COM / DCOM,自定义等)。)Web请求必须由Web服务器接收(否则称为Web服务器)。 HTTP Server )。 该Web服务器必须处理标准的HTTP请求和响应,通常将HTML返回给调用用户。 在服务器环境中执行的代码可能是CGI驱动,Servlet,ASP或某些其他服务器端编程语言,但是最终结果是Web服务器将HTML返回给用户。

Web服务器可能需要响应用户请求执行应用程序。 它可能正在生成新闻列表,或处理向访客留言簿的表单提交。 如果服务器应用程序是用Java Servlet编写的,则它将需要一个执行位置,该位置通常称为Servlet Engine 。 取决于Web服务器,此引擎可以是内部,外部或完全不同的产品。 该引擎一直在运行,这与传统的CGI环境不同,在传统的CGI环境中,每次向服务器发出请求时都会启动CGI脚本。 这种持久性提供了Servlet连接和线程池,以及维护每个HTTP请求之间状态的简便方法。 JSP页面通常与servlet引擎绑定在一起,并且将在与servlet相同的空间/应用程序中执行。

有许多产品以不同的方式处理Web服务和Servlet引擎。 Netscape / iPlanet Enterprise Server将Servlet引擎直接构建到Web服务器中,并在同一进程空间中运行。 Apache要求Servlet引擎在外部进程中运行,并且将通过TCP / IP套接字与该引擎进行通信。 其他服务器(例如MS IIS)并不正式支持servlet,并且需要附加产品才能添加该功能。

当您继续使用Enterprise JavaBeans(以及其他J2EE组件,例如JMS和CORBA)时,您将进入应用程序服务器空间。 应用程序服务器是任何提供与企业计算相关的附加功能的服务器,例如,负载平衡,数据库访问类,事务处理,消息传递等。

EJB Application Server提供了一个EJB容器,Bean将在该容器中执行该环境,并且该容器将根据需要管理事务,线程池和其他问题。 这些应用程序服务器通常是独立产品,开发人员可以通过远程对象访问API将其servlet / JSP页面绑定到EJB组件。 根据应用服务器的不同,程序员可以使用CORBA或RMI与他们的bean进行通信,但是基线标准是根据需要使用JNDI来定位和创建EJB引用。

现在,使这个问题困惑的一件事是,许多应用程序服务器提供程序在其产品中都包含了部分或全部这些组件。 如果查看WebLogic(http://www.beasys.com/),您会发现WebLogic包含Web服务器,servlet引擎,JSP处理器,JMS工具以及EJB容器。 从理论上讲,此类产品可用于处理站点开发的所有方面。 实际上,您最有可能使用这种类型的产品来管理/服务EJB实例,而专用的Web服务器则处理特定的HTTP请求。

12)我应该重写service()方法吗?

回答:

不。它提供了很多您只需要自己做的内务处理。 如果无论请求是POST还是GET都需要执行某些操作,请创建一个辅助方法,并在doPost()和doGet()的开头调用该方法。

13)我的应用程序如何知道删除HttpSession的时间(超时)?

回答:

定义一个类,例如SessionTimeoutNotifier,该类实现javax.servlet.http.HttpSessionBindingListener。 创建一个SessionTimeoutNotifier对象,并将其添加到用户会话中。 删除会话后,Servlet引擎将调用SessionTimeoutNotifier.valueUnbound()。 您可以实现valueUnbound()来做任何您想做的事情。

14)当我们可以对servlet执行相同的操作时,为什么要使用JSP?

[原始问题:当已经有servlet技术可用于提供动态内容时,为什么我应该使用JSP?]

回答:

尽管JSP可以很好地提供动态Web内容并将内容与表示分离,但是有些人可能仍想知道为什么应将servlet抛给JSP。 servlet的实用程序不成问题。 它们非常适合服务器端处理,并且由于其庞大的安装基础,因此可以保留。 实际上,从架构上来说,您可以将JSP视为Servlet的高级抽象,它是Servlet 2.1 API的扩展而实现的。 不过,您不应该随意使用servlet。 它们可能并不适合所有人。 例如,尽管页面设计人员可以使用常规HTML或XML工具轻松编写JSP页面,但servlet更适合于后端开发人员,因为它们通常使用IDE编写-这种过程通常需要更高水平的编程专业知识。

部署servlet时,甚至开发人员也必须小心,并确保表示和内容之间没有紧密的联系。 通常,您可以通过在混合中添加第三方HTML包装程序包(例如htmlKona)来实现此目的。 但是,即使采用这种方法,尽管可以通过简单的屏幕更改提供一定的灵活性,但仍然不能阻止您更改演示格式本身。 例如,如果您的演示文稿从HTML更改为DHTML,则仍需要确保包装程序包与新格式兼容。 在最坏的情况下,如果没有包装程序包,则最终可能会在动态内容中对演示文稿进行硬编码。 那么,解决方案是什么? 一种方法是同时使用JSP和Servlet技术来构建应用程序系统。

15)如何使用HTTP协议在applet和servlet之间来回发送信息和数据?

回答:

使用标准的java.net.URL类,或使用java.net.Socket“自己动手”。 请参阅HTTP规范
有关详细信息,请访问W3C。

注意: Servlet无法启动此连接! 如果servlet需要异步将消息发送到applet,则必须使用java.net.Socket(在applet端)以及java.net.ServerSocket和Threads(在服务器端)打开持久套接字。

16)我可以获取当前servlet在文件系统上的路径(不是URL)吗?

回答:

尝试使用:

request.getRealPath(request.getServletPath())

一个示例可能是:

out.println(request.getRealPath(request.getServletPath()));

17)如何以菊花链方式将servlet链接在一起,以使一个servlet的输出成为下一个servlet的输入?

回答:

有两种常见的方法将一个servlet的输出链接到另一个servlet:

  1. 第一种方法是别名,它描述了要执行的一系列servlet。
  2. 第二个是定义一个新的MIME-Type并将一个servlet关联为处理程序。由于我并不真正使用第二个,因此我将重点介绍别名。

要将servlet链接在一起,必须指定servlet的顺序列表,并将其与别名关联。 当对该别名发出请求时,将调用列表中的第一个servlet,处理其任务,并将输出作为请求对象发送到列表中的下一个servlet。 输出可以再次发送到另一个servlet。

要实现此方法,您需要配置servlet引擎(JRun,JavaWeb服务器,JServ…)。

例如,要为Servlet链配置JRun,请选择JSE服务(JRun Servlet引擎)以访问“ JSE服务配置”面板。 您只需定义一个新的映射规则,即可在其中定义链接servlet。

假设使用/ servlets / chainServlet表示虚拟路径,并用逗号分隔servlet列表,如srvA,srvB。

因此,当您调用诸如http:// localhost / servlets / chainServlet之类的请求时,将首先在内部调用servlet srvA,并将其结果传递到servlet srvB中。

srvA servlet代码应如下所示:

public class srvA extends HttpServlet {
                  ...
                  public void doGet (...) {
                    PrintWriter out =res.getWriter();
                    rest.setContentType("text/html");
                    ...
                    out.println("Hello Chaining servlet");
                  }
               }

servlet srvB所要做的就是打开请求对象的输入流,并将数据读入BufferedReader对象,例如:

BufferedReader b = new BufferedReader( new InputStreamReader(req.getInputStream() ) );
               String data = b.readLine();
               b.close();

之后,您可以使用数据格式化输出。

它也应该可以与Java Web Server或Jserv一起使用。 只需查看他们的文档即可定义别名。 希望对您有所帮助。

18)为什么servlet中没有构造函数?

回答:

就其具有充当构造函数的init()方法而言,Servlet就像小应用程序一样。 由于servlet环境负责实例化servlet,因此不需要显式的构造函数。 您需要运行的任何初始化代码都应放在init()方法中,因为它是在Servlet容器首次加载Servlet时被调用的。

19)将JDBC与Servlet一起使用时,如何处理多个并发数据库请求/更新?

回答:

每当修改数据时,所有的dbms都提供锁定功能。 可以有两种情况:

  1. 在不同的行上有多个数据库更新,如果您正在使用servlet,则servlet将为不同的用户打开多个连接。 在这种情况下,无需执行其他编程。
  2. 如果数据库更新在同一行上,则这些行将由dbms自动锁定,因此我们必须反复向dbms发送请求,直到dbms释放该锁定为止。

JDBC文档中解决了此问题。 查找“交易”和“自动提交”。 可能会造成混乱。

20)GenericServlet和HttpServlet有什么区别?

回答:

GenericServlet适用于可能不使用HTTP的Servlet,例如FTP Servlet。 当然,事实证明,没有像FTP servlet这样的东西,但是他们在设计规范时正试图为将来的增长做计划。 也许有一天会有另一个子类,但是现在,始终使用HttpServlet。

21)如何在Servlet和JSP之间共享会话对象?

回答:

Servlet和JSP页面之间的共享会话很简单。 JSP通过创建会话对象并使其变得可用来使其变得容易一些。 在servlet中,您必须自己做。 这是这样的:

//create a session if one is not created already now
               HttpSession session = request.getSession(true);
               //assign the session variable to a value.
               session.putValue("variable","value");

在jsp页面中,这是获取会话值的方法:

<%
               session.getValue("varible");
               %>

22)什么是servlet?

回答:

Servlet是使用Java程序扩展Web服务器以执行以前由CGI脚本或专有服务器扩展框架处理的任务的一种方式。

23)是否有什么方法可以在不重新启动服务器的情况下从Web服务器内存中卸载servlet?

回答:

没有标准的方法/机制可以从内存中卸载servlet。 某些服务器(例如JWS)提供了从其管理模块加载和卸载servlet的方法。 其他人(例如Tomcat)要求您仅替换WAR文件。

24)JavaBean和Servlet有什么区别?

回答:

JavaBeans是创建可重用的软件组件或bean遵循的一组规则。 这包含属性和事件。 最后,您有了一个可由程序(例如IDE)检查的组件,以允许JavaBean组件的用户对其进行配置并在其Java程序中运行。

Servlet是在Servlet引擎中运行的Java类,实现了特定的接口:Servlet,强制您实现某些方法(service())。 Servlet是运行该Servlet的Web服务器的扩展,仅当用户请求从网页向Servlet的GET或POST调用时才通知您。

因此,除了Java之外,两者都没有共同点。

25)我们可以在一个会话对象中存储多少数据?

回答:

由于会话保留在服务器端,因此可以在其中存储任何数量的数据。

唯一的限制是sessionId长度,该长度不得超过〜4000字节-HTTP标头长度限制为4Kb暗示了此限制,因为sessionId可能存储在cookie中或以URL编码(使用“ URL rewriting ”)和cookie规范表示Cookie和HTTP请求(例如GET /document.html\n)的大小不能超过4kb。

26)doGet和doPost方法之间有什么区别?

回答:

调用doGet以响应HTTP GET请求。 当用户单击链接或在浏览器的地址栏中输入URL时,就会发生这种情况。 某些HTML FORM(在FORM标记中指定了METHOD =“ GET”HTML FORM)也会发生这种情况。

调用doPost以响应HTTP POST请求。 这在某些HTML FORM(在FORM标记中指定了METHOD =“ POST”HTML FORM)中会发生。

HttpServlet基类中的服务的默认(超类)实现调用这两种方法。 您应该覆盖一个或两个来执行servlet的动作。 您可能不应该重写service()。

27)encodeRedirectUrl和encodeURL有什么区别?

回答:

encodeURL和encodeRedirectURL是HttpResponse对象的方法。 如有必要,两者都重写原始URL以包括会话数据。 (如果启用了cookie,则两个都不操作。)

encodeURL用于HTML页面内的普通链接。

encodeRedirectURL用于传递给response.sendRedirect()的链接。 它对语法的要求略有不同,因此不适合在这里使用。

28)我可以在servlet中使用System.exit()吗?

回答:

加油! 不不不不不…

充其量,您将获得一个安全例外。 最糟糕的是,您将使servlet引擎或整个Web服务器退出。 你真的不想那样吧?

我需要在Connection或Statement对象上进行同步吗?

回答:

不用了 如果您的JDBC驱动程序支持多个连接,那么即使其他请求/线程也正在访问同一连接上的其他语句,各种createStatement方法也会为您提供一个线程安全,可重入,独立的语句,该语句应该可以正常工作。

当然,双手合十不会伤人……许多早期的JDBC驱动程序并没有重入。 现代版本的JDBC驱动程序应该可以正常运行,但是永远不能保证。

使用连接池可以避免整个问题,并且可以提高性能。

30)如何确定正在使用的servlet或JSP引擎的名称和版本号?

回答:

在Servlet内,您可以按以下方式调用ServletContext.getServerInfo()方法:

String thisServer= getServletConfig().getServletContext().getServerInfo();

如果使用的是JSP,则可以使用以下表达式:

<%= application.getServerInfo() %>

31)如何在运行时获取servlet / JSP页面的绝对URL?

回答:

您可以获取所有必要信息,以便从请求对象确定URL。 要从方案,服务器名称,端口,URI和查询字符串重构绝对URL,可以使用java.net中的URL类。 以下代码片段将确定您页面的绝对URL:

String file = request.getRequestURI();
               if (request.getQueryString() != null) {
                  file += '?' + request.getQueryString();
               }
               URL reconstructedURL = new URL(request.getScheme(),
                                                             request.getServerName(),
                                                             request.getServerPort(),
                                                             file);
               out.println(URL.toString());

32)为什么GenericServlet和HttpServlet实现Serializable接口?

回答:

GenericServlet和HttpServlet实现Serializable接口,以便servlet引擎可以在不使用servlet时“休眠” servlet的状态,并在需要时重新设置它的位置,或者复制servlet实例以实现更好的负载平衡。 我不知道当前的servlet引擎是否或如何做到这一点,这可能会产生严重的影响,例如在程序员不知道的情况下破坏对init()方法中获得的对象的引用。 程序员应该意识到这一陷阱,并实现尽可能无状态的Servlet,将数据存储委托给Session对象或ServletContext。 通常,无状态Servlet更好,因为它们的伸缩性更好并且代码更简洁。

33)在覆盖doGet(),doPost()和service()方法之间如何选择?

回答:

doGet()和doPost()方法之间的区别在于,当Servlet从HTTP协议请求中接收到GET或POST请求时,它们会在servlet中通过其service()方法在HttpServlet中调用。

GET请求是从服务器获取资源的请求。 这是浏览器请求网页的情况。 也可以在请求中指定参数,但是总体上参数的长度受到限制。 在html中这样声明的网页中的表单就是这种情况:<form method =” GET”>或<form>。

POST请求是将表单数据发布(发送)到服务器上的资源的请求。 在html中这样声明的网页中的表单就是这种情况:<form method =” POST”>。 在这种情况下,参数的大小可能会更大。

GenericServlet具有一个service()方法,该方法在发出客户端请求时被调用。 这意味着传入请求都将调用它,并且HTTP请求将被照原样提供给Servlet(您必须自己进行解析)。

HttpServlet具有doGet()和doPost()方法,这些方法在客户端请求为GET或POST时被调用。 这意味着请求的解析是由servlet完成的:您调用了适当的方法,并具有方便的方法来读取请求参数。

注意: doGet()和doPost()方法(以及其他HttpServlet方法)由service()方法调用。

最后,如果您必须响应HTTP协议客户端(通常是浏览器)发出的GET或POST请求,请毫不犹豫地扩展HttpServlet并使用其便捷方法。

如果必须响应未使用HTTP协议的客户端发出的请求,则必须使用service()。

每种技术的优缺点是什么?

回答:

Servlet扩展了网站的服务器端功能。 Servlet与该服务器(或任何其他服务器)上的其他应用程序进行通信,并执行“常规”静态HTML文档之外的任务。 Servlet可以接收一个请求,以通过EJB从一个或多个数据库中获取一些信息,然后将该数据转换为静态HTML / WML页面,以供客户端查看。 Even if the servlet talks to many other applications all over the world to get this information, it still looks like it happened at that website.

RMI (Remote Method Invocation) is just that – a way to invoke methods on remote machines. It is way for anapplication to talk to another remote machine and execute different methods, all the while appearing as if the action was being performed on the local machine.

Servlets (or JSP) are mainly used for any web-related activity such as online banking, online grocery stores, stock trading, etc. With servlets, you need only to know the web address and the pages displayed to you take care of calling the different servlets (or actions within a servlet) for you. Using RMI, you must bind the RMI server to an IP and port, and the client who wishes to talk to the remote server must know this IP and port, unless of course you used some kind of in-between lookup utility, which you could do with (of all things) servlets.

35) How can we use a servlet as a proxy for communications between two applets?

Answer:

One way to accomplish this is to have the applets communicate via TCP/IP sockets to the servlet. The servlet would then use a custom protocol to receive and push information between applets. However, this solution does have firewall problems if the system is to be used over and Internet verses an Intranet.

36) How can I design my servlet/JSP so that query results get displayed on several pages, like the results of a search engine? Each page should display, say, 10 records each and when the next link is clicked, I should see the next/previous 10 records and so on.

Answer:

Use a Java Bean to store the entire result of the search that you have found. The servlet will then set a pointer to the first line to be displayed in the page and the number of lines to display, and force a display of the page. The Action in the form would point back to the servlet in the JSP page which would determine whether a next or previous button has been pressed and reset the pointer to previous pointer + number of lines and redisplay the page. The JSP page would have a scriplet to display data from the Java Bean from the start pointer set to the maximum number of lines with buttons to allow previous or next pages to be selected. These buttons would be displayed based on the page number (ie if first then don't display previous button).

37) How do I deal with multi-valued parameters in a servlet?

Answer:

Instead of using getParameter() with the ServletRequest, as you would with single-valued parameters, use the getParameterValues() method. This returns a String array (or null) containing all the values of the parameter requested.

38) How can I pass data retrieved from a database by a servlet to a JSP page?

Answer:

One of the better approaches for passing data retrieved from a servlet to a JSP is to use the Model 2 architecture as shown below:

Basically, you need to first design a bean which can act as a wrapper for storing the resultset returned by the database query within the servlet. Once the bean has been instantiated and initialized by invoking its setter methods by the servlet, it can be placed within the request object and forwarded to a display JSP page as follows:

com.foo.dbBean bean = new com.foo.dbBean();
               //call setters to initialize bean
               req.setAttribute("dbBean", bean);
               url="..."; //relative url for display jsp page
               ServletContext sc = getServletContext();
               RequestDispatcher rd = sc.getRequestDispatcher(url);
               rd.forward(req, res);

The bean can then be accessed within the JSP page via the useBean tag as:

<jsp:useBean id="dbBean" class="com.foo.dbBean" scope="request"/>
               ...
               <%
                  //iterate through the rows within dbBean and
                  //access the values using a scriptlet
               %>

Also, it is best to design your application such that you avoid placing beans into the session unless absolutely necessary. Placing large objects within the session imposes a heavy burden on the performance of the servlet engine. Of course, there may be additional design considerations to take care of – especially if your servlets are running under a clustered or fault-tolerant architecture.

39) How can I use a servlet to generate a site using frames?

Answer:

In general, look at each frame as a unique document capable of sending its own requests and receiving its own responses. You can create a top servlet (say, FrameServlet) that upon invocation creates the frame layout you desire and sets the SRC parameters for the frame tags to be another servlet, a static page or any other legal value for SRC.

---------------------- SAMPLE ----------------------
              
               public void doGet(HttpServletRequest request,
               HttpServletResponse response) throws ServletException, IOException {
               response.setContentType("text/html");
               PrintWriter out = new PrintWriter (response.getWriter());
              
               out.println("");
               out.println("Your Title");
              
               // definingthe three rows of Frames for the main page
               // top : frm_1
               // middle : frm_2
               // bottom : frm_3
              
               out.println("");
               out.println("");
               out.println("");
               out.println("");
               out.println("");
              
               out.println("");
               out.println("");
               out.close();
               -------------------------- END ------------------------------------------

Where MenuServlet and DummyServlet provide content and behavior for the frames generated by FrameServlet.

40) What is HTTP tunneling, in the general sense?

Answer:

HTTP tunneling is a general technique whereby arbitrary data may be sent via an HTTP connection to and from CGI scripts or Java Servlets on a Web server. This is done by serializing the data to be transmitted into a stream of bytes, and sending an HTTP message with content type “application/octet-stream”.

HTTP tunneling is also referred to as Firewall tunneling.

41) How do I handle FORMs with multiple form elements (eg radio buttons) using the same name?

Answer:

For radio buttons, the HTML spec assumes that a given group of buttons will have the same NAME and different VALUEs; the browser makes sure that only one button per group name will be selected (at most). So you can just call request.getParameter(“groupname”).

<input type="radio" name="topping" value="cheese" checked>Cheese
               <input type="radio" name="topping" value="pepperoni">Pepperoni
               <input type="radio" name="topping" value="anchovies">Anchovies

If the user selects “Pepperoni” then request.getParameter(“topping”) will return the string “pepperoni”.

For lists using the <select multiple> FORM tag, multiple values can be returned for the same parameter name. When that can happen, use request.getParameterValues(“param”) which returns a String[] you can iterate through.

It's bad form (so to speak), but you can also duplicate other element types, like

Name 1: <input type="text" name="name" value="Dick">
               Name 2: <input type="text" name="name" value="Jane">

These also get returned in an array by request.getParameterValues().

42) How do I separate presentation (HTML) from business logic (Java) when using servlets?

Answer:

Almost anybody who has ever written a servlet can identify with this one. We all know it's bad for to embed HTML code in our java source; it's lame to have to recompile and re-deploy every time you want an HTML element to look a bit different. But what are our choices here? There are two basic options;

1. Use JSP:
Java Server Pages allows you to embed Java code or the results of a servlet into your HTML. You could, for instance, define a servlet that gives a stock quote, then use the tag in a JSP page to embed the output. But then, this brings up the same problem; without discipline, your content/presentation and program logic are again meshed. I think the ideal here is to completely separate the two.

2. Use a templating/parsing system:
Hmm…I know you're about to rant about re-inventing the wheel, but it's not that bad (see below). Plus, it really does pay to take this approach; you can have a group of programmers working on the Java code, and a group of HTML producers maintaining the interface. So now you probably want to know how to do it…so read on.

Use SSI!

Remember SSI? It hasn't gotten much attention in recent years because of embeddable scripting languages like ASP and JSP, but it still remains a viable option. To leverage it in the servlet world, I believe the best way is to use an API called SSI for Java from Areane. This API will let you emulate SSI commands from a templating system, and much more. It will let you execute any command on any system, including executing java classes! It also comes with several utility classes for creating stateful HTML form elements, tables for use with iteration, and much more. It's also open source, so it's free and you can tweak it to your heart's content! You can read the SSI for Java documentation for detailed info, but the following is an example of its use.

这是servlet:

import javax.servlet.*;
               import javax.servlet.http.*;
               import java.io.*;
               import java.util.*;
               import com.areane.www.ssi.*;
              
               public class SSITemplatingServlet extends HttpServlet {
                  private String templateFilesDirectory = "d:\\projects\\idemo\\templates\\"; //Holds path to template files
              
                  /**Handles GET requests; defers every request to the POST processor*/
                  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException, FileNotFoundException {doPost(req, res);}
                  
                  /**Handles all requests. Processes the request,
                    *saves the values, parses the file, then feeds the file to the out stream*/
                  public void doPost(HttpServletRequest req, HttpServletResponse res)
                    throws ServletException, IOException, FileNotFoundException {
                    HttpSession ses = req.getSession(true);
                                
                    Properties context = null;
                    if((context = (Properties)ses.getValue("user.context")) == null) { //if properties doesn't already exist, create it.
                               context = new Properties();
                    }

                    //Write parameters to Properties object
                    Enumeration paramNames = req.getParameterNames();
                    String curName, curVal;
                    while(paramNames.hasMoreElements()) {
                               curName = (String)paramNames.nextElement();
                               curVal = req.getParameter(curName);
                               context.setProperty(curName, curVal);
                    }
              
                    //Save the values to the session
                    ses.putValue("user.context", context);

                    //Parse the page and stream to the client
                    String templateName = req.getParameter("template"); // Get the file name of the template to use
                    res.setContentType("text/html");
                    SsiPage page = SsiParser.parse(this.templateFilesDirectory + templateName); //Parsing occurs here
                    page.write(res.getWriter(), context); //Stream to the client
                
                    page = null; //clean up
                  }
               }

Now, just create a template file, pass the servlet the template file name, and have at it!

43) For an HTML FORM with multiple SUBMIT buttons, how can a servlet ond

differently for each button?

Answer:

The servlet will respond differently for each button based on the html that you have placed in the HTML page. Let's explain.

For a submit button the HTML looks like <input type=submit name=”Left” value=”left”>. A servlet could extract the value of this submit by using the getParameter(“Left”) from the HttpRequest object. It follows then that if you have HTML within a FORM that appears as:

<input type=submit name="Direction" value="left">

               <input type=submit name="Direction" value="right">

               <input type=submit name="Direction" value="up">

               <input type=submit name="Direction" value="down">

Then the getParameter(“Direction”) from the HttpRequest would extract the value pressed by the user, either “left”, “right”, “up” or “down”. A simple comparision in the servlet with the these values could occur and processing based on the submit button would be performed.

Similiarly,for submit buttons with different names on a page, each of these values could be extracted using the getParameter() call and acted on. However, in a situation where there are multiple buttons, common practice would be to use one name and multiple values to identify the button pressed.

44) What is meant by the term “business logic”?

Answer:

“Business logic” is just a fancy way of saying “code.”

More precisely, in a three-tier architecture, business logic is any code that is not specifically related to storing and retrieving data (that's “data storage code”), or to formatting data for display to the user (that's “presentation logic”). It makes sense, for many reasons, to store this business logic in separate objects; the middle tier comprises these objects. However, the divisions between the three layers are often blurry, and business logic is more of an ideal than a reality in most programs. The main point of the term is, you want somewhere to store the logic and “business rules” (another buzzword) of your application, while keeping the division between tiers clear and clean.

45) How can I explicitly unload a servlet or call the destroy method?

Answer:

In general, you can't. The Servlet API does not specify when a servlet is unloaded or how the destroy method is called. Your servlet engine (ie the implementation of the interfaces in the JSDK) might provide a way to do this, probably through its administration interface/tool (like Webshpere or JWS). Most servlet engines will also destroy and reload your servlet if they see that the class file(s) have been modified.

46) What is a servlet bean?

Answer:

A servlet bean is a serializable servlet that follows the JavaBeans component architecture, basically offering getter/setter methods.

As long as you subclass GenericServlet/HttpServlet, you are automatically Serializable.

If your web server supports them, when you install the servlet in the web server, you can configure it through a property sheet-like interface.

47) Why do we need to call super.init(config) in the init method of a servlet?

Answer:

Just do as you're told and you won't get hurt!

Because if you don't, then the config object will get lost. Just extend HttpServlet, use init() (no parameters) and it'll all work ok.

From the Javadoc: init() – A convenience method which can be overridden so that there's no need to call super.init(config).

48) What is a servlet engine?

Answer:

A “servlet engine” is a program that plugs in to a web server and runs servlets. The term is obsolete; the preferred term now is “servlet container” since that applies both to plug-in engines and to stand-alone web servers that support the Servlet API.

49) Which is the most efficient (ie processing speed) way to create a server application that accesses a database: A Servlet using JDBC; a JSP page using a JavaBean to carry out the db access; or JSP combined with a Servlet? Are these my only choices?

Answer:

Your question really should be broken in two.

1-What is the most efficient way of serving pages from a Java object?. There you have a clear winner in the Servlet. Althought if you are going to change the static content of the page often is going to be a pain because you'll have to change Java code. The second place in speed is for JSP pages. But, depending on your application, the difference in speed between JSP pages and raw servlets can be so small that is not worth the extra work of servlet programming.

2-What is the most efficient way of accessing a database from Java?. If JDBC is the way you want to go the I'd suggest to pick as many drivers as you can (II,III,IV or wathever) and benchmark them. Type I uses a JDBC/ODBC bridge and usually has lousy performance. Again, go for the simplest (usually type IV driver) solution if that meets you performance needs.

For database applications, the performance bottleneck is usually the database, not the web server/engine. In this case, the use of a package that access JDBC with connection pooling at the application level used from JSP pages (with or withouth beans as middleware) is the usual choice. Of course, your applications requirements may vary.

50) How can I change the port of my Java Web Server from 8080 to something else?

Answer:

这很简单。 JAVA WEB SERVER comes with remote Web administration tool. You can access this with a web browser.

Administration tool is located on port 9090 on your web server. To change port address for web server:

  1. Access tool (http://hostname:9090)
  2. Enter User Id/Password (by default it is admin/admin)
  3. Select service (Web service)
  4. Click on “manage” button. You will get a popup screen with all settings.
  5. Click on network tree node, On right hand side you will get text box for entering port no.
  6. Change port number with desire one.
  7. click on restart button.

51) Can I send multiple responses for a single request?

Answer:

No. That doesn't even make sense

You can, however, send a “redirect”, which tells the user's browser to send another request, possibly to the same servlet with different parameters. Search this FAQ on “redirect” to learn more.

52) What is FORM based login and how do I use it? Also, what servlet containers support it?

Answer:

Form based login is one of the four known web based login mechanisms. For completeness I list all of them with a description of their nature:

  1. HTTP Basic Authentication
    • An authentication protocol defined within the HTTP protocol (and based on headers). It indicates the HTTP realm for which access is being negotiated and sends passwords with base64 encoding, therefore it is not very secure. (See RFC2068 for more information.)
  2. HTTP Digest Authentication
    • Like HTTP Basic Authentication, but with the password transmitted in an encrypted form. It is more secure than Basic, but less then HTTPS Authentication which uses private keys. Yet it is not currently in widespread use.
  3. HTTPS Authentication (SSL Mutual Authentication)
    • This security mechanism provides end user authentication using HTTPS (HTTP over SSL). It performs mutual (client & server) certificate based authentication with a set of different cipher suites.
  4. Form Based Login
    • A standard HTML form (static, Servlet/JSP or script generated) for logging in. It can be associated with protection or user domains, and is used to authenticate previously unauthenticated users.
    • The major advantage is that the look and feel of the login screen can be controlled (in comparison to the HTTP browsers' built in mechanisms).

To support 1., 3., and 4. of these authentication mechanisms is a requirement of the J2EE Specification (as of v1.2, 3.4.1.3 Required Login Mechanisms). (HTTP Digest Authentication is not a requirement, but containers are encouraged to support it.)

You can also see section 3.3.11.1 of the J2EE Specs. (User Authentication, Web Client) for more detailed descriptions of the mechanisms.

Thus any Servlet container that conforms to the J2EE Platform specification should support form based login.

To be more specific, the Servlet 2.2 Specification describes/specifies the same mechanisms in 11.5 including form based login in 11.5.3.

This section (11.5.3) describes in depth the nature, the requirements and the naming conventions of form based login and I suggest to take a look at it.

Here is a sample of a conforming HTML login form:

<form method="POST" action="j_security_check">
                  <input type="text" name="j_username">
                  <input type="password" name="j_password">
               </form>

Known Servlet containers that support FORM-based login are:

  • iPlanet Application Server
  • Tomcat (the reference implementation of the Java Servlet API)

53) How do I capture a request and dispatch the exact request (with all the parameters received) to another URL?

Answer:

As far as i know it depends on the location of the next target url.

  • If the next servlet url is in the same host, then you can use the forward method.

Here is an example code about using forward:

RequestDispatcher rd = null;
               String targetURL = "target_servlet_name";
               ServletContext ctx = this.getServletContext();
               rd = ctx.getRequestDispatcher(targetURL);
               rd.forward(request, response);

54) How can the data within an HTML form be refreshed automatically whenever there is a change in the database?

Answer:

JSP is intended for dynamically generating pages. The generated pages can include wml, html, dhtml or whatever you want…

When you have a generated page, JSP has already made its work. From this moment you have a page.

If you want automatic refreshing, then this should be acomplished by the technology included in the generated page (JSP will tell only what to include in the page).

The browser can not be loaded by extern factors. The browser is the one who fetches url's since the http protocol is request-response based. If a server can reload a browser without its allow, it implies that we could be receiving pages which we haven't asked for from servers.

May you could use applets and a ServerSocket for receiving incoming signals from the server for changed data in the DB. This way you can load new information inside the applet or try to force a page reload.

[That's a nice idea — it could use the showDocument() call to reload the current page. It could also use HTTP polling instead of maintaining an expensive socket connection. -Alex]

Perhaps (if possible), could be simpler using an automatic JavaScript refreshing function that force page reload after a specified time interval.

55) What is a web application (or “webapp”)?

Answer:

A web application is a collection of servlets, html pages, classes, and other resources that can be bundled and run on multiple containers from multiple vendors. A web application is rooted at a specific path within a web server. For example, a catalog application could be located at http://www.mycorp.com/catalog. All requests that start with this prefix will be routed to the ServletContext which represents the catalog application.

56) How can I call a servlet from a JSP page? How can I pass variables from the JSP that the servlet can access?

Answer:

You can use <jsp:forward page=”/relativepath/YourServlet” /> or response.sendRedirect(“http://path/YourServlet”).

Variables can be sent as:

<jsp:forward page=/relativepath/YourServlet>
               <jsp:param name="name1" value="value1" />
               <jsp:param name="name2" value="value2" />
               </jsp:forward>

You may also pass parameters to your servlet by specifying response.sendRedirect(“http://path/YourServlet?param1=val1”).

57) Can there be more than one instance of a servlet at one time ?

Answer:

It is important to note that there can be more than one instance of a given Servlet class in the servlet container. For example, this can occur where there was more than one servlet definition that utilized a specific servlet class with different initialization parameters. This can also occur when a servlet implements the SingleThreadModel interface and the container creates a pool of servlet instances to use.

58) How can I measure the file downloading time using servlets?

Answer:

ServletOutputStream out = response.getOutputStream();
               String filename = getServletContext().getRealPath(request.getQueryString());
               FileInputStream fin = new FileInputStream(filename);
               long start = System.currentTimeMillis();
               byte data[] = new byte[1024];
               int len = 0;
               while ((len = fin.read(data)) > 0) {
                  out.write(data, 0, len);
               }
               out.flush();
               long stop = System.currentTimeMills();
               log("took " + (stop - start) + "ms to download " + filename);

59) What is inter-servlet communication?

Answer:

As the name says it, it is communication between servlets. Servlets talking to each other. [There are many ways to communicate between servlets, including

  • Request Dispatching
  • HTTP Redirect
  • Servlet Chaining
  • HTTP request (using sockets or the URLConnection class)
  • Shared session, request, or application objects (beans)
  • Direct method invocation (deprecated)
  • Shared static or instance variables (deprecated)

Search the FAQ, especially topic Message Passing (including Request Dispatching) for information on each of these techniques. -Alex]

Basically interServlet communication is acheived through servlet chaining. Which is a process in which you pass the output of one servlet as the input to other. These servlets should be running in the same server.

eg ServletContext.getRequestDispatcher(HttpRequest, HttpResponse).forward(“NextServlet”) ; You can pass in the current request and response object from the latest form submission to the next servlet/JSP. You can modify these objects and pass them so that the next servlet/JSP can use the results of this servlet.

There are some Servlet engine specific configurations for servlet chaining.

Servlets can also call public functions of other servlets running in the same server. This can be done by obtaining a handle to the desired servlet through the ServletContext Object by passing it the servlet name ( this object can return any servlets running in the server). And then calling the function on the returned Servlet object.

eg TestServlet test= (TestServlet)getServletConfig().getServletContext().getServlet(“OtherServlet”); otherServletDetails= Test.getServletDetails();

You must be careful when you call another servlet's methods. If the servlet that you want to call implements the SingleThreadModel interface, your call could conflict with the servlet's single threaded nature. (The server cannot intervene and make sure your call happens when the servlet is not interacting with another client.) In this case, your servlet should make an HTTP request to the other servlet instead of direct calls.

Servlets could also invoke other servlets programmatically by sending an HTTP request. This could be done by opening a URL connection to the desired Servlet.

60) How do I make servlet aliasing work with Apache+Tomcat?

Answer:

When you use Tomcat standalone as your web server, you can modify the web.xml in $TOMCAT_HOME/webapps/myApp/WEB-INF to add a url-pattern:

<web-app>
                  <servlet>
                    <servlet-name>
                              myServlet
                    </servlet-name>
                    <servlet-class>
                              myServlet
                    </servlet-class>
                  </servlet>
                  <servlet-mapping>
                    <servlet-name>
                              myServlet
                    </servlet-name>
                    <url-pattern>
                              /jsp-bin/*
                    </url-pattern>
                 </servlet-mapping>
               </web-app>

This will let you use: http://webserver:8080/myApp/jsp-bin/stuff.html instead of: http://webserver:8080/myApp/servlet/myServlet/stuff.html But it won't work on port 80 if you've integrated Tomcat with Apache. Graeme Wallace provided this trick to remedy the situation. Add the following to your tomcat-apache.conf (or to a static version of it, since tomcat re-generates the conf file every time it starts):

<LocationMatch /myApp/jsp-bin/* >
                  SetHandler jserv-servlet
               </LocationMatch>

This lets Apache turn over handling of the url pattern to your servlet.

61) Is there any way to determine the number of concurrent connections my servlet engine can handle?

Answer:

Depends on whether or not your servlet container uses thread pooling. If you do not use a thread pool, the number of concurrent connections accepted by Tomcat 3.1, for example, is 10. This you can see for yourself by testing a servlet with the Apache JMeter tool.

However, if your servlet container uses a thread pool, you can specify the number of concurrent connections to be accepted by the container. For Tomcat 3.1, the information on how to do so is supplied with the documentation in the TOMCAT_HOME/doc/uguide directory.

62) What is a request dispatcher and how does it work?

Answer:

A RequestDispatcher object can forward a client's request to a resource or include the resource itself in the response back to the client. A resource can be another servlet, or an HTML file, or a JSP file, etc.

You can also think of a RequestDispatcher object as a wrapper for the resource located at a given path that is supplied as an argument to the getRequestDispatcher method.

For constructing a RequestDispatcher object, you can use either the ServletRequest.getRequestDispatcher() method or the ServletContext.getRequestDispatcher() method. They both do the same thing, but impose slightly different constraints on the argument path. For the former, it looks for the resource in the same webapp to which the invoking servlet belongs and the pathname specified can be relative to invoking servlet. For the latter, the pathname must begin with '/' and is interpreted relative to the root of the webapp.

To illustrate, suppose you want Servlet_A to invoke Servlet_B. If they are both in the same directory, you could accomplish this by incorporating the following code fragment in either the service method or the doGet method of Servlet_A:

RequestDispatcher dispatcher = getRequestDispatcher("Servlet_B");
               dispatcher.forward( request, response );

where request, of type HttpServletRequest, is the first parameter of the enclosing service method (or the doGet method) and response, of type HttpServletResponse, the second. You could accomplish the same by

RequestDispatcher dispatcher=getServletContext().getRequestDispatcher( "/servlet/Servlet_B" );
               dispatcher.forward( request, response );

63) What is a Servlet Context?

Answer:

A Servlet Context is a grouping under which related servlets (and JSPs and other web resources) run. They can share data, URL namespace, and other resources. There can be multiple contexts in a single servlet container .

The ServletContext object is used by an individual servlet to “call back” and obtain services from the container (such as a request dispatcher). Read the JavaDoc for javax.servlet.ServletContext for more information.

You can maintain “application global” variables by using Servlet Context Attributes .

64) Does the RequestDispatcher expect a relative URL to be relative to the originally-called servlet or to the current servlet (if different)?

Answer:

Since the RequestDispatcher will be passing the control (request object and response object) from the current Servlet, the relative URL must be relative to the current servlet.

The originally called servlet has passed the control to the current servlet, and now current servlet is acting as controller to other resourses.

65) What is the difference between in-process and out-of-process servlet containers?

Answer:

The in-process Servlet containers are the containers which work inside the JVM of Web server, these provides good performance but poor in scalibility.

The out-of-process containers are the containers which work in the JVM outside the web server. poor in performance but better in scalibility

In the case of out-of-process containers, web server and container talks with each other by using the some standard mechanism like IPC.

In addition to these types of containers, there is 3rd type which is stand-alone servlet containers. These are an integral part of the web server.

66) How is SingleThreadModel implemented in Tomcat? In other containers? [I would assume that Tomcat uses its connection thread pool, and creates a new instance of the servlet for each connection thread, instead of sharing one instance among all threads. Is that true?]

Answer:

The question mixes together two rather independent aspects of a servlet container: “concurrency control” and “thread pooling”.

Concurrency control, such as achieved by having a servlet implement the SingleThreadModel interface, addresses the issue of thread safety. A servlet will be thread-safe or thread-unsafe regardless of whether the servlet container used a thread pool. Thread pooling merely eliminates the overhead associated with the creation and destruction of threads as a servlet container tries to respond to multiple requests received simultaneously. It is for this reason that the specification document for Servlet 2.2 API is silent on the subject of thread pooling — as it is merely an implementation detail. However, the document does indeed address the issue of thread safety and how and when to use SingleThreadModel servlets.

Section 3.3.3.1 of the Servlet 2.2 API Specification document says that if a servlet implements the SingleThreadModel it is guaranteed “that only one request thread at time will be allowed in the service method.” It says further that “a servlet container may satisfy this guarantee by serializing requests on a servlet or by maintaining a pool of servlet instances.”

Obviously, for superior performance you'd want the servlet container to create multiple instances of a SingleThreadModel type servlet should there be many requests received in quick succession. Whether or not a servlet container does that depends completely on the implementation. My experiments show that Tomcat 3.1 does indeed create multiple instances of a SingleThreadModel servlet, but only for the first batch of requests received concurrently. For subsequent batches of concurrent requests, it seems to use only one of those instances.

67) Which servlet containers have persistent session support? Specifically, does Tomcat 3.1?

Answer:

All servlet containers that implement the Servlet 2.2 API must provide for session tracking through either the use of cookies or through URL rewriting. All Tomcat servlet containers support session tracking.

68) Can I use JAAS as the authentication technology for servlets ?

Answer:

Yes, JAAS can be used as authentication technology for servlets. One important feature of JAAS is pure Java implementation. The JAAS infrastructure is divided into two main components: an authentication component and an authorization component. The JAAS authentication component provides the ability to reliably and securely determine who is currently executing Java code, regardless of whether the code is running as an application, an applet, a bean, or a servlet.

69) How can I set a servlet to load on startup of the container, rather than on the first request?

Answer:

The Servlet 2.2 spec defines a load-on-startup element for just this purpose. Put it in the <servlet> section of your web.xml deployment descriptor. It is either empty (<load-on-startup/>) or contains “a positive integer indicating the order in which the servlet should be loaded. Lower integers are loaded before higher integers. If no value is specified, or if the value specified is not a positive integer, the container is free to load it at any time in the startup sequence.”

例如,

<servlet>
                  <servlet-name>foo</servlet-name>
                  <servlet-class>com.foo.servlets.Foo</servlet-class>
                  <load-on-startup>5</load-on-startup>
               </servlet>

Some servlet containers also have their own techniques for configuring this; please submit feedback with information on these.

70) Is it possible to write a servlet that acts as a FTP server?

Answer:

是。 It would spawn a thread that opens a ServerSocket, then listens for incoming connections and speaks the FTP protocol.

71) Is there a way to disable a user's ability to double-click a submit image/button (and therefore submitting duplicate data — multiple submits)? Is there a way to do this with Javascript?

Answer:

Give the submit image (or button) an onClick() handler. Have the handler check if a flag is set and if not set the flag and submit the form and then clear the form.

72) What are the main differences between Servlets and ISAPI?

Answer:

The first difference is obviously that Servlets is the technology from Sun Microsystems and ISAPI is from Microsoft.

Other Differences are:

  1. Servlet is a simple .class file and ISAPI is a DLL
  2. Servlets run in the Servlet containers and may be in-process or out of process. ISAs run in the same address space as the HTTP server
  3. Servlet container preprocesses and postprocesses the data communication between the client and server. ISAPI Filters provide the capability of pre-processing and post-processing of all data sent between the client and the server
  4. Java is the only choice for writing Servlets, VC++/MFC is used to write ISAPI code
  5. Servlets works on most of the Web servers plus third party containers can be integrated with other web servers to provide servlets on them. ISAPI works on only ISAPI-compliant Web server (for example, Microsoft Internet Information Server)
  6. Servlets can connect to the Databases through JDBC as well as jdbc-odbc bridges. ISAPI can connect to Databases through only ODBC
  7. Servlets have access to many server-side technologies like EJB and etc. ISAPI is limited in scope
  8. Multiple commands can be implemented in a servlet by using pathinfo . ISAPI allows multiple commands in one DLL, implemented as member functions of the CHttpServer object in the DLL.
  9. Content generation and content presentation can be done seperately in Servlets with the help of JSP. ISAPI code has to generate HTML code itself.

73) Can I associate a servlet with a particular mime-type, so if the client requests a file of that type, my servlet will be executed?

Answer:

In web.xml you can use a mime-mapping to map the type with a certain extension and then map the servlet to that extension.

例如

<mime-mapping>
                  <extension>
                    zzz
                  </extension>
                  <mime-type>
                    text/plain
                  </mime-type>
               </mime-mapping>

               <servlet-mapping>
                  <url>
                    *.zzz
                  </url>
                  <servlet-name>
                    MyServlet
                  </servlet-name>
               </servlet-mapping>

So, when a file for type zzz is requested, the servlet gets called.

74) What are the different cases for using sendRedirect() vs. getRequestDispatcher()?

Answer:

When you want to preserve the current request/response objects and transfer them to another resource WITHIN the context, you must use getRequestDispatcher or getNamedDispatcher.

If you want to dispatch to resources OUTSIDE the context, then you must use sendRedirect. In this case you won't be sending the original request/response objects, but you will be sending a header asking to the browser to issue a request to the new URL.

If you don't need to preserve the request/response objects, you can use either.

75) How do I access the value of a cookie using JavaScript?

Answer:

You can manipulate cookies in JavaScript with the document.cookie property. You can set a cookie by assigning this property, and retrieve one by reading its current value.

The following statement, for example, sets a new cookie with a minimum number of attributes:

document.cookie = "cookieName=cookieValue";

And the following statement displays the property's value:

alert(document.cookie);

The value of document.cookie is a string containing a list of all cookies that are associated with a web page. It consists, that is, of name=value pairs for each cookie that matches the current domain, path, and date. The value of the document.cookie property, for instance, might be the following string:

cookieName1=cookieValue1; cookieName2=cookieValue2;

76) How do I write to a log file using JSP under Tomcat? Can I make use of the log() method for this?

Answer:

Yes, you can use the Servlet API's log method in Tomcat from within JSPs or servlets. These messages are stored in the server's log directory in a file called servlet.log.

77) How can I use a servlet to print a file on a printer attached to the client?

Answer:

The security in a browser is designed to restrict you from automating things like this. However, you can use JavaScript in the HTML your servlet returns to print a frame. The browser will still confirm the print job with the user, so you can't completely automate this. Also, you'll be printing whatever the browser is displaying (it will not reliably print plug-ins or applets), so normally you are restricted to HTML and images.

[The JavaScript source code for doing this is:
<input type="button" onClick="window.print(0)" value="Print This Page">

78) How do you do servlet aliasing with Apache and Tomcat?

Answer:

Servlet aliasing is a two part process with Apache and Tomcat. First, you must map the request in Apache to Tomcat with the ApJServMount directive, eg,

ApJServMount/myservlet/ROOT

Second, you must map that url pattern to a servlet name and then to a servlet class in your web.xml configuration file. Here is a sample exerpt:

<servlet>
                  <servlet-name>myservlet</servlet-name>
                  <servlet-class>com.mypackage.MyServlet</servlet-class>
               </servlet>
               <servlet-mapping>
                  <servlet-name>myservlet</servlet-name>
                  <url-pattern>/myservlet</url-pattern>
               </servlet-mapping>

79) I want my servlet page to redirect to a login page if the session has timed out. How can I know if my session has timed out?

Answer:

If the servlet engine does the time-out, following code should help you:

//assume you have a HttpServletRequest request
               if(request.getSession(false)==null) {
                  //no valid session (timeouted=invalid)

                  //code to redirect to login page
               }

80) Can Tomcat be configured to interpret all, or selected, .html files within a given context as JSP? Or, do JSP files have to end with a .jsp extension?

Answer:

yes you can do that by modifying the web.xml file. You will have to invoke the org.apache.jasper.runtime.JspServlet for all the requests having extension .html. You can do that by changing the Servlet mapping code:

<servlet-mapping>
                  <servlet-name>
                    jsp
                  </servlet-name>
                  <url>*.html</url>
               </servlet-mapping>

And comment out the following block

<mime-mapping>
                  <extension>
                    html
                  </extension>
                  <mime-type>
                    text/html
                  </mime-type>
               </mime-mapping>

81) What is the difference between request attributes, session attributes, and ServletContext attributes?

Answer:

A ServletContext attribute is an object bound into a context through ServletContext.setAttribute() method and which is available to ALL servlets (thus JSP) in that context, or to other contexts via the getContext() method. By definition a context attribute exists locally in the VM where they were defined. So, they're unavailable on distributed applications.

Session attributes are bound to a session, as a mean to provide state to a set of related HTTP requests. Session attributes are available ONLY to those servlets which join the session. They're also unavailable to different JVMs in distributed scenarios. Objects can be notified when they're bound/unbound to the session implementing the HttpSessionBindingListener interface.

Request attributes are bound to a specific request object, and they last as far as the request is resolved or while it keep dispatched from servlet to servlet. They're used more as comunication channel between Servlets via the RequestDispatcher Interface (since you can't add Parameters…) and by the container. Request attributes are very useful in web apps when you must provide setup information between information providers and the information presentation layer (a JSP) that is bound to a specific request and need not be available any longer, which usually happens with sessions without a rigorous control strategy.

Thus we can say that context attributes are meant for infra-structure such as shared connection pools, session attributes to contextual information such as user identification, and request attributes are meant to specific request info such as query results.

82) Are singleton/static objects shared between servlet contexts?

[Question continues: For example if I have two contexts on a single web server, and each context uses a login servlet and the login servlet connects to a DB. The DB connection is managed by a singleton object. Do both contexts have their own instance of the DB singleton or does one instance get shared between the two?]

Answer:

It depends on from where the class is loaded.

The classes loaded from context's WEB-INF directory are not shared by other contexts, whereas classes loaded from CLASSPATH are shared. So if you have exactly the same DBConnection class in WEB-INF/classes directory of two different contexts, each context gets its own copy of the singleton (static) object.

83) When building web applications, what are some areas where synchronization problems arrise?

Answer:

In general, you will run into synchronization issues when you try to access any shared resource. By shared resource, I mean anything which might be used by more than one request.

Typical examples include:

  • Connections to external servers, especially if you have any sort of pooling.
  • Anything which you include in a HttpSession. (Your user could open many browser windows and make many simultaneous requests within the one session.)
  • Log destinations, if you do your own logging from your servlets.

84) What is the difference between apache webserver, java webserver and tomcat server?

Answer:

Apache is an HTTP server written in C that can be compiled and run on many platforms.

Java WebServer is an HTTP server from Sun written in Java that also supports Servlets and JSP.

Tomcat is an open-source HTTP server from the Apache Foundation, written in Java, that supports Servlets and JSP. It can also be used as a “plug-in” to native-code HTTP servers, such as Apache Web Server and IIS, to provide support for Servlets (while still serving normal HTTP requests from the primary, native-code web server).

85) How can you embed a JavaScript within servlets / JSP pages?

Answer:

You don't have to do anything special to include JavaScript in servlets or JSP pages. Just have the servlet/JSP page generate the necessary JavaScript code, just like you would include it in a raw HTML page.

The key thing to remember is it won't run in the server. It will run back on the client when the browser loads the generate HTML, with the included JavaScript.

86) How can I make a POST request through response.sendRedirect() or response.setStatus() and response.setHeader() methods?

Answer:

You can't. It's a fundamental limitation of the HTTP protocol. You'll have to figure out some other way to pass the data, such as

  • Use GET instead
  • Make the POST from your servlet, not from the client
  • Store data in cookies instead of passing it via GET/POST

87) How do I pass a request object of one servlet as a request object to another servlet?

Answer:

Use a Request Dispatcher.

88) I call a servlet as the action in a form, from a jsp. How can I redirect the response from the servlet, back to the JSP? (RequestDispatcher.forward will not help in this case, as I do not know which resource has made the request. request.getRequestURI will return the uri as contained in the action tag of the form, which is not what is needed.)

Answer:

You'll have to pass the JSP's URI in to the servlet, and have the servlet call sendRedirect to go back to the JSP. 例如:

<FORM ACTION="/foo/myservlet">
                  <INPUT TYPE="HIDDEN" NAME="redirect" VALUE="/foo/thisjsp.jsp">
                     Shoe size: <INPUT NAME="shoesize">
                  <INPUT TYPE="SUBMIT">
               </FORM>

Then in the servlet…

response.sendRedirect(request.getParameter("redirect"));

89) What is the ServletConfig object, and why is it useful?

Answer:

The ServletConfig object is an interface. It contains the methods

  • getInitParameter
  • getInitParameterNames
  • getServletContext
  • getServletName

You can use the methods to determine the Servlet's initialization parameters, the name of the servlets instance, and a reference to the Servlet Context the servlet is running in.

getServletContext is the most valuable method, as it allows you to share information accross an application (context).

90) I have a global variable in a servlet class. What will happen to this global variable if two requests hit on the same time?

Answer:

What will happen is an unforeseeable event.

The best way to establish a default occurrence (the servlet handles a request at a time) is to synchronize the access to the global variable or alternatively to create a servlet that implements the SingleThreadModel interface.

91) Suppose I have 2 servers, server1 and server2. How can I take data in a cookie from server1, and send it to server2?

Answer:

You'll have to create a (new) similar cookie on server 2.

Have a ReadCookieServlet running on server1 that

  • Reads the cookie, using request.getCookies()
  • Redirects to WriteCookieServlet running on server2, passing the cookie name, value and expiration date as request parameters, using response.sendRedirect() .

Have a WriteCookieServlet running on server2 that

  • Reads the cookie name, value and expiration date request parameters, using request.getParameter() .
  • Creates a similar cookie, using response.addCookie() .

92) How can I pass data from a servlet running in one context (webapp) to a servlet running in another context?

Answer:

There are three ways I can think of off the top of my head:

  1. Store the information you want to share in a persistant format, such as in a file system or database. That way, any servlet that is running in a JVM that can “see” these resources can get to this information.
  2. If persisting this information is not an option, you can bind this information to a context that is accessible to all servlet contexts, such as the application server's context. This way, you can keep the data you want to share in memory.
  3. Use the old fashion way of passing information to a servlet – HTTP. One servlet could foward a request to another servlet and include the data that needs to be shared as parameters in the request.

93) How can I write an “error page” — that is, a servlet or JSP to report errors of other servlets?

Answer:

The Servlet 2.2 specification allows you to specify an error page (a servlet or a JSP) for different kinds of HTTP errors or ServletExceptions. You can specify this in deployment descriptor of the web application as:

<error-page>
                  <exception-type>FooException</exception-type>
                  <location>/error.jsp</location>
                </error-page>

where FooException is a subclass of ServletException.

The web container invokes this servlet in case of errors, and you can access the following information from the request object of error servlet/JSP: error code, exception type, and a message.

94) What is the difference between ServletContext and ServletConfig?

Answer:

A ServletContext represents the context in a servlet container of a servlet instance operates. A servlet container can have several contexts (or web applications) at one time. Each servlet instance is running in one of these contexts. All servlets instances running in the same context are part of the same web application and, therefore, share common resources. A servlet accesses these shared resource (such as a RequestDispatcher and application properties) through the ServletContext object.

This notion of a web application became very significant upon the Servlet 2.1 API, where you could deploy an entire web application in a WAR file. Notice that I always said “servlet instance”, not servlet. That is because the same servlet can be used in several web applications at one time. In fact, this may be common if there is a generic controller servlet that can be configured at run time for a specific application. Then, you would have several instances of the same servlet running, each possibly having different configurations.

This is where the ServletConfig comes in. This object defines how a servlet is to be configured is passed to a servlet in its init method. Most servlet containers provide a way to configure a servlet at run-time (usually through flat file) and set up its initial parameters. The container, in turn, passes these parameters to the servlet via the ServetConfig.

95) Under what circumstances will a servlet be reloaded?

Answer:

That depends on the Servlet container.

Most of the Servlet containers reload the servlet only it detects the code change in the Servlet, not in the referenced classes.

In Tomcat's server.xml deployment descriptor, if you have mentioned

<Context path="/myApp"
                              docBase="D:/myApp/webDev"
                                crossContext="true"
                                debug="0"
                                reloadable="true"
                                trusted="false" >
               </Context>

The reloadable = true makes the magic. Every time the Servlet container detects that the Servlet code is changed, it will call the destroy on the currently loaded Servlet and reload the new code.

But if the class that is referenced by the Servlet changes, then the Servlet will not get loaded. You will have to change the timestamp of the servlet or stop-start the server to have the new class in the container memory.

96) What is a Servlet Filter?

Answer:

A filter is basically a component that is invoked whenever a resource is invoked for which the filter is mapped. The resource can be something like a servlet, or a URL pattern. A filter normally works on the request, response, or header attributes, and does not itself send a response to the client.

97) I am using the RequestDispatcher's forward() method to redirect to a JSP. The problem is that the jsp's url is now relative to the servlet's url and all my url's in the jsp such as <img src=”pic.gif”> will be corrupt. How do I solve this problem?

Answer:

You can use absolute urls like:

<BODY>
                  <% String base = request.getContextPath(); %>
                  <IMG src="<%=base%>/img/pic.gif">
               </BODY>

or write out a BASE tag like:

<% String base = request.getContextPath(); %>
               <HEAD>
                  <BASE HREF="<%=base%>">
               </HEAD>
               <BODY>
                  <IMG src="img/pic.gif">
               </BODY>

That should take care of the problem.

98) How can I return a readily available (static) HTML page to the user instead of generating it in the servlet?

Answer:

To solve your problem, you can either send a “Redirect” back to the client or use a RequestDispatcher and forward your request to another page:

  1. Redirect:

    A redirection is made using the HttpServletResponse object:

    if(condition) {
          response.sendRedirect("page1.html");
       } else {
               response.sendRedirect("page2.html");
       }
  2. RequestDispatcher:

    A request dispatcher can be obtained through the ServletContext. It can be used to include another page or to forward to it.

    if(condition) {
          this.getServletContext()
                       .getRequestDispatcher("page1.html").forward();
       } else {
         this.getServletContext()
                       .getRequestDispatcher("page2.html").forward();
       }

Both solutions require, that the pages are available in you document root. If they are located somewhere else on your filesystem, you have to open the file manually and copy their content to the output writer.

If your application server is set up in combination with a normal web server like Apache, you should use solution (1), because the the web server usually serves static files much faster than the application server.

99) What is the difference between static variables and instance variables in a servlet?

Answer:

According to the Java Language definition, a static variable is shared among all instances of a class, where a non-static variable — also called an instance variable — is specific to a single instance of that class.

According to the Servlet specification, a servlet that does not declare SingleThreadModel usually has one and only one instance, shared among all concurrent requests hitting that servlet.

That means that, in servlets (and other multithreaded applications), an instance variable behaves very much like a static variable, since it is shared among all threads. You have to be very careful about synchronizing access to shared data.

The big difference between instance variables and static variables comes when you have configured your servlet engine to instantiate two instances of the same servlet class, but with different init parameters. In this case, there will be two instances of the same servlet class, which means two sets of instance variables, but only one set of static variables.

Remember that you can store data in lots of different places in a servlet. To wit:

  • Local variables – for loop iterators, result sets, and so forth
  • Request attributes – for data that must be passed to other servlets invoked with the RequestDispatcher
  • Session attributes – persists for all future requests from the current user only
  • Instance variables – for data that persists for the life of the servlet, shared with all concurrent users
  • Static variables – for data that persists for the life of the application, shared with all concurrent users — including any other servlet instances that were instantiated with different init parameters
  • Context attributes – for data that must persist for the life of the application, and be shared with all other servlets

100) How can I share data between two different web applications?

Answer:

Different servlets may share data within one application via ServletContext. If you have a compelling to put the servlets in different applications, you may wanna consider using EJBs.

Reference: Top 100 Java Servlet Questions from our JCG partner Sunil Gulabani at the Sunil Gulabani blog.

翻译自: https://www.javacodegeeks.com/2013/09/top-100-java-servlet-questions.html

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值