简体   繁体   English

有关Servlet的问题

[英]Questions on Servlets

1st question: Which scope object to be used if: 第一个问题:在以下情况下使用哪个范围对象:

  1. You wish to maintain the user profile that the customer needs to enter in multiple pages. 您希望维护客户需要在多个页面中输入的用户配置文件。
  2. You wish to validate the user id and password entered by user suing another servlet but in the same web application. 您希望验证用户使用另一个servlet但在同一Web应用程序中输入的用户ID和密码。

2nd question: The RequestDispatcher object has two methods, include() and forward() . 第二个问题: RequestDispatcher对象有两个方法, include()forward() What is the difference? 有什么区别?

3rd question: Servlet uses a javax.servlet.http.HttpServletResponse object. 第三个问题: Servlet使用javax.servlet.http.HttpServletResponse对象。 How do you use it to return Text data and Binary data? 你如何使用它来返回文本数据和二进制数据?

(a) Session Scoped (a)会议范围

RequestDispather.forward() Once you forward the request from say Servlet A to any other Servlet/JSP control gets transferred from Servlet A to forwarded patrty & it never returns back to A for that request. RequestDispather.forward()一旦你将来自Servlet A的请求转发给任何其他Servlet / JSP控件,从Servlet A转移到转发的patrty,它永远不会返回给A请求。

RequestDispather.Include() In include what you are doing is if Servlet A(Above example) is including the response of other Servlet/JSP(say B or B.jsp) so momentarily Control goes to B or B.jsp (they will genrate the response) control comes back to A & generated response is added in A's Response. RequestDispather.Include()包含你正在做的是如果Servlet A(上面的例子)包含其他Servlet / JSP(比如B或B.jsp)的响应那么暂时控制转到B或B.jsp(他们会生成响应)控制返回到A,生成的响应被添加到A的响应中。

check HttpServletResponse API for response writing 检查HttpServletResponse API以获取响应

  1. Answer: 回答:

    Forward() method is used to forward the request from Servlet to Servlet/JSP/HTML or JSP to Servlet/JSP/HTML Forward()方法用于将请求从Servlet转发到Servlet / JSP / HTML或JSP转发到Servlet / JSP / HTML

    include() method is used to include the response of Servlet or JSP in another Servlet or JSP include()方法用于在另一个Servlet或JSP中包含Servlet或JSP的响应

    1. Answer: 回答:

    You can create a separate Servlet class to validate User ID and Password for another Servlet class,it can be done by Servlet Chaining concept. 您可以创建一个单独的Servlet类来验证另一个Servlet类的用户ID和密码,它可以通过Servlet Chaining概念来完成。 In the validation Server class,call request dispatcher to forward the response to Main Servlet class. 在验证Server类中,调用请求调度程序将响应转发给Main Servlet类。

eg. 例如。

RequestDispatcher rd=req.getRequestDispatcher("url/servlet name"); rd.forward(req,res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM