为了账号安全,请及时绑定邮箱和手机立即绑定

这个有点不太了解,关于servlet之间的通信方法有哪些?如何具体实现?

这个有点不太了解,关于servlet之间的通信方法有哪些?如何具体实现?

千万里不及你 2021-10-14 15:11:49
最好能给个示例。如何从将一个HTTP请求从一个servlet发送到另一个servlet.如何使用forware()以及include()方法?
查看完整描述

3 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

一个servlet直接调用另一个servlet的doget 或 dopost方法不行吗? 将所有参数都传过去。

forware(String url)和include() 是RequestDispatcher接口定义的方法,前者是直接转到另一个url的。后者是将另一个url的处理过程包含在内

查看完整回答
反对 回复 2021-10-18
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String operation = request.getParameter("operation");
if (operation == null) {
operation = "unknown";
}
String address;
if (operation.equals("order")) {
address = "/WEB-INF/Order.jsp";
} else if (operation.equals("cancel")) {
address = "/WEB-INF/Cancel.jsp";
} else {
address = "/WEB-INF/UnknownOperation.jsp";
}
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}

差别:使用sendRedirect时
– 用户可以看到JSP的URL(使用
RequestDispatcher.forward时用户只能看到servlet的
URL)
– 客户程序要经过两次往返(而forward只需一次)
• sendRedirect的优点
– 用户可以单独访问JSP页面
• 用户能够保存JSP页面的地址
• sendRedirect的缺点
– 由于用户可以在不首先经过servlet的情况下访问JSP页
面,所以,JSP页面所需的数据有可能不存在。
• 因此,JSP页面需要编写代码检查这种情况。



查看完整回答
反对 回复 2021-10-18
?
月关宝盒

TA贡献1772条经验 获得超5个赞

以下是几种常调用的方法
Servlet to Servlet Communication

Listing 1: ServletBase
public class ServletBase extends HttpServlet{
static Connection databaseConnection = null;
public void init(ServletConfig _config) throws
ServletException{
super.init(_config);
if ( databaseConnection == null )
//- Open up the database connection
}
protected boolean isLoggedOn( String _username ){
return true;
}
protected boolean logUserOn( String _username ){
return true;
}
}
Listing 2: Using the NewSerletBase Class
public class logonServlet extends ServletBase{
public void service(HttpServletRequest _req, HttpServletRe-
sponse _res) throws ServletException{
if ( isLoggedOn( _req.getParameter(襏SERNAME? ){
//- Display a message indicating they are already logged on
}else{
logUserOn( _req.getParameter(襏SERNAME? );
}
}
}
Listing 3: Storing an Object
public class logonServlet extends HttpServlet{
public void service(HttpServletRequest _req, HttpServletRe-
sponse _res) throws ServletException{
ServletContext thisContext = getServletContext();
//-- Assume some method creates a new connection class
Connection newConnection = createConnection();
thisContext.setAttribute( database.connection? newConnection
);
//-- Return some output to the client
}
}
Listing 4: retrieving an Object
public class logoffServlet extends HttpServlet{
public void service(HttpServletRequest _req, HttpServletRe-
sponse _res) throws ServletException{
ServletContext thisContext = getServletContext();
//-- Assume some method creates a new connection class
Connection newConnection = thisContext.getAttribute(
database.connection?;
if ( newConnection == null )
//- Database has not been opened yet
//-- Return some output to the client
}
}
Listing 5: Looking at All the Objects
public class allServlet extends HttpServlet{
public void service(HttpServletRequest _req, HttpServletRe-
sponse _res) throws ServletException{
ServletContext thisContext = getServletContext();
//-- Assume some method creates a new Connection class
Enumeration E = thisContext.getAttributeNames();
while ( E.hasMoreElements() ){
String name = (String)E.nextElement();
System.out.println( "Object: " + name );
}
}
}
Listing 6: Retrieving Remote Contexts
public class otherServlet extends HttpServlet{
public void service(HttpServletRequest _req, HttpServletRe-
sponse _res) throws ServletException{
ServletContext otherContext =
getServletContext(http://<otherdomain>/servlet/allServlet?;
//-- Assume some method creates a new Connection class
Enumeration E = otherContext.getAttributeNames();
while ( E.hasMoreElements() ){
String name = (String)E.nextElement();
System.out.println( "Object: " + name );
}
}
}
Listing 7: Forwarding a Request
public class forwardServlet extends HttpServlet{
public void service(HttpServletRequest _req, HttpServletRe-
sponse _res) throws ServletException{
ServletContext xt = getServletContext();
RequestDispatcher xyzServlet =
xt.getRequestDispatcher(http://<domain>/servlet/xyzServlet?;
//- Do any preliminary processing
_req.setAttribute( database.results? new Results() );
xyzServlet.forward( _req, _res );
}
}
Listing 8: Inserting Content
public class insertServlet extends HttpServlet{
public void service(HttpServletRequest _req, HttpServletRe-
sponse _res) throws ServletException{
ServletContext xt = getServletContext();
RequestDispatcher xyzServlet =
xt.getRequestDispatcher(http://<domain>/servlet/xyzServlet?;
PrintWriter Out = _res.getWriter();
Out.println( this is from the insertServlet ?);
for(int x=0; x < 10; x++ )
xyzServlet.insert( _req, _res );
Out.println( this is the end of the print servlet ?);
}
}

/////////////////////////////////////////
forward方法是把请求的内容转发到另外的一个servlet.而include是把另一个servlet处理过后的内容拿过来.
举例来说比如在servlet1打一句out.print("1111"),servlet2打上out.print("22222"),在servlet1中用forward命令会转到servlet2中,显示22222.
而在servlet1中使用include方法会依然在servlet1的页面中,但是在1111后打出22222



查看完整回答
反对 回复 2021-10-18
  • 3 回答
  • 0 关注
  • 213 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信