亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這個有點不太了解,關于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 關注
  • 245 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號