怎样调用另一页面的变量? 在<%! %>中声明就可以了, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以先把变量放到session里:session.setAttribute("UserName ",UserName );再把它取出来:String UserName =(String)session.getAttribute("UserName"); 如果是两个关联的页面 比如说A页面转到B页面 可以把用传的变量放在url 如下http://community.csdn.net/Expert/topic/4261/4261203.xml?temp=.6280329取得方法:request.getParameter("temp");就可以得到temp的值如果是两个不关联的页面用楼上的就ok了我也是刚学不久 也不知对错 就个人意见 jsp页面之间用session或者request。隐藏的话,可以在子窗口内用javascript引用父窗口声明的javascript变量。 如果用隐藏域的话,可以在子窗口内用javascript引用父窗口的域。 /**addproduct.jsp文件:*/<%@ page contentType="text/html; charset=gb2312" language="java" errorPage=""%><%@ page import="java.sql.*" %><%@ page import="java.io.*" %><%@ page language="java" %><%@ page import="java.util.*"%><%@ page import="java.util.Date,java.text.DateFormat,java.io.File" %><%@ page import="javax.servlet.*,javax.servlet.http.*,java.text.*" %><html><head><title>发布新产品</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link href="css/style" rel="stylesheet" type="text/css"></head><SCRIPT LANGUAGE="JavaScript"><!--function showimg(){ document.form2.Myimg.src=document.form2.product_url.value; document.form2.Myimg.width=100; }//--></SCRIPT><body bgcolor="#CCCCCC" background="/main_background.jpg"><p><% String action=request.getParameter("action"); if (action==null) {%></p><p><br></p><table width="100%" border="0" height="27"> <tr> <td align="center" background="/images/table.gif">******有限公司--发布新产品</td> </tr></table><form name="form2" method="post" action="uploadproduct.jsp" enctype="multipart/form-data"> <table align="center" width="600" border="0" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF"> <tr> <td width="143" height="27" align="center" bgcolor="#FFFFFF">上传图片:</td> <td width="437" height="27"> <input name="product_url" type="file" id="product_url" size="30" onChange=setTimeout('showimg()',500)> <input type="submit" name="Submit3" value="上传"> <IMG name=Myimg SRC="/main_background.jpg" BORDER="0"> </td> </tr> </table></form><form name="form1" method="post" action="addproduct.jsp?action=add"> <table width="600" height="267" border="0" align="center" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF"> <tr bgcolor="#FFFFFF"> <td width="143" height="27" align="center">产品型号:</td> <td width="350" height="27"> <input name="product_no" type="text" id="product_no" size="60"> </td> </tr> <tr bgcolor="#FFFFFF"> <td height="27" align="center" width="143">产品名称:</td> <td height="27" width="350"> <input name="product_name" type="text" id="procuct_name" size="60"> </td> </tr> <tr bgcolor="#FFFFFF"> <td height="25" align="center" width="143">所属类别:</td> <td height="25" width="350"> <select name="product_style" id="product_style"> <option selected>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>4</option> <option>6</option> </select> </td> </tr> <tr bgcolor="#FFFFFF"> <td height="31" align="center" width="143">产品描述:</td> <td height="31" width="350"> <textarea name="product_info" cols="60" rows="20" id="product_info"></textarea> </td> </tr> <tr bgcolor="#FFFFFF"> <td height="31" align="center">详细包装:</td> <td height="31" align="left"> <textarea name="product_packing" cols="60" rows="10" id="product_packing"></textarea> </td> </tr> <tr bgcolor="#FFFFFF"> <td height="31" colspan="2" align="center"> <input type="submit" name="Submit" value="发布"> <input type="reset" name="Submit2" value="重写"> <input type="button" name="Submit22" value="返回" onClick="window.location.href='index.jsp'"> </td> </tr> </table></form><%}else{%><jsp:useBean id="guestbook" class="guestbook.GuestBookJdbc" scope="page"/><% String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver"; //String URL = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:/htdocs/guestbook/data/guestbook.mdb"; String URL = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=h:/inetpub/wwwrootguestbook/data/productinfo.mdb"; //String USER = "louis"; //String PASS = "5998015"; Connection conn = null; try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL); } catch(ClassNotFoundException ex) { out.println(ex); } catch(SQLException ex) { out.println(ex); } String product_no=request.getParameter("product_no"); String product_name=request.getParameter("product_name"); //String product_url=request.getParameter("product_url"); upfilepath=request.getParameter("upfilepath"); String product_url=upfilepath; String product_style=request.getParameter("product_style"); String product_info=request.getParameter("product_info"); String product_packing=request.getParameter("product_packing"); Statement sqlrs= conn.createStatement() ; String sql="insert into product_info(product_no,product_name,product_url,product_style,product_info,product_packing) values('"+product_no+"','"+product_name+"','"+product_url+"','"+product_style+"','"+product_info+"','"+product_packing+"')"; //String sql="insert into Guestbook(username,tx,email,url,qq,content,ip,sex) values('"+username+"','"+tx+"','"+email+"','"+url+"','"+qq+"','"+content+"','"+ip+"','"+sex+"')";try{ sqlrs.executeUpdate(sql);}catch(Exception e){ out.print("出错了!请写完您的信息,以便我们记录。");}}%><table width="75%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="22" align="center">CopyRight 2003-2005 © *******公司版权所有 </td> </tr></table></body></html>/**upload.jsp文件*/<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><%@ page import="com.jspsmart.upload.*"%><jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /> <html><head><title>上载附件 </title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body background="main_background.jpg"> <center>正在上传文件...<br><p><%!public String upfilepath="";%/><%//上载附件try{ mySmartUpload.initialize(pageContext); mySmartUpload.service(request,response); mySmartUpload.upload(); upfilepath=mySmartUpload.getFiles().getFile(0).getFileName(); mySmartUpload.save("upload");//文件保存的目录为upload out.println("已经成功上传了文件,请查看<a href=upload/"+upfilepath+" "+"target=_blank>这里</a>,看文件是否上传成功<br><p>"); response.sendRedirect("addproduct.jsp"); }catch(Exception e){ e.printStackTrace();}%></body></html>upfilepath是两个页面都需用到的变量,应该怎样调用才能有效? 提交上传按钮后,怎样将上传文件的路径显示到另一个Form中的文本框中?本人在上传文件的upload.jsp中设session.setAttribute("upfilepath ",upfilepath );在addproduct.jsp中String upfilepath2=(String)session.getAttribute("upfilepath");文本框设: <input name="product_path" type="text" id="product_path" value=<%=upfilepath2%> size="60">,测试后文件框显示为null,不知为什么不显示保存文件的路径? 1.问题不清 , Java or Javascript ?2.如果 Java , 概念不清, 就好像一个类 A 直接访问 类 B 的变量而不遵守常见编程约定。3.有些功能需要别的类来协作就按功能划分,不一定要什么东西都写在 JSP 中,可能 JavaBean 或其他的 Java 类能解决问题。 我想动态循环每隔2秒钟改变input里面的值 UserTransaction.begin()的写法 菜鸟紧急求救!!关于进入JSP/ADMIN页面的问题 将stringWriter中的内容写入excel,如果超过65536和行怎么办? jsp中的全程变量问题 希望大家关注一下,发了N多天都没人理我 菜鸟问题 EJB中如何解决逻辑锁的问题? 怎么用session来记录页面中的选中的复选框的内容啊? 新手学习struts2提供的上传但是娶不到上传的文件 求助:高手请进!eclipse+tomcat启动出故障 求gb2312转换为utf-8的一段代码
http://community.csdn.net/Expert/topic/4261/4261203.xml?temp=.6280329
取得方法:request.getParameter("temp");就可以得到temp的值
如果是两个不关联的页面用楼上的就ok了我也是刚学不久 也不知对错 就个人意见
隐藏的话,可以在子窗口内用javascript引用父窗口声明的javascript变量。
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage=""%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page language="java" %>
<%@ page import="java.util.*"%>
<%@ page import="java.util.Date,java.text.DateFormat,java.io.File" %>
<%@ page import="javax.servlet.*,javax.servlet.http.*,java.text.*" %>
<html>
<head>
<title>发布新产品</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/style" rel="stylesheet" type="text/css">
</head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function showimg()
{
document.form2.Myimg.src=document.form2.product_url.value;
document.form2.Myimg.width=100;
}
//-->
</SCRIPT>
<body bgcolor="#CCCCCC" background="/main_background.jpg">
<p>
<%
String action=request.getParameter("action");
if (action==null)
{
%>
</p>
<p><br>
</p>
<table width="100%" border="0" height="27">
<tr>
<td align="center" background="/images/table.gif">******有限公司--发布新产品</td>
</tr>
</table>
<form name="form2" method="post" action="uploadproduct.jsp" enctype="multipart/form-data">
<table align="center" width="600" border="0" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
<tr>
<td width="143" height="27" align="center" bgcolor="#FFFFFF">上传图片:</td>
<td width="437" height="27">
<input name="product_url" type="file" id="product_url" size="30" onChange=setTimeout('showimg()',500)>
<input type="submit" name="Submit3" value="上传">
<IMG name=Myimg SRC="/main_background.jpg" BORDER="0"> </td>
</tr>
</table>
</form>
<form name="form1" method="post" action="addproduct.jsp?action=add">
<table width="600" height="267" border="0" align="center" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
<tr bgcolor="#FFFFFF">
<td width="143" height="27" align="center">产品型号:</td>
<td width="350" height="27">
<input name="product_no" type="text" id="product_no" size="60">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="27" align="center" width="143">产品名称:</td>
<td height="27" width="350">
<input name="product_name" type="text" id="procuct_name" size="60">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="25" align="center" width="143">所属类别:</td>
<td height="25" width="350">
<select name="product_style" id="product_style">
<option selected>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>4</option>
<option>6</option>
</select>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="31" align="center" width="143">产品描述:</td>
<td height="31" width="350">
<textarea name="product_info" cols="60" rows="20" id="product_info"></textarea>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="31" align="center">详细包装:</td>
<td height="31" align="left">
<textarea name="product_packing" cols="60" rows="10" id="product_packing"></textarea>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="31" colspan="2" align="center">
<input type="submit" name="Submit" value="发布">
<input type="reset" name="Submit2" value="重写">
<input type="button" name="Submit22" value="返回" onClick="window.location.href='index.jsp'">
</td>
</tr>
</table>
</form>
<%}else{%><jsp:useBean id="guestbook" class="guestbook.GuestBookJdbc" scope="page"/>
<%
String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
//String URL = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:/htdocs/guestbook/data/guestbook.mdb";
String URL = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=h:/inetpub/wwwrootguestbook/data/productinfo.mdb";
//String USER = "louis";
//String PASS = "5998015";
Connection conn = null;
try
{
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL);
}
catch(ClassNotFoundException ex)
{
out.println(ex);
}
catch(SQLException ex)
{
out.println(ex);
}
String product_no=request.getParameter("product_no");
String product_name=request.getParameter("product_name");
//String product_url=request.getParameter("product_url");
upfilepath=request.getParameter("upfilepath");
String product_url=upfilepath;
String product_style=request.getParameter("product_style");
String product_info=request.getParameter("product_info");
String product_packing=request.getParameter("product_packing");
Statement sqlrs= conn.createStatement() ;
String sql="insert into product_info(product_no,product_name,product_url,product_style,product_info,product_packing) values('"+product_no+"','"+product_name+"','"+product_url+"','"+product_style+"','"+product_info+"','"+product_packing+"')";
//String sql="insert into Guestbook(username,tx,email,url,qq,content,ip,sex) values('"+username+"','"+tx+"','"+email+"','"+url+"','"+qq+"','"+content+"','"+ip+"','"+sex+"')";
try
{
sqlrs.executeUpdate(sql);
}catch(Exception e)
{
out.print("出错了!请写完您的信息,以便我们记录。");
}}%>
<table width="75%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="22" align="center">CopyRight 2003-2005 © *******公司版权所有 </td>
</tr>
</table>
</body>
</html>/**upload.jsp文件*/<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<html>
<head>
<title>上载附件 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body background="main_background.jpg">
<center>正在上传文件...<br><p>
<%!public String upfilepath="";%/><%
//上载附件try
{ mySmartUpload.initialize(pageContext);
mySmartUpload.service(request,response);
mySmartUpload.upload();
upfilepath=mySmartUpload.getFiles().getFile(0).getFileName();
mySmartUpload.save("upload");//文件保存的目录为upload
out.println("已经成功上传了文件,请查看<a href=upload/"+upfilepath+" "+"target=_blank>这里</a>,看文件是否上传成功<br><p>");
response.sendRedirect("addproduct.jsp");
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>upfilepath是两个页面都需用到的变量,应该怎样调用才能有效?
文本框设: <input name="product_path" type="text" id="product_path" value=<%=upfilepath2%> size="60">,测试后文件框显示为null,不知为什么不显示保存文件的路径?
2.如果 Java , 概念不清, 就好像一个类 A 直接访问 类 B 的变量而不遵守常见编程约定。
3.有些功能需要别的类来协作就按功能划分,不一定要什么东西都写在 JSP 中,可能 JavaBean 或其他的 Java 类能解决问题。