以下是几种常调用的方法
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 ?);
}
}
关于回答网友有关servlet和servlet之间通信的问题(建议放入精华区)-JSP教程,Jsp/Servlet
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 关于回答网友有关servlet和servlet之间通信的问题(建议放入精华区)-JSP教程,Jsp/Servlet
相关推荐
- 暂无文章
