当前位置:首页>开发>正文

请教如何在servlet与java之间传递对象 servlet 之间怎么传递数据

2023-06-27 22:29:37 互联网 未知 开发

 请教如何在servlet与java之间传递对象 servlet 之间怎么传递数据

请教如何在servlet与java之间传递对象

用流试试。
public class TestBean implements Serializable{
private String name
……

}//java bean

……
//构造一个TestBean对象
TestBean bean = new TestBean(txtOutput.getText())

//发送OutputStream out = con.getOutputStream()
ObjectOutputStream objOutStream = new ObjectOutputStream(out)
objOutStream.writeObject(bean)
objOutStream.flush()
……
使用ObjectOutputStream发送流到服务器servlet

servlet 之间怎么传递数据

一、jsp往 servlet 传 : 1.jsp页面添加标签 : 2 . servlet doPost方法中取:request.getParameter("test") 二、servlet 往jsp 传1.servlet doPost方法中往session传值:request.getAttrebute().getSession().put(key , value)2.jsp java代码取:<% request.getAttrebute().getSeesion().getString(key) %>

servlet之间的通信方法有哪些?如何具体实现?

以下是几种常调用的方法
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:///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:///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:///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

请问javascript和servlet之间怎么传值?

JavaScript部分:
var xmlhttp = CreateRequest()

function CreateRequest() {
 var xmlResquest
 try {
  xmlResquest = new ActiveXObject("Msxml2.XMLHTTP")
 }
 catch (e) {
  try {
   xmlResquest = new ActiveXObject("Microsoft.XMLHTTP")
  }
  catch (e) {
   try {
    xmlResquest = new XMLHttpRequest()
   }
   catch (e) {
   }
  }
 }
 return xmlResquest
}

function sendrequest() {
 xmlhttp.open("get", "servlet/GetMenu?p=11113111&fresh="   Math.random())//你的Servlet地址和你要传的值就在这里,Math.random()产生一个随机数,解决缓存问题,使每次请求相当于一个新请求。
 xmlhttp.onreadystatechange = getmenu
 xmlhttp.send(null)
}

function getmenu() {
 if (xmlhttp.readyState == 4) {
  if (xmlhttp.status == 200) {
    alert(xmlhttp.responseText)
//xmlhttp.responseText就 是 Servlet的out.print("")的值 了··
   }

  }
 else {

 }
}

================================
Servlet部分:

取值就用你平时用的request.getParameter("XXX")就可以了

要返回到JavaScript的值就用:
out.print("")
就OK了··
==========================================
这 是最简单的Ajax的例子了··

servlet程序中如何传递值

类似asp中的application方法

jsp 也有 application 内置对象

在jsp页面中 ,可以通过

<%
application.setAttrubute("name","值")//设值
%>

在别的地方获得

<%
application.getAttribute("name")
%>

要是存放在 web.xml中的初始化参数 ,可以通过

application.getInitParameter("参数名称");获得

参数名称 是在 web.xml 设定的 。

servlet是一样的 ,
application,jsp内置了,他自动获取
servlet要先获得application对象,
ServletContext application = getServletConfig().getServletContext()
然后再执行上面说的。

只在servlet之间传递参数,用session,不用jsp,怎么做

什么叫session ? 这个是需要一个JSESSIONID来维持的。
而servlet之间也需要这个。

如果你是2个servlet之间传递,而不是页面和servlet之间(希望你能明白之间的区别),
可以用request.setAttribute的方式给下一个servlet发参数

servlet怎么通过链接向servlet传值

像这样就可以了:
<a href="/JSPFileUploadDownload/servlet/FileDownServlet?参数名=值">下载</a>

你在FileDownServle中通过request.getParameter(String name)方法来获得值

如果还有不明白的,HI我。